import { ViewScene } from '@/site/components/motion/scene';
import { ButtonLink } from '@/site/components/ui/button';
import {
    ArrowRight,
    Clock,
    Mail,
    MapPin,
    Phone,
    SocialIcon,
} from '@/site/components/ui/icons';
import { SiteLink } from '@/site/components/ui/link';
import { useShared } from '@/site/hooks/use-shared';
import { useT } from '@/site/i18n';
import { LanguageSwitcher } from './language-switcher';
import { Logo } from './logo';

const listTitle = 'mb-5 text-eyebrow font-semibold text-soft uppercase';

/**
 * Health-tourism accreditation: the HealthTürkiye mark and a link to the
 * Ministry of Health authorisation certificate (PDF). Both come from files
 * under public/ (config/site.php → accreditation) and are shown only while
 * the files exist.
 */
function Accreditation() {
    const { site } = useShared();
    const t = useT();
    const { logo, certificateUrl } = site.accreditation;

    if (!logo && !certificateUrl) {
        return null;
    }

    return (
        <div className="mt-10 flex max-w-sm items-center gap-5">
            {logo ? (
                // The mark is drawn for a light background: give it one.
                <img
                    src={logo.src}
                    alt={t.footer.healthTurkiye}
                    width={logo.width}
                    height={logo.height}
                    loading="lazy"
                    decoding="async"
                    className="bg-paper size-24 shrink-0 rounded-lg object-contain p-1.5"
                />
            ) : null}
            {certificateUrl ? (
                <a
                    href={certificateUrl}
                    target="_blank"
                    rel="noopener"
                    className="group text-fg/80 hover:text-fg text-[0.9375rem] leading-snug transition-colors duration-300"
                >
                    {t.footer.certificate}
                    <span className="text-accent-fg mt-1.5 flex items-center gap-1.5 font-medium">
                        <span aria-hidden="true">—</span>
                        <span className="link-underline">
                            {t.footer.certificateView}
                        </span>
                        <ArrowRight className="ease-out-expo size-4 transition-transform duration-500 group-hover:translate-x-1" />
                        <span className="sr-only">
                            (PDF, {t.common.newTab})
                        </span>
                    </span>
                </a>
            ) : null}
        </div>
    );
}
const listLink =
    'link-underline text-[0.9375rem] text-fg/80 transition-colors duration-300 hover:text-fg';

/**
 * Site footer: brand, links to every area of expertise, company and legal
 * pages, and contact details. Contact details that are not set yet are not
 * shown at all — nothing is invented (docs/DECISIONS.md K11).
 */
export function SiteFooter() {
    const { site, nav } = useShared();
    const t = useT();
    const { contact } = site;
    const hasContact = Boolean(
        contact.phone || contact.email || contact.address,
    );

    return (
        <footer className="stage-dark border-line border-t">
            <div className="shell grid gap-14 py-16 md:py-24 lg:grid-cols-12 lg:gap-10">
                <div className="lg:col-span-4">
                    <Logo />
                    <p className="text-soft mt-6 max-w-xs">{site.tagline}</p>

                    {site.social.length > 0 ? (
                        <ul className="mt-8 flex gap-3">
                            {site.social.map((profile) => (
                                <li key={profile.network}>
                                    <a
                                        href={profile.url}
                                        target="_blank"
                                        rel="noopener noreferrer me"
                                        aria-label={`${t.footer.social(profile.network)} (${t.common.newTab})`}
                                        className="border-line-strong text-fg/80 hover:border-fg hover:text-fg grid size-11 place-items-center rounded-full border transition-colors duration-300"
                                    >
                                        <SocialIcon
                                            network={profile.network}
                                            className="size-5"
                                        />
                                    </a>
                                </li>
                            ))}
                        </ul>
                    ) : null}

                    <Accreditation />
                </div>

                <nav
                    aria-label={t.common.footerNav}
                    className="grid gap-12 sm:grid-cols-[1.5fr_1fr_1fr] lg:col-span-5"
                >
                    <div className="sm:col-span-1">
                        <h2 className={listTitle}>{t.footer.treatments}</h2>
                        <ul className="space-y-3">
                            {nav.categories.map((category) => (
                                <li key={category.key}>
                                    <SiteLink
                                        href={category.url}
                                        className={listLink}
                                    >
                                        {category.name}
                                    </SiteLink>
                                </li>
                            ))}
                        </ul>
                    </div>
                    <div>
                        <h2 className={listTitle}>{t.footer.company}</h2>
                        <ul className="space-y-3">
                            {[
                                nav.pages.about,
                                nav.pages.treatments,
                                nav.pages.blog,
                                nav.pages.faq,
                                nav.pages.contact,
                            ].map((link) =>
                                link ? (
                                    <li key={link.url}>
                                        <SiteLink
                                            href={link.url}
                                            className={listLink}
                                        >
                                            {link.label}
                                        </SiteLink>
                                    </li>
                                ) : null,
                            )}
                        </ul>
                    </div>
                    {nav.legal.length > 0 ? (
                        <div>
                            <h2 className={listTitle}>{t.footer.legal}</h2>
                            <ul className="space-y-3">
                                {nav.legal.map((page) => (
                                    <li key={page.key}>
                                        <SiteLink
                                            href={page.url}
                                            className={listLink}
                                        >
                                            {page.label}
                                        </SiteLink>
                                    </li>
                                ))}
                            </ul>
                        </div>
                    ) : null}
                </nav>

                <div className="lg:col-span-3">
                    <h2 className={listTitle}>{t.footer.contact}</h2>
                    {hasContact ? (
                        <address className="text-fg/80 space-y-4 text-[0.9375rem] not-italic">
                            {contact.phone && contact.phoneHref ? (
                                <a
                                    href={contact.phoneHref}
                                    className="hover:text-fg flex gap-3"
                                >
                                    <Phone className="text-accent-fg mt-0.5 size-5 shrink-0" />
                                    {contact.phone}
                                </a>
                            ) : null}
                            {contact.email ? (
                                <a
                                    href={`mailto:${contact.email}`}
                                    className="hover:text-fg flex gap-3"
                                >
                                    <Mail className="text-accent-fg mt-0.5 size-5 shrink-0" />
                                    {contact.email}
                                </a>
                            ) : null}
                            {contact.address ? (
                                <p className="flex gap-3">
                                    <MapPin className="text-accent-fg mt-0.5 size-5 shrink-0" />
                                    <span className="whitespace-pre-line">
                                        {contact.address}
                                    </span>
                                </p>
                            ) : null}
                            {contact.hours ? (
                                <p className="flex gap-3">
                                    <Clock className="text-accent-fg mt-0.5 size-5 shrink-0" />
                                    <span className="whitespace-pre-line">
                                        {contact.hours}
                                    </span>
                                </p>
                            ) : null}
                        </address>
                    ) : (
                        <p className="text-soft max-w-xs text-[0.9375rem]">
                            {t.footer.contactPending}
                        </p>
                    )}
                    <ButtonLink
                        href={nav.pages.contact.url}
                        variant="outline"
                        className="mt-7"
                    >
                        {t.footer.contactCta}
                    </ButtonLink>
                </div>
            </div>

            {/* The name once more, oversized and in outline: it slides past as the page ends. */}
            <ViewScene
                aria-hidden={true}
                end={0.5}
                smooth={90}
                className="overflow-clip"
            >
                <p className="footer-word font-display">{site.wordmark}</p>
            </ViewScene>

            <div className="border-line border-t">
                <div className="shell text-soft flex flex-col gap-6 py-8 text-sm">
                    <p className="max-w-3xl">{t.footer.disclaimer}</p>
                    <div className="flex flex-wrap items-center justify-between gap-4">
                        <p>
                            {t.footer.rights(site.year, site.name)}
                            {site.legal.company
                                ? ` · ${site.legal.company}`
                                : ''}
                        </p>
                        <LanguageSwitcher />
                    </div>
                </div>
            </div>
        </footer>
    );
}
