import { Link, usePage } from '@inertiajs/react';
import { useEffect, useRef, useState, type ReactNode } from 'react';
import AboutController from '@/actions/App/Http/Controllers/AboutController';
import { index as careersIndex } from '@/actions/App/Http/Controllers/CareerController';
import CatalogDownloadController from '@/actions/App/Http/Controllers/CatalogDownloadController';
import CertificateController from '@/actions/App/Http/Controllers/CertificateController';
import { index as contactIndex } from '@/actions/App/Http/Controllers/ContactController';
import { index as factoriesIndex } from '@/actions/App/Http/Controllers/FactoryController';
import GalleryController from '@/actions/App/Http/Controllers/GalleryController';
import HomeController from '@/actions/App/Http/Controllers/HomeController';
import { index as newsIndex } from '@/actions/App/Http/Controllers/NewsController';
import { index as productsIndex } from '@/actions/App/Http/Controllers/ProductController';
import { index as projectsIndex } from '@/actions/App/Http/Controllers/ProjectController';
import {
    ArrowUpIcon,
    CheckCircleIcon,
    CheckIcon,
    ChevronDownIcon,
    CloseIcon,
    CopyIcon,
    DownloadIcon,
    InstagramIcon,
    MailIcon,
    MapPinIcon,
    MenuIcon,
    PhoneIcon,
    ShieldCheckIcon,
    TelegramIcon,
    WhatsAppIcon,
} from '@/components/Icons';
import type { SharedPageProps } from '@/types/binder';

interface MainLayoutProps {
    children: ReactNode;
}

export default function MainLayout({ children }: MainLayoutProps) {
    const { props, url } = usePage<SharedPageProps>();
    const { siteSettings, flash } = props;
    const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
    const [companyMenuOpen, setCompanyMenuOpen] = useState(false);
    const [dismissedFlashKey, setDismissedFlashKey] = useState<string | null>(
        null,
    );
    const [copiedCode, setCopiedCode] = useState(false);
    const [showScrollTop, setShowScrollTop] = useState(false);
    const companyDropdownRef = useRef<HTMLDivElement | null>(null);

    const primaryNavItems = [
        { label: 'صفحه اصلی', href: HomeController.url(), exact: true },
        { label: 'پروژه‌ها', href: projectsIndex.url() },
        { label: 'کارخانجات', href: factoriesIndex.url() },
        { label: 'محصولات B2B', href: productsIndex.url() },
        { label: 'اخبار', href: newsIndex.url() },
        { label: 'تماس با ما', href: contactIndex.url() },
    ];

    const companyNavItems = [
        { label: 'درباره ما', href: AboutController.url() },
        { label: 'گواهینامه‌ها و رتبه‌بندی', href: CertificateController.url() },
        { label: 'گالری تصاویر و ویدیو', href: GalleryController.url() },
        { label: 'فرصت‌های شغلی و استخدام', href: careersIndex.url() },
    ];

    const isActive = (href: string, exact = false): boolean => {
        if (exact) {
            return url === '/' || url === '';
        }

        return url.startsWith(href);
    };

    const isCompanyActive = companyNavItems.some((item) => isActive(item.href));

    const flashKey = flash?.success
        ? `${flash.success}::${flash.tracking_code ?? ''}`
        : null;
    const showFlash = Boolean(flashKey && flashKey !== dismissedFlashKey);

    const sanitizedPhone = siteSettings.management_phone
        ? siteSettings.management_phone.replace(/[^0-9+]/g, '')
        : null;
    const whatsappUrl = siteSettings.social_links?.whatsapp || null;
    const quoteHref = `${productsIndex.url()}#quote-form`;

    useEffect(() => {
        setMobileMenuOpen(false);
        setCompanyMenuOpen(false);
    }, [url]);

    useEffect(() => {
        document.body.style.overflow = mobileMenuOpen ? 'hidden' : '';

        return () => {
            document.body.style.overflow = '';
        };
    }, [mobileMenuOpen]);

    useEffect(() => {
        const handleKeyDown = (event: KeyboardEvent) => {
            if (event.key === 'Escape') {
                setMobileMenuOpen(false);
                setCompanyMenuOpen(false);
            }
        };

        const handleClickOutside = (event: MouseEvent) => {
            if (
                companyDropdownRef.current &&
                !companyDropdownRef.current.contains(event.target as Node)
            ) {
                setCompanyMenuOpen(false);
            }
        };

        document.addEventListener('keydown', handleKeyDown);
        document.addEventListener('mousedown', handleClickOutside);

        return () => {
            document.removeEventListener('keydown', handleKeyDown);
            document.removeEventListener('mousedown', handleClickOutside);
        };
    }, []);

    useEffect(() => {
        const handleScroll = () => {
            setShowScrollTop(window.scrollY > 400);
        };

        handleScroll();
        window.addEventListener('scroll', handleScroll, { passive: true });
        return () => window.removeEventListener('scroll', handleScroll);
    }, []);

    const handleCopyTrackingCode = async (code: string) => {
        try {
            await navigator.clipboard.writeText(code);
            setCopiedCode(true);
            window.setTimeout(() => setCopiedCode(false), 2000);
        } catch {
            // Clipboard API may be unavailable on non-secure origins
        }
    };

    return (
        <div className="flex min-h-screen flex-col overflow-x-clip bg-slate-50/40 text-slate-900">
            {/* Top Corporate Info Bar */}
            <div className="border-b border-slate-200 bg-white text-xs text-slate-600">
                <div className="mx-auto flex max-w-7xl flex-wrap items-center justify-between gap-3 px-4 py-2.5 sm:px-6 lg:px-8">
                    <div className="flex flex-wrap items-center gap-4">
                        <span className="inline-flex items-center gap-1.5 font-medium text-brand-green">
                            <ShieldCheckIcon className="h-4 w-4" />
                            <span>
                                پیمانکار پایه ۱ راه و ترابری | تأسیس{' '}
                                {siteSettings.established_year}
                            </span>
                        </span>
                        {siteSettings.management_phone && sanitizedPhone && (
                            <a
                                href={`tel:${sanitizedPhone}`}
                                className="hidden items-center gap-1 hover:text-brand-blue sm:inline-flex"
                                dir="ltr"
                            >
                                <span>{siteSettings.management_phone}</span>
                                <PhoneIcon className="h-3.5 w-3.5 text-brand-blue" />
                            </a>
                        )}
                    </div>
                </div>
            </div>

            {/* Sticky Main Navigation */}
            <header className="sticky top-0 z-40 border-b border-slate-200/90 bg-white/95 backdrop-blur-md">
                <div className="mx-auto flex max-w-7xl items-center justify-between gap-4 px-4 py-3.5 sm:px-6 lg:px-8">
                    <div className="flex shrink-0 items-center gap-3">
                        <button
                            type="button"
                            onClick={() => setMobileMenuOpen((prev) => !prev)}
                            aria-expanded={mobileMenuOpen}
                            aria-controls="mobile-nav-drawer"
                            className="inline-flex h-10 w-10 shrink-0 items-center justify-center rounded-xl border border-slate-200 bg-slate-50 text-slate-700 transition hover:bg-slate-100 lg:hidden"
                            aria-label="منو"
                        >
                            {mobileMenuOpen ? (
                                <CloseIcon className="h-5 w-5" />
                            ) : (
                                <MenuIcon className="h-5 w-5" />
                            )}
                        </button>

                        <Link
                            href={HomeController.url()}
                            className="flex shrink-0 items-center gap-3"
                        >
                            <img
                                src={siteSettings.logo_url}
                                alt={siteSettings.company_name}
                                width={44}
                                height={44}
                                className="h-11 w-11 rounded-lg object-contain"
                            />
                            <div className="leading-tight">
                                <div className="text-base font-extrabold whitespace-nowrap text-slate-900 sm:text-lg">
                                    {siteSettings.company_name}
                                </div>
                                <div className="text-[11px] font-medium whitespace-nowrap text-slate-500">
                                    ({siteSettings.legal_type}) — Binder Rah Co.
                                </div>
                            </div>
                        </Link>
                    </div>

                    <nav
                        aria-label="منوی اصلی"
                        className="hidden items-center gap-1 lg:flex"
                    >
                        {primaryNavItems.slice(0, 4).map((item) => {
                            const active = isActive(item.href, item.exact);

                            return (
                                <Link
                                    key={item.href}
                                    href={item.href}
                                    prefetch
                                    className={`rounded-xl px-3 py-2 text-[13px] font-medium whitespace-nowrap transition ${
                                        active
                                            ? 'bg-blue-50 font-semibold text-brand-blue'
                                            : 'text-slate-700 hover:bg-slate-100 hover:text-slate-950'
                                    }`}
                                >
                                    {item.label}
                                </Link>
                            );
                        })}

                        {/* Company Dropdown */}
                        <div
                            ref={companyDropdownRef}
                            className="relative"
                            onMouseEnter={() => setCompanyMenuOpen(true)}
                            onMouseLeave={() => setCompanyMenuOpen(false)}
                        >
                            <button
                                type="button"
                                onClick={() =>
                                    setCompanyMenuOpen((prev) => !prev)
                                }
                                aria-expanded={companyMenuOpen}
                                aria-haspopup="menu"
                                className={`inline-flex items-center gap-1 rounded-xl px-3 py-2 text-[13px] font-medium whitespace-nowrap transition ${
                                    isCompanyActive || companyMenuOpen
                                        ? 'bg-blue-50 font-semibold text-brand-blue'
                                        : 'text-slate-700 hover:bg-slate-100 hover:text-slate-950'
                                }`}
                            >
                                <span>شرکت</span>
                                <ChevronDownIcon
                                    className={`h-3.5 w-3.5 transition-transform duration-150 ${
                                        companyMenuOpen ? 'rotate-180' : ''
                                    }`}
                                />
                            </button>

                            {companyMenuOpen && (
                                <div className="absolute top-full right-0 pt-2">
                                    <div
                                        role="menu"
                                        className="w-56 rounded-2xl border border-slate-200 bg-white p-1.5 shadow-lg"
                                    >
                                        {companyNavItems.map((item) => {
                                            const active = isActive(item.href);

                                            return (
                                                <Link
                                                    key={item.href}
                                                    href={item.href}
                                                    prefetch
                                                    role="menuitem"
                                                    onClick={() =>
                                                        setCompanyMenuOpen(
                                                            false,
                                                        )
                                                    }
                                                    className={`block rounded-xl px-3.5 py-2.5 text-[13px] font-medium transition ${
                                                        active
                                                            ? 'bg-blue-50 font-semibold text-brand-blue'
                                                            : 'text-slate-700 hover:bg-slate-50 hover:text-slate-950'
                                                    }`}
                                                >
                                                    {item.label}
                                                </Link>
                                            );
                                        })}
                                    </div>
                                </div>
                            )}
                        </div>

                        {primaryNavItems.slice(4).map((item) => {
                            const active = isActive(item.href, item.exact);

                            return (
                                <Link
                                    key={item.href}
                                    href={item.href}
                                    prefetch
                                    className={`rounded-xl px-3 py-2 text-[13px] font-medium whitespace-nowrap transition ${
                                        active
                                            ? 'bg-blue-50 font-semibold text-brand-blue'
                                            : 'text-slate-700 hover:bg-slate-100 hover:text-slate-950'
                                    }`}
                                >
                                    {item.label}
                                </Link>
                            );
                        })}
                    </nav>

                    <div className="flex shrink-0 items-center gap-2.5">
                        <Link
                            href={quoteHref}
                            className="hidden rounded-xl bg-brand-orange px-4 py-2.5 text-xs font-bold whitespace-nowrap text-white shadow-xs transition hover:bg-orange-700 sm:inline-flex"
                        >
                            درخواست استعلام قیمت
                        </Link>
                    </div>
                </div>
            </header>

            {/* Mobile Sidebar Drawer */}
            {mobileMenuOpen && (
                <div className="fixed inset-0 z-50 lg:hidden">
                    <div
                        className="fixed inset-0 bg-slate-900/40 backdrop-blur-[1px]"
                        onClick={() => setMobileMenuOpen(false)}
                        aria-hidden="true"
                    />
                    <aside
                        id="mobile-nav-drawer"
                        aria-label="منوی موبایل"
                        className="fixed inset-y-0 right-0 flex w-80 max-w-[82vw] flex-col overflow-y-auto border-l border-slate-200 bg-white shadow-2xl"
                    >
                        <div className="flex items-center justify-between border-b border-slate-200 px-5 py-4">
                            <div className="flex min-w-0 items-center gap-3">
                                <img
                                    src={siteSettings.logo_url}
                                    alt={siteSettings.company_name}
                                    width={40}
                                    height={40}
                                    className="h-10 w-10 shrink-0 rounded-xl border border-slate-100 bg-slate-50 object-contain p-1"
                                />
                                <div className="min-w-0">
                                    <div className="truncate text-sm font-extrabold text-slate-900">
                                        {siteSettings.company_name}
                                    </div>
                                    <div className="truncate text-[11px] font-medium text-brand-green">
                                        پیمانکار پایه ۱ راه و ترابری
                                    </div>
                                </div>
                            </div>
                            <button
                                type="button"
                                onClick={() => setMobileMenuOpen(false)}
                                aria-label="بستن منو"
                                className="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-xl border border-slate-200 bg-slate-50 text-slate-600 transition hover:bg-slate-100 hover:text-slate-900"
                            >
                                <CloseIcon className="h-4.5 w-4.5" />
                            </button>
                        </div>

                        <nav className="flex-1 space-y-5 px-4 py-5">
                            <div>
                                <div className="mb-2 px-3 text-[11px] font-bold tracking-wide text-slate-400">
                                    دسترسی اصلی
                                </div>
                                <div className="space-y-1">
                                    {primaryNavItems.slice(0, 4).map((item) => {
                                        const active = isActive(
                                            item.href,
                                            Boolean(item.exact),
                                        );

                                        return (
                                            <Link
                                                key={item.href}
                                                href={item.href}
                                                onClick={() =>
                                                    setMobileMenuOpen(false)
                                                }
                                                className={`flex items-center justify-between rounded-xl px-3.5 py-2.5 text-[13px] transition ${
                                                    active
                                                        ? 'bg-blue-50 font-bold text-brand-blue'
                                                        : 'font-medium text-slate-700 hover:bg-slate-50 hover:text-slate-950'
                                                }`}
                                            >
                                                <span>{item.label}</span>
                                                {active && (
                                                    <span className="h-1.5 w-1.5 rounded-full bg-brand-blue" />
                                                )}
                                            </Link>
                                        );
                                    })}
                                </div>
                            </div>

                            <div className="border-t border-slate-100 pt-4">
                                <div className="mb-2 px-3 text-[11px] font-bold tracking-wide text-slate-400">
                                    شرکت و ارتباطات
                                </div>
                                <div className="space-y-1">
                                    {[
                                        ...companyNavItems,
                                        ...primaryNavItems.slice(4),
                                    ].map((item) => {
                                        const active = isActive(item.href);

                                        return (
                                            <Link
                                                key={item.href}
                                                href={item.href}
                                                onClick={() =>
                                                    setMobileMenuOpen(false)
                                                }
                                                className={`flex items-center justify-between rounded-xl px-3.5 py-2.5 text-[13px] transition ${
                                                    active
                                                        ? 'bg-blue-50 font-bold text-brand-blue'
                                                        : 'font-medium text-slate-700 hover:bg-slate-50 hover:text-slate-950'
                                                }`}
                                            >
                                                <span>{item.label}</span>
                                                {active && (
                                                    <span className="h-1.5 w-1.5 rounded-full bg-brand-blue" />
                                                )}
                                            </Link>
                                        );
                                    })}
                                </div>
                            </div>
                        </nav>

                        <div className="space-y-2.5 border-t border-slate-200 bg-slate-50/70 p-4">
                            <Link
                                href={quoteHref}
                                onClick={() => setMobileMenuOpen(false)}
                                className="inline-flex w-full items-center justify-center rounded-xl bg-brand-orange px-4 py-3 text-xs font-bold text-white shadow-xs transition hover:bg-orange-700"
                            >
                                درخواست استعلام قیمت B2B
                            </Link>
                            {(sanitizedPhone || whatsappUrl) && (
                                <div className="flex items-center gap-2">
                                    {sanitizedPhone && (
                                        <a
                                            href={`tel:${sanitizedPhone}`}
                                            className="inline-flex flex-1 items-center justify-center gap-1.5 rounded-xl border border-slate-200 bg-white px-3 py-2.5 text-xs font-semibold text-slate-700 transition hover:border-brand-blue hover:bg-blue-50 hover:text-brand-blue"
                                        >
                                            <PhoneIcon className="h-4 w-4 text-brand-blue" />
                                            <span>تماس مستقیم</span>
                                        </a>
                                    )}
                                    {whatsappUrl && (
                                        <a
                                            href={whatsappUrl}
                                            target="_blank"
                                            rel="noopener noreferrer"
                                            className="inline-flex flex-1 items-center justify-center gap-1.5 rounded-xl border border-slate-200 bg-white px-3 py-2.5 text-xs font-semibold text-slate-700 transition hover:border-brand-green hover:bg-emerald-50 hover:text-brand-green"
                                        >
                                            <WhatsAppIcon className="h-4 w-4 text-brand-green" />
                                            <span>واتساپ</span>
                                        </a>
                                    )}
                                </div>
                            )}
                        </div>
                    </aside>
                </div>
            )}

            {/* Flash Success Notification */}
            {showFlash && flash?.success && (
                <div
                    role="status"
                    aria-live="polite"
                    className="mx-auto mt-4 w-full max-w-7xl px-4 sm:px-6 lg:px-8"
                >
                    <div className="flex flex-wrap items-center justify-between gap-3 rounded-2xl border border-emerald-200 bg-emerald-50 px-5 py-4 text-sm text-emerald-900 shadow-xs">
                        <div className="flex items-center gap-2.5">
                            <CheckCircleIcon className="h-5 w-5 shrink-0 text-emerald-600" />
                            <span className="font-medium">{flash.success}</span>
                        </div>
                        <div className="flex items-center gap-2">
                            {flash.tracking_code && (
                                <>
                                    <span
                                        className="rounded-lg bg-white px-3 py-1 font-mono text-xs font-bold text-emerald-800 shadow-xs"
                                        dir="ltr"
                                    >
                                        {flash.tracking_code}
                                    </span>
                                    <button
                                        type="button"
                                        onClick={() =>
                                            void handleCopyTrackingCode(
                                                flash.tracking_code!,
                                            )
                                        }
                                        className="inline-flex items-center gap-1 rounded-lg border border-emerald-200 bg-white px-2.5 py-1 text-xs font-semibold text-emerald-800 transition hover:bg-emerald-100"
                                    >
                                        {copiedCode ? (
                                            <>
                                                <CheckIcon className="h-3.5 w-3.5 text-emerald-600" />
                                                <span>کپی شد</span>
                                            </>
                                        ) : (
                                            <>
                                                <CopyIcon className="h-3.5 w-3.5" />
                                                <span>کپی کد پیگیری</span>
                                            </>
                                        )}
                                    </button>
                                </>
                            )}
                            <button
                                type="button"
                                onClick={() => setDismissedFlashKey(flashKey)}
                                aria-label="بستن پیام"
                                className="inline-flex h-7 w-7 items-center justify-center rounded-lg text-emerald-700 transition hover:bg-emerald-100"
                            >
                                <CloseIcon className="h-4 w-4" />
                            </button>
                        </div>
                    </div>
                </div>
            )}

            {/* Main Content */}
            <main className="flex-1">{children}</main>

            {/* Corporate Footer */}
            <footer className="mt-16 border-t border-slate-200 bg-white">
                <div className="mx-auto max-w-7xl px-4 py-12 sm:px-6 lg:px-8">
                    <div className="grid gap-10 md:grid-cols-2 lg:grid-cols-4">
                        <div className="space-y-4">
                            <div className="flex items-center gap-3">
                                <img
                                    src={siteSettings.logo_url}
                                    alt={siteSettings.company_name}
                                    loading="lazy"
                                    decoding="async"
                                    width={48}
                                    height={48}
                                    className="h-12 w-12 object-contain"
                                />
                                <div>
                                    <div className="font-extrabold text-slate-900">
                                        {siteSettings.company_name}
                                    </div>
                                    <div className="text-xs text-slate-500">
                                        ({siteSettings.legal_type}) — تأسیس{' '}
                                        {siteSettings.established_year}
                                    </div>
                                </div>
                            </div>
                            <p className="text-xs leading-6 text-slate-600">
                                {siteSettings.short_about}
                            </p>
                            {siteSettings.has_catalog_pdf && (
                                <a
                                    href={CatalogDownloadController.url()}
                                    className="inline-flex items-center gap-2 rounded-xl border border-slate-200 bg-slate-50 px-4 py-2 text-xs font-semibold text-brand-blue transition hover:border-blue-300 hover:bg-blue-50"
                                >
                                    <DownloadIcon className="h-4 w-4" />
                                    <span>دانلود کاتالوگ معرفی شرکت (PDF)</span>
                                </a>
                            )}
                        </div>

                        <div>
                            <h3 className="mb-4 text-sm font-bold text-slate-900">
                                دسترسی سریع
                            </h3>
                            <ul className="space-y-2.5 text-xs text-slate-600">
                                <li>
                                    <Link
                                        href={projectsIndex.url()}
                                        className="hover:text-brand-blue"
                                    >
                                        پروژه‌ها و سوابق اجرایی (۸۲+ پروژه)
                                    </Link>
                                </li>
                                <li>
                                    <Link
                                        href={factoriesIndex.url()}
                                        className="hover:text-brand-blue"
                                    >
                                        کارخانجات تولیدی و آزمایشگاه فنی
                                    </Link>
                                </li>
                                <li>
                                    <Link
                                        href={productsIndex.url()}
                                        className="hover:text-brand-blue"
                                    >
                                        محصولات صنعتی و استعلام قیمت B2B
                                    </Link>
                                </li>
                                <li>
                                    <Link
                                        href={CertificateController.url()}
                                        className="hover:text-brand-blue"
                                    >
                                        رتبه‌بندی پیمانکاری، ایزوها و عضویت‌ها
                                    </Link>
                                </li>
                                <li>
                                    <Link
                                        href={careersIndex.url()}
                                        className="hover:text-brand-blue"
                                    >
                                        فرصت‌های شغلی و ارسال رزومه
                                    </Link>
                                </li>
                            </ul>
                        </div>

                        <div>
                            <h3 className="mb-4 text-sm font-bold text-slate-900">
                                تلفن واحدهای شرکت
                            </h3>
                            <ul className="space-y-2.5 text-xs text-slate-600">
                                {siteSettings.management_phone && (
                                    <li className="flex items-center justify-between gap-2">
                                        <span>دفتر مدیریت:</span>
                                        <a
                                            href={`tel:${siteSettings.management_phone.replace(/[^0-9+]/g, '')}`}
                                            className="font-semibold text-slate-800 hover:text-brand-blue"
                                            dir="ltr"
                                        >
                                            {siteSettings.management_phone}
                                        </a>
                                    </li>
                                )}
                                {siteSettings.technical_phone && (
                                    <li className="flex items-center justify-between gap-2">
                                        <span>دفتر فنی:</span>
                                        <a
                                            href={`tel:${siteSettings.technical_phone.replace(/[^0-9+]/g, '')}`}
                                            className="font-semibold text-slate-800 hover:text-brand-blue"
                                            dir="ltr"
                                        >
                                            {siteSettings.technical_phone}
                                        </a>
                                    </li>
                                )}
                                {siteSettings.accounting_phone && (
                                    <li className="flex items-center justify-between gap-2">
                                        <span>حسابداری و کارگزینی:</span>
                                        <a
                                            href={`tel:${siteSettings.accounting_phone.replace(/[^0-9+]/g, '')}`}
                                            className="font-semibold text-slate-800 hover:text-brand-blue"
                                            dir="ltr"
                                        >
                                            {siteSettings.accounting_phone}
                                        </a>
                                    </li>
                                )}
                                {siteSettings.fax && (
                                    <li className="flex items-center justify-between gap-2">
                                        <span>نمابر:</span>
                                        <span
                                            className="font-semibold text-slate-800"
                                            dir="ltr"
                                        >
                                            {siteSettings.fax}
                                        </span>
                                    </li>
                                )}
                            </ul>
                        </div>

                        <div>
                            <h3 className="mb-4 text-sm font-bold text-slate-900">
                                نشانی دفتر مرکزی
                            </h3>
                            <div className="space-y-3 text-xs leading-6 text-slate-600">
                                <p className="flex items-start gap-2">
                                    <MapPinIcon className="mt-1 h-4 w-4 shrink-0 text-brand-orange" />
                                    <span>{siteSettings.address}</span>
                                </p>
                                {siteSettings.postal_code && (
                                    <p>
                                        کد پستی:{' '}
                                        <span
                                            className="font-semibold text-slate-800"
                                            dir="ltr"
                                        >
                                            {siteSettings.postal_code}
                                        </span>
                                    </p>
                                )}
                                {siteSettings.email && (
                                    <p className="flex items-center gap-2">
                                        <MailIcon className="h-4 w-4 shrink-0 text-brand-blue" />
                                        <a
                                            href={`mailto:${siteSettings.email}`}
                                            className="font-medium hover:text-brand-blue"
                                            dir="ltr"
                                        >
                                            {siteSettings.email}
                                        </a>
                                    </p>
                                )}
                                <div className="flex items-center gap-2 pt-2">
                                    {whatsappUrl ? (
                                        <a
                                            href={whatsappUrl}
                                            target="_blank"
                                            rel="noopener noreferrer"
                                            aria-label="واتساپ"
                                            title="واتساپ"
                                            className="inline-flex h-9 w-9 items-center justify-center rounded-xl border border-slate-200 bg-slate-50 text-slate-600 transition hover:border-emerald-300 hover:bg-emerald-50 hover:text-emerald-700"
                                        >
                                            <WhatsAppIcon className="h-4 w-4" />
                                        </a>
                                    ) : (
                                        <span
                                            aria-label="واتساپ"
                                            title="واتساپ"
                                            className="inline-flex h-9 w-9 items-center justify-center rounded-xl border border-slate-200/70 bg-slate-50/70 text-slate-400"
                                        >
                                            <WhatsAppIcon className="h-4 w-4" />
                                        </span>
                                    )}
                                    {siteSettings.social_links?.instagram ? (
                                        <a
                                            href={
                                                siteSettings.social_links
                                                    .instagram
                                            }
                                            target="_blank"
                                            rel="noopener noreferrer"
                                            aria-label="اینستاگرام"
                                            title="اینستاگرام"
                                            className="inline-flex h-9 w-9 items-center justify-center rounded-xl border border-slate-200 bg-slate-50 text-slate-600 transition hover:border-rose-300 hover:bg-rose-50 hover:text-rose-600"
                                        >
                                            <InstagramIcon className="h-4 w-4" />
                                        </a>
                                    ) : (
                                        <span
                                            aria-label="اینستاگرام"
                                            title="اینستاگرام"
                                            className="inline-flex h-9 w-9 items-center justify-center rounded-xl border border-slate-200/70 bg-slate-50/70 text-slate-400"
                                        >
                                            <InstagramIcon className="h-4 w-4" />
                                        </span>
                                    )}
                                    {siteSettings.social_links?.telegram ? (
                                        <a
                                            href={
                                                siteSettings.social_links
                                                    .telegram
                                            }
                                            target="_blank"
                                            rel="noopener noreferrer"
                                            aria-label="تلگرام"
                                            title="تلگرام"
                                            className="inline-flex h-9 w-9 items-center justify-center rounded-xl border border-slate-200 bg-slate-50 text-slate-600 transition hover:border-sky-300 hover:bg-sky-50 hover:text-sky-600"
                                        >
                                            <TelegramIcon className="h-4 w-4" />
                                        </a>
                                    ) : (
                                        <span
                                            aria-label="تلگرام"
                                            title="تلگرام"
                                            className="inline-flex h-9 w-9 items-center justify-center rounded-xl border border-slate-200/70 bg-slate-50/70 text-slate-400"
                                        >
                                            <TelegramIcon className="h-4 w-4" />
                                        </span>
                                    )}
                                </div>
                            </div>
                        </div>
                    </div>

                    <div className="mt-10 flex flex-wrap items-center justify-between gap-4 border-t border-slate-200 pt-6 text-xs text-slate-500">
                        <p>
                            © تمامی حقوق مادی و معنوی متعلق به{' '}
                            {siteSettings.company_name} می‌باشد.
                        </p>
                        <div className="flex items-center gap-4">
                            <span className="text-brand-green">
                                ISO 9001 • ISO 14001 • ISO 45001 • IMS
                            </span>
                        </div>
                    </div>
                </div>
            </footer>

            {/* Scroll-to-Top Button */}
            {showScrollTop && (
                <button
                    type="button"
                    onClick={() =>
                        window.scrollTo({ top: 0, behavior: 'smooth' })
                    }
                    aria-label="بازگشت به بالا"
                    title="بازگشت به بالا"
                    className="fixed right-4 bottom-5 z-30 inline-flex h-10 w-10 items-center justify-center rounded-full border border-slate-200 bg-white/95 text-slate-700 shadow-lg backdrop-blur-md transition hover:border-brand-blue hover:text-brand-blue"
                >
                    <ArrowUpIcon className="h-5 w-5" />
                </button>
            )}

            {/* Desktop Floating B2B Quick-Contact Pill */}
            <div className="fixed bottom-5 left-5 z-30 hidden items-center gap-1.5 rounded-2xl border border-slate-200/90 bg-white/95 p-1.5 shadow-lg backdrop-blur-md md:flex">
                <Link
                    href={quoteHref}
                    className="rounded-xl bg-brand-orange px-3.5 py-2 text-xs font-bold text-white transition hover:bg-orange-700"
                >
                    استعلام قیمت
                </Link>
                {sanitizedPhone && (
                    <a
                        href={`tel:${sanitizedPhone}`}
                        title={siteSettings.management_phone ?? 'تماس سریع'}
                        aria-label="تماس سریع"
                        className="inline-flex h-8 w-8 items-center justify-center rounded-xl text-brand-blue transition hover:bg-blue-50"
                    >
                        <PhoneIcon className="h-4 w-4" />
                    </a>
                )}
                {whatsappUrl && (
                    <a
                        href={whatsappUrl}
                        target="_blank"
                        rel="noopener noreferrer"
                        title="واتساپ"
                        aria-label="واتساپ"
                        className="inline-flex h-8 w-8 items-center justify-center rounded-xl text-brand-green transition hover:bg-emerald-50"
                    >
                        <WhatsAppIcon className="h-4 w-4" />
                    </a>
                )}
            </div>
        </div>
    );
}
