import type { Metadata } from "next";
import Link from "next/link";
import { HeroMesh } from "@/components/HeroMesh";
import { ServiceIcon } from "@/components/Icons";
import { Unavailable } from "@/components/RichText";
import { ApiDown, api } from "@/lib/api";
import { faNum } from "@/lib/format";
import type { HomeData } from "@/lib/types";

export async function generateMetadata(): Promise<Metadata> {
  try {
    const home = await api<HomeData>("/home");
    return { title: { absolute: home.seo.title }, description: home.seo.description ?? undefined };
  } catch {
    return { title: "گروه تصمیم‌سازان فردا" };
  }
}

export default async function HomePage() {
  let home: HomeData;
  try {
    home = await api<HomeData>("/home");
  } catch (error) {
    if (error instanceof ApiDown) return <Unavailable />;
    throw error;
  }

  return (
    <>
      <section className="hero-stage min-h-[calc(100svh-7.75rem)]">
        <HeroMesh />
        <div className="shell relative z-[1] grid min-h-[calc(100svh-7.75rem)] items-center gap-12 py-16 lg:grid-cols-[1.15fr_0.85fr] lg:gap-8 lg:py-20">
          <div>
            <p className="reveal text-[0.9rem] font-semibold tracking-[0.22em] text-gold">گروه تصمیم‌سازان فردا</p>
            <h1 className="display reveal reveal-delay-1 mt-5 text-[clamp(2.75rem,6vw,5rem)] text-white">
              تصمیم‌سازان فردا
            </h1>
            <div className="rule-gold mt-7 reveal reveal-delay-2" />
            <p className="reveal reveal-delay-2 mt-7 max-w-2xl text-[clamp(1.25rem,2.2vw,1.7rem)] font-semibold leading-[1.85] text-white/95">
              {home.hero_title}
            </p>
            <p className="reveal reveal-delay-3 mt-5 max-w-xl text-[1.08rem] leading-9 text-white/72">
              {home.hero_lead}
            </p>
            <div className="reveal reveal-delay-4 mt-11 flex flex-wrap gap-4">
              <Link href="/services" className="btn btn-primary">آشنایی با خدمات تخصصی <span className="btn-chevron" aria-hidden="true">←</span></Link>
              <Link href="/request" className="btn btn-ghost">درخواست جلسه همکاری</Link>
            </div>
          </div>
          <div className="reveal reveal-delay-3 relative mx-auto flex w-full max-w-md items-center justify-center lg:ms-auto">
            <div className="absolute inset-[-8%] rounded-[2rem] bg-[radial-gradient(circle,rgb(26_165_166/0.28),transparent_68%)] blur-2xl" />
            <div className="absolute inset-[18%] rounded-full border border-white/10" />
            <div className="absolute inset-[8%] rounded-full border border-gold/20" />
            <img src="/logo.png" alt="لوگوی گروه تصمیم‌سازان فردا" className="logo-glow relative w-[min(100%,21rem)]" />
          </div>
        </div>
      </section>

      <section className="section-pad section-band">
        <div className="shell">
          <div className="flex flex-col gap-5 lg:flex-row lg:items-end lg:justify-between">
            <div>
              <p className="eyebrow">جایگاه برند</p>
              <h2 className="display mt-4 text-[clamp(1.9rem,3vw,2.7rem)] text-navy">چرا تصمیم‌سازان فردا؟</h2>
            </div>
            <p className="max-w-lg text-[1.05rem] leading-9 text-muted">
              سه مفهوم اصلی در طراحی و محتوا حفظ می‌شود: تفکر راهبردی، توانمندی فناورانه، ارزش‌آفرینی اقتصادی.
            </p>
          </div>
          <div className="mt-12 grid gap-5 md:grid-cols-3">
            {home.why_items.map((item, index) => (
              <article key={item.title} className="panel p-7 pe-6 ps-8">
                <span className="panel-accent" aria-hidden="true" />
                <p className="text-base font-bold text-gold-deep">{faNum(String(index + 1).padStart(2, "0"))}</p>
                <h3 className="mt-5 text-[1.2rem] font-extrabold leading-9 text-navy">{item.title}</h3>
                <p className="mt-4 text-[1.02rem] leading-9 text-muted">{item.text}</p>
              </article>
            ))}
          </div>
        </div>
      </section>

      <section className="section-pad bg-mist/80">
        <div className="shell">
          <div className="flex flex-wrap items-end justify-between gap-4">
            <div>
              <p className="eyebrow">خدمات</p>
              <h2 className="display mt-4 text-[clamp(1.9rem,3vw,2.7rem)] text-navy">خدمات تخصصی</h2>
            </div>
            <Link href="/services" className="btn btn-outline text-[0.95rem]">همه خدمات <span className="btn-chevron" aria-hidden="true">←</span></Link>
          </div>
          <div className="mt-10 space-y-3">
            {home.services.map((service) => (
              <div key={service.slug} className={`service-tile ${service.is_featured ? "featured" : ""}`}>
                <div className="service-icon"><ServiceIcon name={service.icon} /></div>
                <div>
                  <h3 className="text-[1.2rem] font-extrabold text-navy">
                    <Link href={`/services/${service.slug}`} className="transition hover:text-teal">{service.card_title}</Link>
                  </h3>
                  <p className="mt-3 max-w-3xl text-[1.02rem] leading-9 text-muted">{service.summary}</p>
                  {service.secondary ? (
                    <Link href={`/services/${service.secondary.slug}`} className="mt-3 inline-block text-base font-semibold text-teal">
                      {service.secondary.label}
                    </Link>
                  ) : null}
                </div>
                <Link href={`/services/${service.slug}`} className="hidden self-center text-base font-semibold text-navy md:inline-flex">
                  مشاهده ←
                </Link>
              </div>
            ))}
          </div>
        </div>
      </section>

      <section className="section-pad">
        <div className="shell">
          <p className="eyebrow">مسئله‌محور</p>
          <h2 className="display mt-4 text-[clamp(1.9rem,3vw,2.7rem)] text-navy">راهکار برای مسئله سازمان</h2>
          <div className="panel mt-10 p-3">
            <div className="hidden grid-cols-2 gap-8 rounded-xl bg-navy px-6 py-4 text-[0.9rem] font-semibold tracking-[0.08em] text-gold md:grid">
              <span>مسئله سازمان</span>
              <span>راهکار تصمیم‌سازان فردا</span>
            </div>
            {home.problems.map((row) => (
              <div key={row.problem} className="problem-row">
                <p className="text-[1.08rem] font-bold leading-9 text-navy">{row.problem}</p>
                <p className="text-[1.05rem] leading-9 text-muted">{row.solution}</p>
              </div>
            ))}
          </div>
        </div>
      </section>

      <section className="section-pad bg-mist/80">
        <div className="shell">
          <p className="eyebrow">مسیر همکاری</p>
          <h2 className="display mt-4 text-[clamp(1.9rem,3vw,2.7rem)] text-navy">فرآیند شش‌مرحله‌ای</h2>
          <ol className="timeline mt-14 grid gap-8 md:grid-cols-6 md:gap-5">
            {home.process_steps.map((step, index) => (
              <li key={step.title} className="relative ps-12 md:ps-0 md:pt-12">
                <span className="step-badge absolute start-0 top-0 md:start-0">{faNum(index + 1)}</span>
                <h3 className="text-[1.02rem] font-extrabold leading-8 text-navy">{step.title}</h3>
                <p className="mt-3 text-[0.95rem] leading-8 text-muted">{step.text}</p>
              </li>
            ))}
          </ol>
        </div>
      </section>

      <section className="section-pad">
        <div className="shell grid gap-12 lg:grid-cols-[0.85fr_1.15fr] lg:items-start">
          <div>
            <p className="eyebrow">ارزش همکاری</p>
            <h2 className="display mt-4 text-[clamp(1.9rem,3vw,2.7rem)] text-navy">مزیت‌های همکاری</h2>
            <p className="lead mt-5 max-w-md">
              اعتبار حرفه‌ای بر جلوه‌های گرافیکی اولویت دارد؛ مزیت‌ها باید در نتیجه عملیاتی دیده شوند.
            </p>
            <Link href="/request" className="btn btn-solid mt-8">شروع گفت‌وگوی تخصصی <span className="btn-chevron" aria-hidden="true">←</span></Link>
          </div>
          <ul className="space-y-3">
            {home.advantages.map((item, index) => (
              <li key={item} className="panel flex items-start gap-5 p-5 pe-5 ps-6">
                <span className="panel-accent" aria-hidden="true" />
                <span className="pt-1 text-base font-bold text-gold-deep">{faNum(String(index + 1).padStart(2, "0"))}</span>
                <span className="text-[1.08rem] font-medium leading-9 text-ink">{item}</span>
              </li>
            ))}
          </ul>
        </div>
      </section>

      <section className="hero-stage">
        <HeroMesh />
        <div className="shell relative z-[1] flex flex-col items-start gap-9 py-24 lg:flex-row lg:items-end lg:justify-between">
          <div className="max-w-3xl">
            <p className="text-[0.9rem] font-semibold tracking-[0.2em] text-gold">دعوت به اقدام</p>
            <h2 className="display mt-5 text-[clamp(1.9rem,3.4vw,2.9rem)]">{home.cta_title}</h2>
            <p className="mt-5 max-w-2xl text-[1.1rem] leading-9 text-white/75">{home.cta_body}</p>
          </div>
          <Link href="/request" className="btn btn-primary shrink-0">درخواست ارزیابی اولیه <span className="btn-chevron" aria-hidden="true">←</span></Link>
        </div>
      </section>
    </>
  );
}
