"use client";

import { motion } from "motion/react";
import {
  MonitorCheck,
  BookOpen,
  BarChart3,
  ArrowUpRight,
  Hexagon,
  Zap,
  Shield,
  Server,
  Globe,
  RefreshCw,
  Puzzle,
} from "lucide-react";
import { useI18n, type TranslationKey } from "@/lib/i18n";

const PLATFORM_URL = "http://192.168.111.90:8016/console/personal";

const SERVICE_URLS: Record<string, string> = {
  monitoring: "http://192.168.111.90:8018",
  docs: "http://192.168.111.90:8087/sinocode/current/",
  analytics: "http://192.168.111.90:8019",
};

/* ═══ Data ═══ */
const SERVICES = [
  {
    key: "monitoring" as const,
    icon: MonitorCheck,
    accentVar: "--portal-accent-green",
    glowColor: "rgba(52,211,153,0.15)",
    decorIcon: Shield,
  },
  {
    key: "docs" as const,
    icon: BookOpen,
    accentVar: "--portal-accent-violet",
    glowColor: "rgba(139,92,246,0.15)",
    decorIcon: Hexagon,
  },
  {
    key: "analytics" as const,
    icon: BarChart3,
    accentVar: "--portal-accent-cyan",
    glowColor: "rgba(0,229,255,0.15)",
    decorIcon: Zap,
  },
];

interface FeatureItem {
  icon: typeof Zap;
  titleKey: TranslationKey;
  descKey: TranslationKey;
}

const FEATURES: FeatureItem[] = [
  { icon: Zap, titleKey: "portal.features.unified", descKey: "portal.features.unifiedDesc" },
  { icon: Shield, titleKey: "portal.features.security", descKey: "portal.features.securityDesc" },
  { icon: BarChart3, titleKey: "portal.features.monitor", descKey: "portal.features.monitorDesc" },
  { icon: Globe, titleKey: "portal.features.global", descKey: "portal.features.globalDesc" },
  { icon: RefreshCw, titleKey: "portal.features.failover", descKey: "portal.features.failoverDesc" },
  { icon: Puzzle, titleKey: "portal.features.flexible", descKey: "portal.features.flexibleDesc" },
];

interface ModelItem { name: string; provider: string; }
const MODELS: ModelItem[] = [
  { name: "Claude", provider: "Anthropic" },
  { name: "GPT", provider: "OpenAI" },
  { name: "Gemini", provider: "Google" },
  { name: "DeepSeek", provider: "DeepSeek" },
  { name: "Grok", provider: "xAI" },
];

interface ToolItem { name: string; tag: string; }
const TOOLS: ToolItem[] = [
  { name: "Claude Code", tag: "Anthropic" },
  { name: "Codex", tag: "OpenAI" },
  { name: "Gemini CLI", tag: "Google" },
  { name: "OpenCode", tag: "Community" },
  { name: "Droid", tag: "Community" },
];

const fadeUp = {
  hidden: { opacity: 0, y: 24 },
  visible: { opacity: 1, y: 0, transition: { duration: 0.5, ease: "easeOut" as const } },
};
const stagger = { visible: { transition: { staggerChildren: 0.08 } } };

export default function PortalPage() {
  const { t } = useI18n();

  const i18nMap: Record<string, { title: string; desc: string }> = {
    monitoring: { title: t("portal.monitoring"), desc: t("portal.monitoringDesc") },
    docs: { title: t("portal.docs"), desc: t("portal.docsDesc") },
    analytics: { title: t("portal.analytics"), desc: t("portal.analyticsDesc") },
  };

  return (
    <div className="portal-page">
      {/* ── Background layers (preserved) ── */}
      <div className="portal-grid" />
      <div className="portal-orb portal-orb--1" />
      <div className="portal-orb portal-orb--2" />
      <div className="portal-orb portal-orb--3" />
      <div className="portal-particles">
        {Array.from({ length: 6 }).map((_, i) => (
          <div key={i} className="portal-particle" style={{
            left: `${15 + i * 14}%`,
            top: `${20 + (i % 3) * 25}%`,
            animationDelay: `${i * 1.5}s`,
            animationDuration: `${8 + i * 2}s`,
          }} />
        ))}
      </div>

      <main className="portal-main">
        {/* ── 2. HERO ── */}
        <section className="portal-hero">
          <div className="portal-hero-scan" />
          <div className="portal-container portal-hero-grid">
            <motion.div className="portal-hero-copy" initial="hidden" animate="visible" variants={stagger}>
              <motion.div className="portal-hero-badge" variants={fadeUp}>
                {t("portal.hero.badge")}
              </motion.div>
              <motion.h1 variants={fadeUp}>
                <span className="gradient-text">SinoCode</span>
                <span className="hero-sub">{t("portal.subtitle")}</span>
              </motion.h1>
              <motion.p className="portal-hero-desc" variants={fadeUp}>
                {t("portal.hero.desc")}
              </motion.p>
              <motion.div className="portal-hero-actions" variants={fadeUp}>
                <a
                  href={PLATFORM_URL}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="btn-accent rounded-sm px-6 py-3 font-mono text-sm tracking-wider uppercase no-underline font-medium"
                  style={{ background: "var(--accent)", color: "var(--background)", borderColor: "var(--accent)" }}
                >
                  {t("portal.hero.cta")}
                </a>
                <a
                  href={SERVICE_URLS.docs}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="btn-accent rounded-sm px-6 py-3 font-mono text-sm tracking-wider uppercase no-underline"
                >
                  {t("portal.hero.docs")} →
                </a>
              </motion.div>
            </motion.div>

            <motion.div
              className="portal-hero-panel glass"
              initial={{ opacity: 0, x: 32, y: 24 }}
              animate={{ opacity: 1, x: 0, y: 0 }}
              transition={{ delay: 0.2, duration: 0.65, ease: [0.22, 1, 0.36, 1] }}
            >
              <div className="portal-panel-header">
                <div>
                  <div className="portal-panel-label">{t("portal.services.tag")}</div>
                  <div className="portal-panel-title">{t("common.systemOnline")}</div>
                </div>
                <div className="portal-status-dot" />
              </div>

              <div className="portal-panel-shortcuts">
                {SERVICES.map((service) => {
                  const Icon = service.icon;
                  const info = i18nMap[service.key];
                  const url = SERVICE_URLS[service.key];

                  return (
                    <a
                      key={service.key}
                      href={url}
                      target="_blank"
                      rel="noopener noreferrer"
                      className="portal-shortcut"
                      style={{
                        "--card-accent": `var(${service.accentVar})`,
                        "--card-glow": service.glowColor,
                      } as React.CSSProperties}
                    >
                      <div className="portal-shortcut-icon">
                        <Icon size={18} strokeWidth={1.7} />
                      </div>
                      <div className="portal-shortcut-copy">
                        <span className="portal-shortcut-title">{info.title}</span>
                        <span className="portal-shortcut-desc">{info.desc}</span>
                      </div>
                      <ArrowUpRight className="portal-shortcut-arrow" />
                    </a>
                  );
                })}
              </div>
            </motion.div>
          </div>

          <div className="portal-hero-voltage" aria-hidden="true">
            {Array.from({ length: 5 }).map((_, i) => (
              <div key={i} className="portal-voltage-bar" />
            ))}
          </div>
        </section>

        {/* ── 3. FEATURES ── */}
        <section id="features" className="portal-section">
          <div className="portal-container">
            <motion.div initial="hidden" whileInView="visible" viewport={{ once: true }} variants={fadeUp}>
              <div className="portal-section-tag">{t("portal.features.tag")}</div>
              <h2 className="portal-section-title">{t("portal.features.title")}</h2>
            </motion.div>
            <motion.div
              className="portal-features-grid"
              initial="hidden"
              whileInView="visible"
              viewport={{ once: true, amount: 0.2 }}
              variants={stagger}
            >
              {FEATURES.map((f) => {
                const Icon = f.icon;
                return (
                  <motion.div key={f.titleKey} className="glass portal-feature-card p-6" variants={fadeUp}>
                    <div
                      className="flex items-center justify-center w-10 h-10 rounded-lg mb-4"
                      style={{ background: "var(--btn-active-bg)", border: "1px solid var(--surface-border)", color: "var(--text-accent)" }}
                    >
                      <Icon size={20} />
                    </div>
                    <h3 className="text-sm font-semibold tracking-wide mb-2 text-t-primary">{t(f.titleKey)}</h3>
                    <p className="text-sm leading-relaxed text-t-muted">{t(f.descKey)}</p>
                  </motion.div>
                );
              })}
            </motion.div>
          </div>
        </section>

        {/* ── 4. SERVICES ── */}
        <section id="services" className="portal-section">
          <div className="portal-container">
            <motion.div initial="hidden" whileInView="visible" viewport={{ once: true }} variants={fadeUp}>
              <div className="portal-section-tag">{t("portal.services.tag")}</div>
              <h2 className="portal-section-title">{t("portal.services.title")}</h2>
            </motion.div>
            <div className="portal-cards">
              {SERVICES.map((service, i) => {
                const Icon = service.icon;
                const DecorIcon = service.decorIcon;
                const info = i18nMap[service.key];
                const url = SERVICE_URLS[service.key];

                return (
                  <motion.a
                    key={service.key}
                    href={url}
                    target="_blank"
                    rel="noopener noreferrer"
                    className="portal-card"
                    style={{
                      "--card-accent": `var(${service.accentVar})`,
                      "--card-glow": service.glowColor,
                    } as React.CSSProperties}
                    initial={{ opacity: 0, y: 40 }}
                    whileInView={{ opacity: 1, y: 0 }}
                    viewport={{ once: true }}
                    transition={{ delay: i * 0.1, duration: 0.5, ease: [0.22, 1, 0.36, 1] }}
                    whileHover={{ y: -10, transition: { type: "spring", stiffness: 400, damping: 25 } }}
                  >
                    <span className="portal-corner portal-corner--tl" />
                    <span className="portal-corner portal-corner--tr" />
                    <span className="portal-corner portal-corner--bl" />
                    <span className="portal-corner portal-corner--br" />
                    <DecorIcon className="portal-card-decor" />
                    <div className="portal-card-icon"><Icon strokeWidth={1.5} /></div>
                    <h3 className="portal-card-title">{info.title}</h3>
                    <p className="portal-card-desc">{info.desc}</p>
                    <div className="portal-card-action">
                      <span>{t("portal.enter")}</span>
                      <ArrowUpRight className="portal-card-action-icon" />
                    </div>
                    <div className="portal-card-bar" />
                  </motion.a>
                );
              })}
            </div>
          </div>
        </section>

        {/* ── 5. ECOSYSTEM ── */}
        <section id="ecosystem" className="portal-section">
          <div className="portal-container">
            <motion.div initial="hidden" whileInView="visible" viewport={{ once: true }} variants={fadeUp}>
              <div className="portal-section-tag">{t("portal.ecosystem.tag")}</div>
              <h2 className="portal-section-title">{t("portal.ecosystem.title")}</h2>
            </motion.div>

            <motion.div initial="hidden" whileInView="visible" viewport={{ once: true }} variants={fadeUp}>
              <h3 className="text-sm font-mono tracking-widest uppercase text-t-muted mb-4">{t("portal.ecosystem.models")}</h3>
            </motion.div>
            <motion.div className="portal-model-grid mb-10" initial="hidden" whileInView="visible" viewport={{ once: true, amount: 0.2 }} variants={stagger}>
              {MODELS.map((m) => (
                <motion.div key={m.name} className="glass portal-model-card p-5 rounded-xl" variants={fadeUp}>
                  <div className="text-base font-bold tracking-wide text-t-primary mb-1">{m.name}</div>
                  <div className="text-xs font-mono text-t-muted tracking-wider uppercase">{m.provider}</div>
                </motion.div>
              ))}
            </motion.div>

            <motion.div initial="hidden" whileInView="visible" viewport={{ once: true }} variants={fadeUp}>
              <h3 className="text-sm font-mono tracking-widest uppercase text-t-muted mb-1">{t("portal.ecosystem.tools")}</h3>
              <p className="text-sm text-t-muted mb-4">{t("portal.ecosystem.toolsDesc")}</p>
            </motion.div>
            <motion.div className="portal-model-grid" initial="hidden" whileInView="visible" viewport={{ once: true, amount: 0.2 }} variants={stagger}>
              {TOOLS.map((tool) => (
                <motion.div key={tool.name} className="glass portal-tool-card p-5 rounded-xl" variants={fadeUp}>
                  <div className="text-sm font-semibold tracking-wide text-t-primary mb-1">{tool.name}</div>
                  <div className="text-xs font-mono tracking-wider uppercase" style={{ color: "var(--accent-purple)" }}>{tool.tag}</div>
                </motion.div>
              ))}
            </motion.div>
          </div>
        </section>

        {/* ── 6. DISCLAIMER ── */}
        <section className="portal-disclaimer">
          <div className="portal-container">
            <motion.div initial="hidden" whileInView="visible" viewport={{ once: true }} variants={fadeUp}>
              <div className="portal-section-tag">{t("portal.disclaimer.tag")}</div>
              <h2 className="portal-section-title">{t("portal.disclaimer.title")}</h2>
            </motion.div>
            <motion.div className="glass portal-disclaimer-panel rounded-xl" initial="hidden" whileInView="visible" viewport={{ once: true }} variants={fadeUp}>
              <p className="text-sm leading-loose text-t-secondary mb-3">{t("portal.disclaimer.p1")}</p>
              <p className="text-sm leading-loose text-t-secondary mb-4">{t("portal.disclaimer.p2")}</p>
              <div className="pt-3 border-t" style={{ borderColor: "var(--surface-border)" }}>
                <span className="text-xs font-mono text-t-muted tracking-wider">{t("portal.disclaimer.copyright")}</span>
              </div>
            </motion.div>
          </div>
        </section>

        {/* ── Footer status ── */}
        <motion.div
          className="portal-footer portal-container"
          initial={{ opacity: 0 }}
          whileInView={{ opacity: 1 }}
          viewport={{ once: true }}
          transition={{ duration: 0.6 }}
        >
          <div className="portal-status-dot" />
          <span className="portal-status-text">
            <Server className="inline-block h-3 w-3 mr-1" style={{ verticalAlign: "-1px" }} />
            {t("common.systemOnline")}
          </span>
        </motion.div>
      </main>
    </div>
  );
}
