use leptos::prelude::*;
use rustelo_content::rustelo_core_lib::i18n::content_helper::create_content_provider_reactive;

use rustelo_components_leptos::ImageModal;
use crate::content_graph::ProjectGraphSection;

struct Problem {
    title_key: &'static str,
    desc_key: &'static str,
}

const PROBLEMS: &[Problem] = &[
    Problem {
        title_key: "typedialog-problem-1-title",
        desc_key: "typedialog-problem-1-desc",
    },
    Problem {
        title_key: "typedialog-problem-2-title",
        desc_key: "typedialog-problem-2-desc",
    },
    Problem {
        title_key: "typedialog-problem-3-title",
        desc_key: "typedialog-problem-3-desc",
    },
    Problem {
        title_key: "typedialog-problem-4-title",
        desc_key: "typedialog-problem-4-desc",
    },
];

struct HowItWorks {
    title_key: &'static str,
    desc_key: &'static str,
    emoji: &'static str,
}

const HOW_IT_WORKS: &[HowItWorks] = &[
    HowItWorks {
        title_key: "typedialog-how-forms-title",
        desc_key: "typedialog-how-forms-desc",
        emoji: "\u{1f4c4}",
    },
    HowItWorks {
        title_key: "typedialog-how-execution-title",
        desc_key: "typedialog-how-execution-desc",
        emoji: "\u{2699}\u{fe0f}",
    },
    HowItWorks {
        title_key: "typedialog-how-factory-title",
        desc_key: "typedialog-how-factory-desc",
        emoji: "\u{1f3ed}",
    },
    HowItWorks {
        title_key: "typedialog-how-ai-title",
        desc_key: "typedialog-how-ai-desc",
        emoji: "\u{1f916}",
    },
    HowItWorks {
        title_key: "typedialog-how-infra-title",
        desc_key: "typedialog-how-infra-desc",
        emoji: "\u{2601}\u{fe0f}",
    },
    HowItWorks {
        title_key: "typedialog-how-roundtrip-title",
        desc_key: "typedialog-how-roundtrip-desc",
        emoji: "\u{1f504}",
    },
];

struct Backend {
    name: &'static str,
    role_key: &'static str,
}

const BACKENDS: &[Backend] = &[
    Backend {
        name: "CLI",
        role_key: "typedialog-backend-cli-role",
    },
    Backend {
        name: "TUI",
        role_key: "typedialog-backend-tui-role",
    },
    Backend {
        name: "Web",
        role_key: "typedialog-backend-web-role",
    },
    Backend {
        name: "AI",
        role_key: "typedialog-backend-ai-role",
    },
    Backend {
        name: "Agent",
        role_key: "typedialog-backend-agent-role",
    },
    Backend {
        name: "Prov-Gen",
        role_key: "typedialog-backend-provgen-role",
    },
];

const TECH_BADGES: &[&str] = &[
    "Rust (8 crates)",
    "Nickel Contracts",
    "TOML Forms",
    "Inquire (CLI)",
    "Ratatui (TUI)",
    "Axum (Web)",
    "Fluent i18n",
    "Tera Templates",
    "Nushell Plugin",
    "Claude API",
    "OpenAI API",
    "Gemini API",
    "Ollama (local)",
    "Multi-Cloud IaC",
];

#[component]
pub fn UnifiedTypedialogPage(
    #[prop(default = rustelo_content::rustelo_core_lib::config::get_default_language().to_string())]
    _language: String,
    #[prop(optional)] lang_content: Option<std::collections::HashMap<String, String>>,
) -> impl IntoView {
    let content = create_content_provider_reactive(lang_content.clone());

    view! {
        <div class="ds-bg-page min-h-screen">

          // ── Hero ─────────────────────────────────────────────────────────────
          <section class="relative py-ds-8 ds-container">
            <div class="mx-auto max-w-4xl text-center">
              <div class="flex flex-col items-center gap-4 mb-6">
                <img
                  src="/images/logos/projects/typedialog.svg"
                  alt="TypeDialog"
                  class="h-20"
                />
              </div>
              <p class="ds-caption ds-text-secondary mb-2 font-mono italic">
                {content.t("typedialog-tagline")}
              </p>
              <span class="inline-block px-3 py-1 ds-caption ds-bg border ds-border ds-rounded-md ds-text-secondary mb-4">
                {content.t("typedialog-badge")}
              </span>
              <h1 class="text-balance text-4xl font-bold tracking-tight ds-text sm:text-5xl mb-4">
                {content.t("typedialog-page-title")}
              </h1>
              <p class="ds-body ds-text-secondary max-w-3xl mx-auto">
                {content.t("typedialog-page-subtitle")}
              </p>
              <div class="flex flex-col sm:flex-row gap-4 justify-center mt-8">
                <a
                  href="https://repo.example.com/org/typedialog"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-primary"
                >
                  {content.t("typedialog-cta-explore")}
                </a>
              </div>
            </div>
          </section>

          // ── Problems ─────────────────────────────────────────────────────────
          <section class="py-ds-6 ds-bg ds-rounded-lg">
            <div class="ds-container-lg ds-container">
              <div class="text-center mb-ds-12">
                <h2 class="text-3xl font-bold ds-text">{content.t("typedialog-problems-title")}</h2>
              </div>
              <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
                {PROBLEMS.iter().map(|p| {
                    let title = content.t(p.title_key);
                    let desc  = content.t(p.desc_key);
                    view! {
                        <div class="ds-bg-page p-ds-6 ds-rounded-lg ds-border border ds-shadow-sm">
                          <h3 class="ds-heading-4 ds-text mb-2">{title}</h3>
                          <p class="ds-caption ds-text-secondary">{desc}</p>
                        </div>
                    }
                }).collect_view()}
              </div>
            </div>
          </section>

          // ── How It Works ─────────────────────────────────────────────────────
          <section class="py-ds-6 ds-rounded-lg">
            <div class="ds-container-lg ds-container">
              <div class="text-center mb-ds-12">
                <h2 class="text-3xl font-bold ds-text">{content.t("typedialog-how-title")}</h2>
              </div>
              <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                {HOW_IT_WORKS.iter().map(|h| {
                    let title = content.t(h.title_key);
                    let desc  = content.t(h.desc_key);
                    let emoji = h.emoji;
                    view! {
                        <div class="ds-bg p-ds-6 ds-rounded-lg ds-shadow-sm border-l-4 ds-border">
                          <div class="flex items-center gap-3 mb-3">
                            <span class="text-xl">{emoji}</span>
                            <h3 class="ds-heading-4 ds-text">{title}</h3>
                          </div>
                          <p class="ds-caption ds-text-secondary leading-relaxed">{desc}</p>
                        </div>
                    }
                }).collect_view()}
              </div>
            </div>
          </section>

          // ── Architecture Diagram ─────────────────────────────────────────────
          <section class="py-ds-6 ds-container">
            <div class="mx-auto max-w-5xl text-center">
              <h2 class="text-3xl font-bold ds-text mb-ds-8">{content.t("typedialog-architecture-title")}</h2>
              <ImageModal
                src_light="/images/projects/typedialog_architecture-light.svg"
                src_dark="/images/projects/typedialog_architecture-dark.svg"
                alt="TypeDialog architecture diagram"
                expand_label=content.t("common-expand-image")

              />
            </div>
          </section>

          // ── Backends ─────────────────────────────────────────────────────────
          <section class="py-ds-6 ds-bg ds-rounded-lg">
            <div class="ds-container-lg ds-container">
              <div class="text-center mb-ds-8">
                <h2 class="text-3xl font-bold ds-text">{content.t("typedialog-backends-title")}</h2>
              </div>
              <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-6 gap-4">
                {BACKENDS.iter().map(|b| {
                    let role = content.t(b.role_key);
                    let name = b.name;
                    view! {
                        <div class="ds-bg-page p-4 ds-rounded-lg text-center border ds-border ds-shadow-sm">
                          <div class="ds-caption font-bold ds-text mb-1">{name}</div>
                          <div class="text-xs ds-text-secondary">{role}</div>
                        </div>
                    }
                }).collect_view()}
              </div>
            </div>
          </section>

          // ── Tech Stack ───────────────────────────────────────────────────────
          <section class="py-ds-6 ds-rounded-lg">
            <div class="ds-container-lg ds-container">
              <div class="text-center mb-ds-8">
                <h2 class="text-3xl font-bold ds-text">{content.t("typedialog-tech-stack-title")}</h2>
              </div>
              <div class="flex flex-wrap gap-3 justify-center">
                {TECH_BADGES.iter().map(|badge| {
                    view! {
                        <span class="px-3 py-2 ds-bg-page border ds-border ds-rounded-md ds-caption font-mono ds-text">
                          {*badge}
                        </span>
                    }
                }).collect_view()}
              </div>
            </div>
          </section>

          // ── CTA ──────────────────────────────────────────────────────────────
          <section class="py-ds-6 ds-bg ds-rounded-lg">
            <div class="mx-auto max-w-3xl ds-container text-center">
              <h2 class="text-3xl font-bold ds-text mb-ds-4">{content.t("typedialog-cta-title")}</h2>
              <p class="ds-text-secondary mb-8 ds-body">{content.t("typedialog-cta-subtitle")}</p>
              <div class="flex flex-col sm:flex-row gap-4 justify-center">
                <a
                  href="https://repo.example.com/org/typedialog"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-primary"
                >
                  {content.t("typedialog-cta-explore")}
                </a>
              </div>
            </div>
          </section>


        <ProjectGraphSection node_id="typedialog".to_string() />

        </div>
    }
}
