use leptos::prelude::*;
use rustelo_content::rustelo_core_lib::i18n::content_helper::create_content_provider_reactive;
use crate::content_graph::ProjectGraphSection;

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

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

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

const HOW_IT_WORKS: &[HowItWorks] = &[
    HowItWorks {
        title_key: "kogral-how-markdown-title",
        desc_key: "kogral-how-markdown-desc",
        emoji: "\u{1f4dd}",
    },
    HowItWorks {
        title_key: "kogral-how-search-title",
        desc_key: "kogral-how-search-desc",
        emoji: "\u{1f50e}",
    },
    HowItWorks {
        title_key: "kogral-how-config-title",
        desc_key: "kogral-how-config-desc",
        emoji: "\u{2699}\u{fe0f}",
    },
    HowItWorks {
        title_key: "kogral-how-mcp-title",
        desc_key: "kogral-how-mcp-desc",
        emoji: "\u{1f916}",
    },
    HowItWorks {
        title_key: "kogral-how-logseq-title",
        desc_key: "kogral-how-logseq-desc",
        emoji: "\u{1f4d3}",
    },
    HowItWorks {
        title_key: "kogral-how-events-title",
        desc_key: "kogral-how-events-desc",
        emoji: "\u{26a1}",
    },
];

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

const COMPONENTS: &[ComponentEntry] = &[
    ComponentEntry {
        name: "kogral-core",
        role_key: "kogral-comp-core-role",
    },
    ComponentEntry {
        name: "kogral-cli",
        role_key: "kogral-comp-cli-role",
    },
    ComponentEntry {
        name: "kogral-mcp",
        role_key: "kogral-comp-mcp-role",
    },
    ComponentEntry {
        name: "Config System",
        role_key: "kogral-comp-config-role",
    },
    ComponentEntry {
        name: "Storage Factory",
        role_key: "kogral-comp-storage-role",
    },
    ComponentEntry {
        name: "stratum-embeddings",
        role_key: "kogral-comp-embeddings-role",
    },
    ComponentEntry {
        name: "Node Types",
        role_key: "kogral-comp-nodes-role",
    },
    ComponentEntry {
        name: "Relationships",
        role_key: "kogral-comp-relations-role",
    },
    ComponentEntry {
        name: "Multi-Graph",
        role_key: "kogral-comp-multigraph-role",
    },
    ComponentEntry {
        name: "NATS Events",
        role_key: "kogral-comp-events-role",
    },
    ComponentEntry {
        name: "Logseq Blocks",
        role_key: "kogral-comp-logseq-role",
    },
    ComponentEntry {
        name: "Orchestration",
        role_key: "kogral-comp-orchestration-role",
    },
];

const TECH_BADGES: &[&str] = &[
    "Rust Edition 2021",
    "Nickel Config",
    "SurrealDB",
    "stratum-embeddings",
    "rig-core",
    "NATS JetStream",
    "MCP Protocol",
    "Logseq Blocks",
    "Tera Templates",
    "Clap CLI",
    "DashMap",
    "Nushell Scripts",
    "mdBook Docs",
    "TypeDialog",
];

#[component]
pub fn UnifiedKogralPage(
    #[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/kogral.svg"
                  alt="Kogral"
                  class="h-28 dark:hidden"
                />
                <img
                  src="/images/logos/projects/kogral.svg"
                  alt="Kogral"
                  class="h-28 hidden dark:block"
                />
              </div>
              <p class="ds-caption ds-text-secondary mb-2 font-mono italic">
                {content.t("kogral-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("kogral-badge")}
              </span>
              <h1 class="text-balance text-4xl font-bold tracking-tight ds-text sm:text-5xl mb-4">
                {content.t("kogral-page-title")}
              </h1>
              <p class="ds-body ds-text-secondary max-w-3xl mx-auto">
                {content.t("kogral-page-subtitle")}
              </p>
              <div class="flex flex-col sm:flex-row gap-4 justify-center mt-8">
                <a
                  href="https://repo.example.com/org/kogral"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-primary"
                >
                  {content.t("kogral-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("kogral-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("kogral-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>

          // ── Core Components ──────────────────────────────────────────────────
          <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("kogral-components-title")}</h2>
              </div>
              <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-4">
                {COMPONENTS.iter().map(|c| {
                    let role = content.t(c.role_key);
                    let name = c.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("kogral-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("kogral-cta-title")}</h2>
              <p class="ds-text-secondary mb-8 ds-body">{content.t("kogral-cta-subtitle")}</p>
              <div class="flex flex-col sm:flex-row gap-4 justify-center">
                <a
                  href="https://repo.example.com/org/kogral"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-primary"
                >
                  {content.t("kogral-cta-explore")}
                </a>
              </div>
            </div>
          </section>


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

        </div>
    }
}
