import { Link } from "@inertiajs/react";
import { route } from "ziggy-js";
import { Seo } from "@/Components/Seo";
import { ServicesProjectsGallery } from "@/Components/ServicesProjectsGallery";
import { PromoShowcase } from "@/Components/PromoShowcase";
import { ArticleCard } from "@/Components/ArticleCard";
import { CoverageCheckSection } from "@/Components/CoverageCheckSection";
import { ServiceTargetsSection } from "@/Components/ServiceTargetsSection";
import { assetUrl } from "@/utils/assets";
import { BlogCardGrid } from "@/Components/ui/BlogCardGrid";
import { SectionHeading } from "@/Components/SectionHeading";
import { Button } from "@/Components/ui/button";
import { useGsapReveal, useHeroAnimation } from "@/hooks/useGsap";
import { withFrontendLayout } from "@/utils/layout";
import type { ArticleItem, CompanySettings, ProjectItem, PromoItem, ServiceItem } from "@/types";

interface HomeProps {
    services: ServiceItem[];
    promos: PromoItem[];
    articles: ArticleItem[];
    projects?: ProjectItem[];
    coverageServices?: string[];
    settings?: CompanySettings;
}

function Home({ services, promos, articles, projects = [], coverageServices = [], settings }: HomeProps) {
    useHeroAnimation();
    useGsapReveal();

    const heroBg = assetUrl(settings?.hero_bg_image || "images/hero-bg.jpg");
    const heroTitle = settings?.hero_title || "Teknologi Andal";
    const heroSubtitle = settings?.hero_subtitle || "untuk Bisnis yang Bertumbuh.";
    const heroHeadline = [heroTitle, heroSubtitle].filter(Boolean).join(" ");
    const heroCtaText = settings?.hero_cta_text || "Konsultasi Sekarang";
    const heroCtaUrl = settings?.hero_cta_url || route("contact");
    const heroSecondaryCtaText = settings?.hero_secondary_cta_text || "Lihat Layanan";
    const heroSecondaryCtaUrl = settings?.hero_secondary_cta_url || "#services";

    // Visibilitas Section dari Pengaturan Admin
    const showCoverage = settings?.show_coverage_section !== "0";
    const showServices = settings?.show_services_section !== "0";
    const showSla = settings?.show_targets_section !== "0";
    const showPromos = promos.length > 0 && settings?.show_promos_section !== "0";
    const showArticles = articles.length > 0 && settings?.show_articles_section !== "0";
    const showProjects = projects.length > 0;
    const showCta = settings?.show_cta_section !== "0";

    return (
        <>
            {/* Judul & deskripsi memakai metadata SEO default dari Admin Panel */}
            <Seo title={null} />

            {/* 1. HERO: identitas perusahaan di atas foto teknologi, rata kiri
                agar terbaca sebagai halaman profil perusahaan, bukan banner
                kampanye. Headline dan dua aksi tetap menjadi satu fokus. */}
            <section className="relative flex min-h-[100svh] items-center overflow-hidden bg-navy">
                <img
                    data-hero="bg"
                    src={heroBg}
                    alt=""
                    aria-hidden="true"
                    className="absolute inset-0 h-full w-full object-cover object-center"
                />
                {/* Scrim navy: teks di kiri tetap terbaca, detail ruang server
                    di kanan tetap terlihat pada layar lebar. */}
                <div className="absolute inset-0 bg-navy/75 lg:bg-gradient-to-r lg:from-navy/95 lg:via-navy/80 lg:to-navy/45" />
                <div className="absolute inset-x-0 bottom-0 h-32 bg-gradient-to-t from-deep-navy/80 to-transparent" />

                <div className="container-nds relative z-10 w-full pt-28 pb-24 sm:pt-32 lg:pt-36 lg:pb-32">
                    <div className="max-w-3xl">
                        <h1 data-hero="title" className="hero-title text-balance text-white opacity-0">
                            {heroHeadline}
                        </h1>

                        <div
                            data-hero="actions"
                            className="mt-9 flex w-full max-w-md flex-col gap-3 sm:flex-row sm:gap-4"
                        >
                            <Button
                                asChild
                                variant="primaryOnDark"
                                size="lg"
                                className="border-white bg-white font-semibold text-navy hover:border-light-gray hover:bg-light-gray active:bg-white"
                            >
                                {heroCtaUrl.startsWith("http") ? (
                                    <a href={heroCtaUrl} target="_blank" rel="noopener noreferrer">
                                        <span>{heroCtaText}</span>
                                    </a>
                                ) : heroCtaUrl.startsWith("#") ? (
                                    <a href={heroCtaUrl}>
                                        <span>{heroCtaText}</span>
                                    </a>
                                ) : (
                                    <Link href={heroCtaUrl}>
                                        <span>{heroCtaText}</span>
                                    </Link>
                                )}
                            </Button>
                            <Button asChild variant="outlineLight" size="lg" className="font-semibold">
                                {heroSecondaryCtaUrl.startsWith("http") ? (
                                    <a href={heroSecondaryCtaUrl} target="_blank" rel="noopener noreferrer">
                                        <span>{heroSecondaryCtaText}</span>
                                    </a>
                                ) : heroSecondaryCtaUrl.startsWith("#") ? (
                                    <a href={heroSecondaryCtaUrl}>
                                        <span>{heroSecondaryCtaText}</span>
                                    </a>
                                ) : (
                                    <Link href={heroSecondaryCtaUrl}>
                                        <span>{heroSecondaryCtaText}</span>
                                    </Link>
                                )}
                            </Button>
                        </div>
                    </div>
                </div>
            </section>

            {/* 2. LAYANAN & REKAM JEJAK PROYEK: satu galeri horizontal kontinu
                yang menyatukan layanan (baris atas) dan proyek (baris bawah). */}
            {(showServices || showProjects) && (
                <ServicesProjectsGallery
                    services={showServices ? services : []}
                    projects={showProjects ? projects : []}
                />
            )}

            {/* 3. SLA KECEPATAN: bukti layanan dalam angka, tepat setelah galeri */}
            {showSla && <ServiceTargetsSection />}

            {/* 4. CEK JANGKAUAN LAYANAN NDS */}
            {showCoverage && <CoverageCheckSection settings={settings} coverageServices={coverageServices} />}

            {/* 5. PROMO PILIHAN: satu penawaran utama berukuran besar dengan panel
                navy, lalu maksimal dua penawaran pendukung berbentuk baris ringkas.
                Komposisi ini khusus Beranda dan berbeda dari katalog /promo. */}
            {showPromos && <PromoShowcase promos={promos} />}

            {/* 6. BLOG & PUBLIKASI */}
            {showArticles && (
                <section className="bg-light-gray pt-12 pb-20 lg:pt-14 lg:pb-28">
                    <div className="container-nds">
                        <div className="flex flex-col items-start justify-between gap-5 sm:flex-row sm:items-end">
                            <SectionHeading title="Artikel Teknologi" />
                            <Button asChild variant="secondary" className="gsap-reveal shrink-0">
                                <Link href={route("blog")}>
                                    <span>Lihat Semua Artikel</span>
                                </Link>
                            </Button>
                        </div>

                        {/* Static Grid: Blog articles on homepage without carousel */}
                        <div className="mt-10">
                            <BlogCardGrid
                                items={articles}
                                getItemKey={(article) => article.id}
                                columns={3}
                                gap={24}
                                renderItem={(article) => (
                                    <ArticleCard article={article} isActive={false} />
                                )}
                            />
                        </div>
                    </div>
                </section>
            )}

            {/* 7. CTA PENUTUP: satu ajakan, rata kiri, tanpa dekorasi */}
            {showCta && (
                <section className="bg-deep-navy py-16 lg:py-24">
                    <div className="container-nds">
                        <div className="grid gap-8 lg:grid-cols-12 lg:items-end lg:gap-10">
                            <div className="lg:col-span-8">
                                <h2 className="gsap-reveal heading-2 text-balance text-white">
                                    Siap Membangun Solusi Digital Terintegrasi?
                                </h2>
                                <p className="gsap-reveal mt-4 max-w-2xl text-base leading-relaxed text-white/80 sm:text-lg">
                                    Ceritakan kebutuhan infrastruktur, aplikasi, atau keamanan jaringan Anda.
                                </p>
                            </div>

                            <div className="gsap-reveal lg:col-span-4 lg:justify-self-end">
                                <Button
                                    asChild
                                    variant="primaryOnDark"
                                    size="lg"
                                    className="w-full border-white bg-white px-8 font-semibold text-navy hover:border-light-gray hover:bg-light-gray active:bg-white sm:w-auto"
                                >
                                    <Link href={route("contact")}>
                                        <span>Konsultasi Sekarang</span>
                                    </Link>
                                </Button>
                            </div>
                        </div>
                    </div>
                </section>
            )}
        </>
    );
}

export default withFrontendLayout(Home);
