import type { Metadata } from "next";
import Image from "next/image";
import Link from "next/link";
import { ArrowRight, Mail, MessageSquare, Phone } from "lucide-react";
import Navbar from "@/components/Navbar";
import Breadcrumbs from "@/components/Breadcrumbs";
import Footer from "@/components/Footer";
import ScrollAnimations from "@/components/ScrollAnimations";
import DealerLocator from "@/components/dealers/DealerLocator";
import { dealers, provinceOf } from "@/lib/dealers";
import { cn } from "@/lib/utils";

const description =
    "Find your nearest iRoof authorized dealer. Browse our island-wide distributor network on the map, search by town or district, and get directions.";

export const metadata: Metadata = {
    title: "Distributor Network",
    description,
    alternates: { canonical: "/dealer-network" },
    openGraph: {
        title: "Distributor Network | iRoof",
        description,
        url: "https://iroof.lk/dealer-network",
    },
};

const districtCount = new Set(dealers.map((d) => d.district).filter(Boolean)).size;
const provinceCount = new Set(dealers.map((d) => provinceOf(d.district)).filter(Boolean)).size;

const stats = [
    { value: dealers.length, label: "Authorized dealers" },
    { value: districtCount, label: "Districts covered" },
    { value: provinceCount, label: "Provinces" },
];

const contactActions = [
    { href: "tel:+94762200200", label: "Call us", value: "076 220 0200", Icon: Phone, primary: true, external: true },
    { href: "mailto:info@iroof.lk", label: "Email", value: "info@iroof.lk", Icon: Mail, primary: false, external: true },
    { href: "/contact", label: "Send an enquiry", value: "Contact form", Icon: MessageSquare, primary: false, external: false },
];

export default function DealerNetworkPage() {
    return (
        <main className="min-h-screen bg-white text-brand-dark">
            <ScrollAnimations />
            <Navbar />

            {/* Hero */}
            <section className="relative pt-24 pb-8 md:pt-40 md:pb-16 w-full flex items-center justify-center overflow-hidden mb-16 md:mb-20">
                <Image src="/images/pexels9.webp" alt="" fill sizes="100vw" className="object-cover" priority />
                <div className="absolute inset-0 bg-brand-dark/70" />
                <div className="relative z-10 text-center px-6">
                    <p className="reveal-up text-[10px] md:text-[11px] font-semibold uppercase tracking-[0.4em] text-white/60 mt-6">Island-wide network</p>
                    <h1 className="reveal-up text-4xl md:text-6xl lg:text-7xl font-medium text-white tracking-tight mt-4">Distributor Network</h1>
                </div>
            </section>

            <div className="max-w-[1400px] mx-auto px-6 md:px-16 lg:px-24 xl:px-32">
                <Breadcrumbs items={[{ label: "Distributor Network" }]} className="mb-10 md:mb-12" />

                {/* Intro + stats */}
                <div className="reveal-up flex flex-col xl:flex-row xl:items-end justify-between gap-10 mb-12 md:mb-14">
                    <div className="max-w-2xl">
                        <div className="flex items-center gap-4 mb-4">
                            <div className="h-[1.5px] w-8 md:w-12 bg-brand-accent" />
                            <span className="text-[10px] md:text-[11px] font-semibold text-brand-accent uppercase tracking-[0.3em] md:tracking-[0.4em]">Find a dealer</span>
                        </div>
                        <h2 className="text-3xl md:text-5xl font-medium tracking-tighter leading-[1.05]">
                            iRoof, near you <br className="hidden md:block" />
                            <span className="text-brand-accent">across Sri Lanka.</span>
                        </h2>
                        <p className="mt-5 text-sm md:text-base text-brand-dark/55 leading-relaxed">
                            Buy genuine iRoof sheets and accessories from an authorized dealer. Search by town or district,
                            or use <strong className="font-semibold text-brand-dark">Near me</strong> to see the closest dealers first.
                        </p>
                    </div>
                    <dl className="grid grid-cols-3 gap-px bg-brand-dark/10 border border-brand-dark/10 self-start xl:self-auto">
                        {stats.map((s) => (
                            <div key={s.label} className="bg-white px-5 py-4 md:px-8 md:py-6 text-center">
                                <dd className="text-2xl md:text-4xl font-semibold tracking-tight text-brand-dark">{s.value}</dd>
                                <dt className="mt-1 text-[9px] md:text-[10px] font-semibold uppercase tracking-[0.15em] text-brand-dark/45">{s.label}</dt>
                            </div>
                        ))}
                    </dl>
                </div>

                <DealerLocator />

                {/* Help / become a dealer */}
                <section className="reveal-up mt-20 md:mt-28 mb-24 bg-brand-dark text-white grid grid-cols-1 lg:grid-cols-12">
                    <div className="lg:col-span-7 px-6 py-12 md:px-14 md:py-16">
                        <p className="text-[10px] md:text-[11px] font-semibold uppercase tracking-[0.35em] text-brand-accent">Need help?</p>
                        <h2 className="mt-4 text-2xl md:text-4xl font-medium tracking-tight leading-tight max-w-xl">
                            Can&apos;t find a dealer near you, or want to become one?
                        </h2>
                        <p className="mt-4 text-sm text-white/60 leading-relaxed max-w-lg">
                            Talk to our central team for stock availability, product advice, or to join the iRoof authorized distributor network.
                        </p>
                    </div>
                    <ul className="lg:col-span-5 border-t lg:border-t-0 lg:border-l border-white/10 divide-y divide-white/10 flex flex-col justify-center">
                        {contactActions.map(({ href, label, value, Icon, primary, external }) => {
                            const content = (
                                <>
                                    <span
                                        className={cn(
                                            "flex items-center justify-center w-11 h-11 shrink-0 transition-colors",
                                            primary ? "bg-brand-accent" : "border border-white/20 group-hover:border-brand-accent group-hover:bg-brand-accent"
                                        )}
                                    >
                                        <Icon className="w-[18px] h-[18px]" aria-hidden="true" />
                                    </span>
                                    <span className="min-w-0 flex-1">
                                        <span className="block text-[10px] font-semibold uppercase tracking-[0.25em] text-white/45">{label}</span>
                                        <span className="block mt-1 text-base md:text-lg font-semibold tracking-tight whitespace-nowrap truncate">{value}</span>
                                    </span>
                                    <ArrowRight className="w-4 h-4 shrink-0 text-white/40 transition-transform group-hover:translate-x-1 group-hover:text-white" aria-hidden="true" />
                                </>
                            );
                            const className = "group flex items-center gap-4 px-6 py-5 md:px-10 md:py-6 hover:bg-white/[0.04] transition-colors";
                            return (
                                <li key={label}>
                                    {external ? (
                                        <a href={href} className={className}>{content}</a>
                                    ) : (
                                        <Link href={href} className={className}>{content}</Link>
                                    )}
                                </li>
                            );
                        })}
                    </ul>
                </section>
            </div>

            <Footer />
        </main>
    );
}
