import { Head, Link } from '@inertiajs/react';
import { ArrowRight, CheckCircle, Clock, XCircle } from 'lucide-react';
import PortalLayout from '@/layouts/portal-layout';

interface Tier { id: number; name: string; type: string; duration: string; price_usd: string }
interface Subscription {
    id: number;
    status: string;
    starts_at: string;
    expires_at: string | null;
    certificate_number: string | null;
    tier: Tier;
}

function StatusIcon({ status }: { status: string }) {
    if (status === 'active') return <CheckCircle size={16} className="text-green-600" />;
    if (status === 'expired') return <XCircle size={16} className="text-red-500" />;
    return <Clock size={16} className="text-yellow-500" />;
}

function fmt(date: string) {
    return new Date(date).toLocaleDateString('en-GB', { day: 'numeric', month: 'long', year: 'numeric' });
}

export default function Membership({ subscription, history }: { subscription: Subscription | null; history: Subscription[] }) {
    return (
        <>
            <Head title="My Membership" />
            <div className="space-y-8">
                <div>
                    <h1 className="font-serif text-2xl font-bold text-[#0A2647]">My Membership</h1>
                    <p className="mt-1 text-sm text-muted-foreground">Your current membership status and history</p>
                </div>

                {subscription ? (
                    <div className="rounded-xl border border-border bg-white p-8">
                        <div className="flex items-start justify-between flex-wrap gap-4">
                            <div>
                                <p className="text-xs font-semibold uppercase tracking-widest text-[#1B6B7D] mb-1">Active Membership</p>
                                <h2 className="font-serif text-2xl font-bold text-[#0A2647]">{subscription.tier.name}</h2>
                                <p className="text-sm text-muted-foreground mt-1">{subscription.tier.type} · {subscription.tier.duration}</p>
                            </div>
                            <span className="rounded-full bg-green-100 px-3 py-1 text-xs font-semibold text-green-700">Active</span>
                        </div>

                        <div className="mt-6 grid gap-6 sm:grid-cols-3 border-t border-border pt-6">
                            <div>
                                <p className="text-xs text-muted-foreground mb-1">Member Since</p>
                                <p className="text-sm font-semibold text-[#0A2647]">{fmt(subscription.starts_at)}</p>
                            </div>
                            {subscription.expires_at && (
                                <div>
                                    <p className="text-xs text-muted-foreground mb-1">Expires</p>
                                    <p className="text-sm font-semibold text-[#0A2647]">{fmt(subscription.expires_at)}</p>
                                </div>
                            )}
                            {subscription.certificate_number && (
                                <div>
                                    <p className="text-xs text-muted-foreground mb-1">Certificate No.</p>
                                    <p className="text-sm font-mono font-semibold text-[#0A2647]">{subscription.certificate_number}</p>
                                </div>
                            )}
                        </div>
                    </div>
                ) : (
                    <div className="rounded-xl border border-dashed border-[#C8D8EC] bg-[#EEF2F8] p-10 text-center">
                        <p className="font-serif text-lg font-semibold text-[#0A2647]">No active membership</p>
                        <p className="mt-2 text-sm text-muted-foreground max-w-sm mx-auto">
                            Join ABEC to access the member directory, events, publications, and your membership certificate.
                        </p>
                        <Link
                            href="/join"
                            className="mt-6 inline-flex items-center gap-2 rounded-full bg-[#0A2647] px-6 py-2.5 text-sm font-semibold text-white hover:bg-[#071D35] transition-colors"
                        >
                            Apply for Membership <ArrowRight size={14} />
                        </Link>
                    </div>
                )}

                {history.length > 0 && (
                    <div>
                        <h2 className="font-serif text-lg font-bold text-[#0A2647] mb-4">Membership History</h2>
                        <div className="rounded-xl border border-border bg-white overflow-hidden">
                            <table className="w-full text-sm">
                                <thead className="bg-[#F0F5FB] border-b border-border">
                                    <tr>
                                        <th className="px-6 py-3 text-left text-xs font-semibold text-[#4A6585] uppercase tracking-wider">Tier</th>
                                        <th className="px-6 py-3 text-left text-xs font-semibold text-[#4A6585] uppercase tracking-wider">Start</th>
                                        <th className="px-6 py-3 text-left text-xs font-semibold text-[#4A6585] uppercase tracking-wider">Expires</th>
                                        <th className="px-6 py-3 text-left text-xs font-semibold text-[#4A6585] uppercase tracking-wider">Status</th>
                                    </tr>
                                </thead>
                                <tbody className="divide-y divide-border">
                                    {history.map((s) => (
                                        <tr key={s.id} className="hover:bg-[#F9FBFD]">
                                            <td className="px-6 py-4 font-medium text-[#0A2647]">{s.tier.name}</td>
                                            <td className="px-6 py-4 text-muted-foreground">{fmt(s.starts_at)}</td>
                                            <td className="px-6 py-4 text-muted-foreground">{s.expires_at ? fmt(s.expires_at) : '—'}</td>
                                            <td className="px-6 py-4">
                                                <span className="inline-flex items-center gap-1.5">
                                                    <StatusIcon status={s.status} />
                                                    <span className="capitalize text-xs font-medium">{s.status}</span>
                                                </span>
                                            </td>
                                        </tr>
                                    ))}
                                </tbody>
                            </table>
                        </div>
                    </div>
                )}
            </div>
        </>
    );
}

Membership.layout = (page: React.ReactNode) => <PortalLayout>{page}</PortalLayout>;
