import { Link, usePage } from "@inertiajs/react";
import { Facebook, Instagram, Linkedin, Twitter } from "lucide-react";
import type { LucideIcon } from "lucide-react";
import LandingLogo from "@/components/landing/landing-logo";
import { cn } from "@/lib/utils";
import {
    about,
    complaints,
    contact,
    deleteAccount,
    home,
    privacyPolicy,
    register,
    termsOfUse,
} from "@/routes";

type FooterLink = { label: string; href: string };

type SocialLink = { label: string; icon: LucideIcon; href: string | null };

/** Add the real profile URLs here once the accounts are confirmed. */
const socialLinks: SocialLink[] = [
    { label: "Twitter", icon: Twitter, href: null },
    { label: "Facebook", icon: Facebook, href: null },
    { label: "Instagram", icon: Instagram, href: null },
    { label: "LinkedIn", icon: Linkedin, href: null },
];

function footerColumns(isOnHomePage: boolean): {
    heading: string;
    links: FooterLink[];
}[] {
    const sectionHref = (anchor: string): string =>
        isOnHomePage ? `#${anchor}` : `${home.url()}#${anchor}`;

    return [
        {
            heading: "Products",
            links: [
                { label: "Savings plans", href: sectionHref("savings") },
                {
                    label: "Transparent financing",
                    href: sectionHref("financing"),
                },
                {
                    label: "Ethical investments",
                    href: sectionHref("investments"),
                },
                { label: "Open an account", href: register.url() },
            ],
        },
        {
            heading: "Company",
            links: [
                { label: "About us", href: about.url() },
                { label: "FAQ", href: sectionHref("faq") },
                { label: "Contact", href: contact.url() },
            ],
        },
        {
            heading: "Legal",
            links: [
                { label: "Privacy policy", href: privacyPolicy.url() },
                { label: "Terms of use", href: termsOfUse.url() },
                { label: "Complaints", href: complaints.url() },
                { label: "Delete account", href: deleteAccount.url() },
            ],
        },
    ];
}

const bottomBarLinks: FooterLink[] = [
    { label: "Privacy", href: privacyPolicy.url() },
    { label: "Terms", href: termsOfUse.url() },
];

function FooterTextLink({
    link,
    className,
}: {
    link: FooterLink;
    className?: string;
}) {
    const isInPageAnchor = link.href.includes("#");
    const linkClassName = cn(
        "text-white/60 transition-colors hover:text-highlight",
        className,
    );

    return isInPageAnchor ? (
        <a href={link.href} className={linkClassName}>
            {link.label}
        </a>
    ) : (
        <Link href={link.href} className={linkClassName}>
            {link.label}
        </Link>
    );
}

export default function LandingFooter() {
    const { props, component } = usePage();
    const { name } = props;
    const columns = footerColumns(component === "welcome");

    return (
        <footer className="bg-brand-dark px-5 pt-16 pb-8 sm:px-6 lg:px-10 lg:pt-20">
            <div className="mx-auto w-full max-w-6xl">
                <div className="mb-12 grid grid-cols-1 gap-8 sm:grid-cols-2 lg:mb-14 lg:grid-cols-[2fr_1fr_1fr_1fr] lg:gap-12">
                    <div>
                        <LandingLogo tone="light" className="w-fit" />
                        <p className="mt-5 max-w-70 text-sm leading-[1.75] text-white/55">
                            A transparent savings and finance cooperative
                            serving members across Nigeria.
                        </p>
                        <ul
                            aria-label="Social media links"
                            className="mt-5 flex gap-2"
                        >
                            {socialLinks.map(({ label, icon: Icon, href }) => {
                                const iconClassName =
                                    "flex size-9 items-center justify-center rounded-full border border-white/10 text-white/55";

                                return (
                                    <li key={label}>
                                        {href === null ? (
                                            <span
                                                title={`${label} — coming soon`}
                                                className={iconClassName}
                                            >
                                                <Icon
                                                    className="size-3.5"
                                                    aria-hidden="true"
                                                />
                                                <span className="sr-only">
                                                    {label} (coming soon)
                                                </span>
                                            </span>
                                        ) : (
                                            <a
                                                href={href}
                                                target="_blank"
                                                rel="noopener noreferrer"
                                                aria-label={label}
                                                className={cn(
                                                    iconClassName,
                                                    "transition-colors hover:border-highlight hover:text-highlight",
                                                )}
                                            >
                                                <Icon
                                                    className="size-3.5"
                                                    aria-hidden="true"
                                                />
                                            </a>
                                        )}
                                    </li>
                                );
                            })}
                        </ul>
                    </div>

                    {columns.map((column) => (
                        <nav key={column.heading} aria-label={column.heading}>
                            <h2 className="mb-5 text-xs font-semibold tracking-[0.14em] text-white/45 uppercase">
                                {column.heading}
                            </h2>
                            <ul className="flex flex-col gap-3 text-sm">
                                {column.links.map((link) => (
                                    <li key={link.label}>
                                        <FooterTextLink link={link} />
                                    </li>
                                ))}
                            </ul>
                        </nav>
                    ))}
                </div>

                <div className="flex flex-col items-center gap-3 border-t border-white/10 pt-7 text-center text-[13px] text-white/45 sm:flex-row sm:justify-between sm:text-left">
                    <span>
                        © {new Date().getFullYear()} {name}. All rights
                        reserved.
                    </span>
                    <ul className="flex flex-wrap justify-center gap-5">
                        {bottomBarLinks.map((link) => (
                            <li key={link.label}>
                                <FooterTextLink link={link} />
                            </li>
                        ))}
                    </ul>
                </div>
            </div>
        </footer>
    );
}
