"use client";

import Link from "next/link";
import { motion, useReducedMotion } from "motion/react";
import {
  ArrowUpRight,
  Bot,
  Braces,
  Database,
  GitBranch,
  LifeBuoy,
  Mail,
  Sparkles,
  TerminalSquare,
} from "lucide-react";

const navItems = [
  { label: "技术文章", href: "#articles" },
  { label: "日常分享", href: "#life" },
  { label: "联系我", href: "mailto:3505891631@qq.com" },
  { label: "Gitea", href: "http://124.221.147.173:3000/fly/fly-personal-site" },
];

const portals = [
  {
    title: "技术文章",
    href: "#articles",
    eyebrow: "Knowledge archive",
    description: "围绕前端工程、服务端基础和 AI Agent 的技术内容索引。",
    icon: TerminalSquare,
    gradient: "from-cyan-300 via-blue-500 to-violet-500",
  },
  {
    title: "日常分享",
    href: "#life",
    eyebrow: "Life stream",
    description: "记录工具使用、阶段复盘、生活观察和那些亲自走过的经验。",
    icon: LifeBuoy,
    gradient: "from-lime-300 via-emerald-400 to-cyan-400",
  },
];

const noteModules = [
  {
    name: "前端",
    href: "/notes/frontend",
    icon: Braces,
    description: "React、Vue、Next.js、CSS、动画、工程化。",
  },
  {
    name: "后端",
    href: "/notes/backend",
    icon: Database,
    description: "API、数据库、部署、鉴权、缓存、服务基础。",
  },
  {
    name: "AI Agent",
    href: "/notes/ai-agent",
    icon: Bot,
    description: "Agent 工作流、工具调用、提示词和自动化实践。",
  },
];

const lifeTopics = ["Daily", "Reading", "Music", "Photo", "Travel", "Thoughts"];

export function HomePage() {
  const reduceMotion = useReducedMotion();
  const distance = reduceMotion ? 0 : 28;
  const duration = reduceMotion ? 0 : 0.58;
  const easeOut = [0.22, 1, 0.36, 1] as const;

  const container = {
    hidden: {},
    show: {
      transition: {
        staggerChildren: reduceMotion ? 0 : 0.075,
        delayChildren: reduceMotion ? 0 : 0.08,
      },
    },
  };

  const reveal = {
    hidden: { opacity: 0, y: distance, filter: reduceMotion ? "none" : "blur(10px)" },
    show: { opacity: 1, y: 0, filter: "blur(0px)", transition: { duration, ease: easeOut } },
  };

  const scaleReveal = {
    hidden: { opacity: 0, y: distance, scale: reduceMotion ? 1 : 0.96 },
    show: { opacity: 1, y: 0, scale: 1, transition: { duration, ease: easeOut } },
  };

  return (
    <main className="min-h-dvh overflow-hidden bg-[#05050b] text-white">
      <section className="relative isolate min-h-dvh px-5 py-5 sm:px-8 lg:px-10">
        <motion.div
          className="neon-aurora"
          initial={{ opacity: 0, scale: reduceMotion ? 1 : 0.92 }}
          animate={{ opacity: 1, scale: 1 }}
          transition={{ duration: reduceMotion ? 0 : 1.2, ease: "easeOut" }}
        />
        <motion.div
          className="cyber-grid"
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          transition={{ duration: reduceMotion ? 0 : 0.9, delay: reduceMotion ? 0 : 0.18 }}
        />
        <motion.div
          className="scanline"
          initial={{ opacity: 0 }}
          animate={{ opacity: 0.18 }}
          transition={{ duration: reduceMotion ? 0 : 0.8, delay: reduceMotion ? 0 : 0.28 }}
        />

        <motion.header
          initial="hidden"
          animate="show"
          variants={container}
          className="relative z-10 mx-auto flex max-w-7xl items-center justify-between rounded-full border border-white/10 bg-white/[0.04] px-4 py-3 shadow-2xl shadow-cyan-500/10 backdrop-blur-2xl"
        >
          <motion.div variants={scaleReveal}>
            <Link
              href="/"
              className="group inline-flex h-11 items-center gap-3 rounded-full bg-white px-4 text-sm font-black text-[#05050b] outline-none transition hover:bg-cyan-200 focus-visible:ring-2 focus-visible:ring-cyan-300"
            >
              <Sparkles className="h-4 w-4 transition group-hover:rotate-12" />
              fly
            </Link>
          </motion.div>

          <nav aria-label="主导航" className="hidden items-center gap-1 md:flex">
            {navItems.map((item) => (
              <motion.a
                key={item.label}
                href={item.href}
                variants={reveal}
                className="rounded-full px-4 py-2 text-sm text-white/68 outline-none transition hover:bg-white/10 hover:text-white focus-visible:ring-2 focus-visible:ring-fuchsia-300"
              >
                {item.label}
              </motion.a>
            ))}
          </nav>
        </motion.header>

        <div className="relative z-10 mx-auto grid min-h-[calc(100dvh-88px)] max-w-7xl gap-8 py-10 lg:grid-cols-[0.9fr_1.1fr] lg:items-center lg:py-6">
          <motion.div
            initial="hidden"
            animate="show"
            variants={container}
            className="max-w-3xl"
          >
            <motion.p
              variants={reveal}
              className="mb-6 inline-flex rounded-full border border-cyan-300/30 bg-cyan-300/10 px-4 py-2 font-mono text-xs uppercase text-cyan-100 shadow-lg shadow-cyan-500/10"
            >
              frontend engineer / web experience / creative code
            </motion.p>
            <motion.h1
              variants={reveal}
              className="font-display text-7xl font-semibold leading-none tracking-normal text-white sm:text-8xl lg:text-[10rem]"
            >
              fly
            </motion.h1>
            <motion.p variants={reveal} className="mt-7 max-w-2xl text-xl leading-9 text-white/72">
              我是 fly，前端开发工程师。专注现代 Web 界面、工程化体验和 AI Agent 方向的探索。
            </motion.p>
            <motion.div variants={container} className="mt-9 flex flex-col gap-3 sm:flex-row">
              <motion.a
                variants={scaleReveal}
                href="#articles"
                className="group inline-flex h-12 items-center justify-center gap-2 rounded-full bg-gradient-to-r from-cyan-300 via-blue-400 to-fuchsia-400 px-6 text-sm font-black text-[#05050b] shadow-xl shadow-cyan-500/25 outline-none transition hover:scale-[1.02] focus-visible:ring-2 focus-visible:ring-cyan-200"
              >
                进入内容矩阵
                <ArrowUpRight className="h-4 w-4 transition group-hover:-translate-y-0.5 group-hover:translate-x-0.5" />
              </motion.a>
              <motion.a
                variants={scaleReveal}
                href="http://124.221.147.173:3000/fly/fly-personal-site"
                className="inline-flex h-12 items-center justify-center gap-2 rounded-full border border-white/14 bg-white/[0.04] px-6 text-sm font-semibold text-white shadow-xl shadow-fuchsia-500/10 outline-none backdrop-blur transition hover:border-fuchsia-300/70 hover:bg-fuchsia-300/10 focus-visible:ring-2 focus-visible:ring-fuchsia-300"
              >
                <GitBranch className="h-4 w-4" />
                Gitea 仓库
              </motion.a>
              <motion.a
                variants={scaleReveal}
                href="mailto:3505891631@qq.com"
                className="inline-flex h-12 items-center justify-center gap-2 rounded-full border border-cyan-300/24 bg-cyan-300/[0.08] px-6 text-sm font-semibold text-cyan-100 shadow-xl shadow-cyan-500/10 outline-none backdrop-blur transition hover:border-cyan-200/80 hover:bg-cyan-300/14 focus-visible:ring-2 focus-visible:ring-cyan-300"
              >
                <Mail className="h-4 w-4" />
                3505891631@qq.com
              </motion.a>
            </motion.div>
          </motion.div>

          <motion.div
            initial="hidden"
            animate="show"
            variants={container}
            className="grid gap-4"
          >
            {portals.map((portal, index) => {
              const Icon = portal.icon;

              return (
                <motion.a
                  key={portal.title}
                  href={portal.href}
                  variants={scaleReveal}
                  className="group relative overflow-hidden rounded-[1.5rem] border border-white/12 bg-white/[0.055] p-5 shadow-2xl shadow-black/40 outline-none backdrop-blur-2xl transition hover:-translate-y-1 hover:border-white/28 focus-visible:ring-2 focus-visible:ring-cyan-300"
                >
                  <div className={`absolute inset-y-0 left-0 w-1/2 bg-gradient-to-r ${portal.gradient} opacity-20 blur-3xl transition group-hover:opacity-40`} />
                  <div className="relative flex items-start justify-between gap-6">
                    <div>
                      <p className="font-mono text-xs uppercase text-white/48">0{index + 1} / {portal.eyebrow}</p>
                      <h2 className="mt-5 text-3xl font-black text-white sm:text-4xl">{portal.title}</h2>
                      <p className="mt-3 max-w-md text-sm leading-7 text-white/62">{portal.description}</p>
                    </div>
                    <span className={`inline-flex h-14 w-14 shrink-0 items-center justify-center rounded-2xl bg-gradient-to-br ${portal.gradient} text-[#05050b] shadow-lg shadow-cyan-500/20`}>
                      <Icon className="h-6 w-6" />
                    </span>
                  </div>
                </motion.a>
              );
            })}
          </motion.div>
        </div>
      </section>

      <motion.section
        id="articles"
        initial="hidden"
        whileInView="show"
        viewport={{ once: true, amount: 0.18 }}
        variants={container}
        className="relative px-5 py-24 sm:px-8 lg:px-10"
      >
        <motion.div variants={reveal} className="absolute inset-x-0 top-0 h-px bg-gradient-to-r from-transparent via-cyan-300/70 to-transparent" />
        <div className="mx-auto max-w-7xl">
          <div className="mb-10 flex flex-col justify-between gap-6 md:flex-row md:items-end">
            <motion.div variants={container}>
              <motion.p variants={reveal} className="font-mono text-xs uppercase text-cyan-200">
                technical archive
              </motion.p>
              <motion.h2 variants={reveal} className="mt-4 font-display text-5xl font-semibold sm:text-7xl">
                技术文章
              </motion.h2>
            </motion.div>
            <motion.p variants={reveal} className="max-w-xl text-base leading-8 text-white/58">
              以「前端」为主线，同时延展到后端基础和 AI Agent。这里展示我的技术内容入口、实践路径和能力边界。
            </motion.p>
          </div>

          <motion.div variants={container} className="grid gap-4 lg:grid-cols-3">
            {noteModules.map((module) => {
              const Icon = module.icon;

              return (
                <motion.div key={module.name} variants={scaleReveal}>
                  <Link
                    href={module.href}
                    className="group flex min-h-80 rounded-[1.75rem] border border-white/10 bg-gradient-to-b from-white/[0.08] to-white/[0.025] p-6 shadow-2xl shadow-black/30 outline-none backdrop-blur-xl transition hover:-translate-y-1 hover:border-cyan-300/45 hover:shadow-cyan-500/15 focus-visible:ring-2 focus-visible:ring-cyan-300"
                  >
                    <div className="flex h-full flex-col justify-between">
                      <div>
                        <span className="inline-flex h-12 w-12 items-center justify-center rounded-2xl bg-cyan-300/12 text-cyan-100 ring-1 ring-cyan-300/25">
                          <Icon className="h-6 w-6" />
                        </span>
                        <h3 className="mt-8 text-4xl font-black">{module.name}</h3>
                        <p className="mt-5 text-sm leading-7 text-white/58">{module.description}</p>
                      </div>
                      <span className="mt-10 inline-flex items-center gap-2 text-sm font-semibold text-cyan-100">
                        打开模块
                        <ArrowUpRight className="h-4 w-4 transition group-hover:-translate-y-0.5 group-hover:translate-x-0.5" />
                      </span>
                    </div>
                  </Link>
                </motion.div>
              );
            })}
          </motion.div>
        </div>
      </motion.section>

      <motion.section
        id="life"
        initial="hidden"
        whileInView="show"
        viewport={{ once: true, amount: 0.18 }}
        variants={container}
        className="px-5 py-24 sm:px-8 lg:px-10"
      >
        <motion.div
          variants={scaleReveal}
          className="mx-auto overflow-hidden rounded-[2rem] border border-white/10 bg-[radial-gradient(circle_at_18%_12%,rgba(217,70,239,0.28),transparent_32%),radial-gradient(circle_at_78%_80%,rgba(34,211,238,0.24),transparent_30%),rgba(255,255,255,0.05)] p-6 shadow-2xl shadow-fuchsia-500/10 backdrop-blur-xl sm:p-10 lg:max-w-7xl"
        >
          <div className="grid gap-10 lg:grid-cols-[0.8fr_1.2fr] lg:items-end">
            <motion.div variants={container}>
              <motion.p variants={reveal} className="font-mono text-xs uppercase text-lime-200">
                life channel
              </motion.p>
              <motion.h2 variants={reveal} className="mt-4 font-display text-5xl font-semibold sm:text-7xl">
                日常分享
              </motion.h2>
              <motion.p variants={reveal} className="mt-6 max-w-xl text-base leading-8 text-white/66">
                记录真实经历里的小路标：工具使用、阶段复盘、生活观察，以及一些值得回头看的想法。
              </motion.p>
            </motion.div>
            <motion.div variants={container} className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
              {lifeTopics.map((topic) => (
                <motion.div key={topic} variants={scaleReveal}>
                  <Link
                    href="/life"
                    className="block rounded-2xl border border-white/12 bg-black/20 px-5 py-5 text-sm font-bold text-white/82 shadow-lg shadow-black/10 outline-none transition hover:-translate-y-0.5 hover:border-cyan-200/55 hover:bg-cyan-300/[0.08] hover:text-cyan-50 hover:shadow-[0_0_28px_rgba(34,211,238,0.18)] focus-visible:ring-2 focus-visible:ring-cyan-300"
                  >
                    {topic}
                  </Link>
                </motion.div>
              ))}
            </motion.div>
          </div>
        </motion.div>
      </motion.section>

      <motion.footer
        initial="hidden"
        whileInView="show"
        viewport={{ once: true, amount: 0.4 }}
        variants={container}
        className="px-5 py-10 text-white/46 sm:px-8 lg:px-10"
      >
        <motion.div
          variants={reveal}
          className="mx-auto flex max-w-7xl flex-col justify-between gap-4 border-t border-white/10 pt-6 text-sm md:flex-row"
        >
          <p>© 2026 fly. Built with Next.js, Tailwind CSS and Motion.</p>
          <a
            href="http://124.221.147.173:3000/fly/fly-personal-site"
            className="inline-flex items-center gap-2 text-cyan-100 outline-none transition hover:text-fuchsia-200 focus-visible:ring-2 focus-visible:ring-cyan-300"
          >
            Gitea repository
            <ArrowUpRight className="h-4 w-4" />
          </a>
        </motion.div>
      </motion.footer>
    </main>
  );
}
