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;

/// Agent descriptor — name and i18n role key
struct Agent {
    name: &'static str,
    role_key: &'static str,
    emoji: &'static str,
}

const AGENTS: &[Agent] = &[
    Agent {
        name: "Architect",
        role_key: "vapora-agent-architect-role",
        emoji: "🏗️",
    },
    Agent {
        name: "Developer",
        role_key: "vapora-agent-developer-role",
        emoji: "💻",
    },
    Agent {
        name: "CodeReviewer",
        role_key: "vapora-agent-reviewer-role",
        emoji: "🔍",
    },
    Agent {
        name: "Tester",
        role_key: "vapora-agent-tester-role",
        emoji: "🧪",
    },
    Agent {
        name: "Documenter",
        role_key: "vapora-agent-documenter-role",
        emoji: "📚",
    },
    Agent {
        name: "Marketer",
        role_key: "vapora-agent-marketer-role",
        emoji: "📣",
    },
    Agent {
        name: "Presenter",
        role_key: "vapora-agent-presenter-role",
        emoji: "🎤",
    },
    Agent {
        name: "DevOps",
        role_key: "vapora-agent-devops-role",
        emoji: "🚀",
    },
    Agent {
        name: "Monitor",
        role_key: "vapora-agent-monitor-role",
        emoji: "📊",
    },
    Agent {
        name: "Security",
        role_key: "vapora-agent-security-role",
        emoji: "🔒",
    },
    Agent {
        name: "ProjectManager",
        role_key: "vapora-agent-pm-role",
        emoji: "📋",
    },
    Agent {
        name: "DecisionMaker",
        role_key: "vapora-agent-decisionmaker-role",
        emoji: "⚖️",
    },
];

const TECH_BADGES: &[&str] = &[
    "Rust (21 crates)",
    "Axum REST API",
    "SurrealDB",
    "NATS JetStream",
    "Leptos WASM",
    "Kubernetes",
    "Prometheus",
    "Knowledge Graph",
    "RLM (Hybrid Search)",
    "A2A Protocol",
    "MCP Server",
    "Webhook Channels",
    "Capability Packages",
];

/// Feature descriptor
struct Feature {
    title_key: &'static str,
    desc_key: &'static str,
    emoji: &'static str,
}

const FEATURES: &[Feature] = &[
    Feature {
        title_key: "vapora-feat-agents-title",
        desc_key: "vapora-feat-agents-desc",
        emoji: "🤖",
    },
    Feature {
        title_key: "vapora-feat-orchestration-title",
        desc_key: "vapora-feat-orchestration-desc",
        emoji: "🧠",
    },
    Feature {
        title_key: "vapora-feat-rlm-title",
        desc_key: "vapora-feat-rlm-desc",
        emoji: "🔎",
    },
    Feature {
        title_key: "vapora-feat-a2a-title",
        desc_key: "vapora-feat-a2a-desc",
        emoji: "🔗",
    },
    Feature {
        title_key: "vapora-feat-knowledge-title",
        desc_key: "vapora-feat-knowledge-desc",
        emoji: "🗺️",
    },
    Feature {
        title_key: "vapora-feat-nats-title",
        desc_key: "vapora-feat-nats-desc",
        emoji: "📨",
    },
    Feature {
        title_key: "vapora-feat-surrealdb-title",
        desc_key: "vapora-feat-surrealdb-desc",
        emoji: "🗄️",
    },
    Feature {
        title_key: "vapora-feat-api-title",
        desc_key: "vapora-feat-api-desc",
        emoji: "🌐",
    },
    Feature {
        title_key: "vapora-feat-webhooks-title",
        desc_key: "vapora-feat-webhooks-desc",
        emoji: "🔔",
    },
    Feature {
        title_key: "vapora-feat-capabilities-title",
        desc_key: "vapora-feat-capabilities-desc",
        emoji: "📦",
    },
];

/// Unified Vapora page — works in both SSR and WASM contexts
#[component]
pub fn UnifiedVaporaPage(
    #[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/vapora_w.svg"
                  alt="Vapora"
                  class="h-28 dark:hidden"
                />
                <img
                  src="/images/logos/projects/vapora.svg"
                  alt="Vapora"
                  class="h-28 hidden dark:block"
                />
              </div>
              <p class="ds-caption ds-text-secondary mb-2 font-mono italic">
                {content.t("vapora-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("vapora-badge")}
              </span>
              <h1 class="text-balance text-4xl font-bold tracking-tight ds-text sm:text-5xl mb-4">
                {content.t("vapora-page-title")}
              </h1>
              <p class="ds-body ds-text-secondary max-w-3xl mx-auto">
                {content.t("vapora-page-subtitle")}
              </p>
              <div class="flex flex-col sm:flex-row gap-4 justify-center mt-8">
                <a
                  href="https://vapora.dev"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-primary"
                >
                  {content.t("vapora-cta-visit")}
                </a>
                <a
                  href="https://github.com/vapora-platform/vapora"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-secondary"
                >
                  {content.t("vapora-cta-explore")}
                </a>
              </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("vapora-architecture-title")}</h2>
              <ImageModal
                src_light="/images/projects/vapora_architecture-light.svg"
                src_dark="/images/projects/vapora_architecture-dark.svg"
                alt="Vapora architecture diagram"
                expand_label=content.t("common-expand-image")

              />
            </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("vapora-problems-title")}</h2>
              </div>
              <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
                <div class="ds-bg-page p-ds-6 ds-rounded-lg ds-border border ds-shadow-sm">
                  <div class="text-2xl mb-3">"❌"</div>
                  <h3 class="ds-heading-4 ds-text mb-2">{content.t("vapora-problem-1-title")}</h3>
                  <p class="ds-caption ds-text-secondary">{content.t("vapora-problem-1-desc")}</p>
                </div>
                <div class="ds-bg-page p-ds-6 ds-rounded-lg ds-border border ds-shadow-sm">
                  <div class="text-2xl mb-3">"❌"</div>
                  <h3 class="ds-heading-4 ds-text mb-2">{content.t("vapora-problem-2-title")}</h3>
                  <p class="ds-caption ds-text-secondary">{content.t("vapora-problem-2-desc")}</p>
                </div>
                <div class="ds-bg-page p-ds-6 ds-rounded-lg ds-border border ds-shadow-sm">
                  <div class="text-2xl mb-3">"❌"</div>
                  <h3 class="ds-heading-4 ds-text mb-2">{content.t("vapora-problem-3-title")}</h3>
                  <p class="ds-caption ds-text-secondary">{content.t("vapora-problem-3-desc")}</p>
                </div>
                <div class="ds-bg-page p-ds-6 ds-rounded-lg ds-border border ds-shadow-sm">
                  <div class="text-2xl mb-3">"❌"</div>
                  <h3 class="ds-heading-4 ds-text mb-2">{content.t("vapora-problem-4-title")}</h3>
                  <p class="ds-caption ds-text-secondary">{content.t("vapora-problem-4-desc")}</p>
                </div>
              </div>
            </div>
          </section>

          // ── How It Works (Features) ──────────────────────────────────────────
          <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("vapora-features-title")}</h2>
              </div>
              <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                {FEATURES.iter().map(|f| {
                    let title = content.t(f.title_key);
                    let desc  = content.t(f.desc_key);
                    let emoji = f.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>

          // ── Tech Stack ───────────────────────────────────────────────────────
          <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("vapora-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>

          // ── Available Agents ─────────────────────────────────────────────────
          <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("vapora-agents-title")}</h2>
              </div>
              <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-4">
                {AGENTS.iter().map(|a| {
                    let role = content.t(a.role_key);
                    let name = a.name;
                    let emoji = a.emoji;
                    view! {
                        <div class="ds-bg p-4 ds-rounded-lg text-center border ds-border ds-shadow-sm">
                          <div class="text-2xl mb-2">{emoji}</div>
                          <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>

          // ── 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("vapora-cta-title")}</h2>
              <p class="ds-text-secondary mb-8 ds-body">{content.t("vapora-cta-subtitle")}</p>
              <div class="flex flex-col sm:flex-row gap-4 justify-center">
                <a
                  href="https://vapora.dev"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-primary"
                >
                  {content.t("vapora-cta-visit")}
                </a>
                <a
                  href="https://github.com/vapora-platform/vapora"
                  target="_blank"
                  rel="noopener noreferrer"
                  class="no-underline ds-btn-secondary"
                >
                  {content.t("vapora-cta-explore")}
                </a>
              </div>
            </div>
          </section>


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

        </div>
    }
}
