import { Head } from '@inertiajs/react';
import {
    Building2,
    Calendar,
    CreditCard,
    Globe,
    Mail,
    MapPin,
    Users,
} from 'lucide-react';

type Org = {
    id: string;
    name: string;
    orgType: string;
    country: string;
    domain: string | null;
    owner: { name: string; email: string } | null;
    memberCount: number;
    plan: string | null;
    subscriptionStatus: string | null;
    trialEndsAt: string | null;
    createdAt: string | null;
};

type Props = { org: Org };

const ORG_TYPE_LABELS: Record<string, string> = {
    sacco: 'SACCO',
    microfinance: 'Microfinance',
    bank: 'Bank',
    insurance: 'Insurance',
    other: 'Other',
};

const STATUS_STYLES: Record<string, string> = {
    trialing: 'bg-amber-500/10 text-amber-600 dark:text-amber-400',
    active: 'bg-emerald-500/10 text-emerald-600 dark:text-emerald-400',
    past_due: 'bg-red-500/10 text-red-600 dark:text-red-400',
    canceled: 'bg-zinc-500/10 text-zinc-600 dark:text-zinc-400',
};

function countryName(code: string): string {
    try {
        return (
            new Intl.DisplayNames(['en'], { type: 'region' }).of(code) ?? code
        );
    } catch {
        return code;
    }
}

function formatDate(iso: string | null): string {
    if (!iso) return '—';
    return new Date(iso).toLocaleDateString('en-GB', {
        day: 'numeric',
        month: 'short',
        year: 'numeric',
    });
}

function DetailRow({
    icon: Icon,
    label,
    children,
}: {
    icon: typeof Globe;
    label: string;
    children: React.ReactNode;
}) {
    return (
        <div className="flex items-start gap-3 py-3">
            <span className="bg-primary/10 mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg">
                <Icon className="text-primary h-4 w-4" />
            </span>
            <div className="min-w-0">
                <dt className="text-muted-foreground text-xs font-medium tracking-wide uppercase">
                    {label}
                </dt>
                <dd className="text-foreground mt-0.5 text-sm font-medium">
                    {children}
                </dd>
            </div>
        </div>
    );
}

export default function Index({ org }: Props) {
    const status = org.subscriptionStatus;

    return (
        <div className="bg-background flex min-h-screen items-center justify-center p-6">
            <Head title={org.name} />

            <div className="w-full max-w-2xl space-y-6">
                <div className="pf-glass p-6 sm:p-8">
                    <div className="mb-6 flex items-center gap-4">
                        <span className="bg-primary/10 flex h-14 w-14 items-center justify-center rounded-2xl">
                            <Building2 className="text-primary h-7 w-7" />
                        </span>
                        <div className="min-w-0">
                            <h1 className="text-foreground truncate text-2xl font-semibold">
                                {org.name}
                            </h1>
                            <p className="text-muted-foreground text-sm">
                                {ORG_TYPE_LABELS[org.orgType] ?? org.orgType} ·{' '}
                                {countryName(org.country)}
                            </p>
                        </div>
                        {status && (
                            <span
                                className={`ml-auto rounded-full px-3 py-1 text-xs font-semibold capitalize ${
                                    STATUS_STYLES[status] ??
                                    'bg-zinc-500/10 text-zinc-600 dark:text-zinc-400'
                                }`}
                            >
                                {status.replace('_', ' ')}
                            </span>
                        )}
                    </div>

                    <dl className="divide-border grid grid-cols-1 gap-x-8 sm:grid-cols-2">
                        <DetailRow icon={Globe} label="Workspace URL">
                            {org.domain ?? 'Not set'}
                        </DetailRow>
                        <DetailRow icon={MapPin} label="Country">
                            {countryName(org.country)}
                        </DetailRow>
                        <DetailRow icon={CreditCard} label="Plan">
                            {org.plan ?? 'No plan selected'}
                            {status === 'trialing' && org.trialEndsAt && (
                                <span className="text-muted-foreground block text-xs font-normal">
                                    Trial ends {formatDate(org.trialEndsAt)}
                                </span>
                            )}
                        </DetailRow>
                        <DetailRow icon={Users} label="Team members">
                            {org.memberCount}
                        </DetailRow>
                        {org.owner && (
                            <DetailRow icon={Mail} label="Owner">
                                {org.owner.name}
                                <span className="text-muted-foreground block text-xs font-normal">
                                    {org.owner.email}
                                </span>
                            </DetailRow>
                        )}
                        <DetailRow icon={Calendar} label="Created">
                            {formatDate(org.createdAt)}
                        </DetailRow>
                    </dl>
                </div>

                <p className="text-muted-foreground text-center text-xs">
                    Staff sign-in and loan modules for this workspace are coming
                    soon.
                </p>
            </div>
        </div>
    );
}
