import { Head, Link } from '@inertiajs/react';
import { Search } from 'lucide-react';
import { useMemo, useState } from 'react';
import PortalLayout from '@/layouts/portal-layout';

interface Member {
    id: number;
    name: string;
    title: string | null;
    profession: string | null;
    membership_number: string | null;
    slug: string | null;
    institution_id: number | null;
    institution: {
        name: string;
        country: { name: string } | null;
    } | null;
}

function initials(name: string) {
    return name.split(' ').map((w) => w[0]).slice(0, 2).join('').toUpperCase();
}

export default function Directory({ members }: { members: Member[] }) {
    const [q, setQ] = useState('');

    const filtered = useMemo(() => {
        const s = q.toLowerCase();
        return members.filter((m) =>
            m.name.toLowerCase().includes(s) ||
            m.profession?.toLowerCase().includes(s) ||
            m.institution?.name.toLowerCase().includes(s) ||
            m.institution?.country?.name.toLowerCase().includes(s)
        );
    }, [q, members]);

    return (
        <>
            <Head title="Member Directory" />
            <div className="space-y-6">
                <div>
                    <h1 className="font-serif text-2xl font-bold text-[#0A2647]">Member Directory</h1>
                    <p className="mt-1 text-sm text-muted-foreground">{members.length} individual members across Africa</p>
                </div>

                <div className="relative max-w-sm">
                    <Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" />
                    <input
                        type="text"
                        value={q}
                        onChange={(e) => setQ(e.target.value)}
                        placeholder="Search by name, profession, institution…"
                        className="w-full rounded-lg border border-border bg-white pl-9 pr-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-[#0A2647]/20"
                    />
                </div>

                {filtered.length === 0 ? (
                    <p className="text-sm text-muted-foreground py-10 text-center">No members match your search.</p>
                ) : (
                    <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">Member</th>
                                    <th className="px-6 py-3 text-left text-xs font-semibold text-[#4A6585] uppercase tracking-wider hidden sm:table-cell">Profession</th>
                                    <th className="px-6 py-3 text-left text-xs font-semibold text-[#4A6585] uppercase tracking-wider hidden lg:table-cell">Institution</th>
                                    <th className="px-6 py-3 text-left text-xs font-semibold text-[#4A6585] uppercase tracking-wider hidden lg:table-cell">Country</th>
                                </tr>
                            </thead>
                            <tbody className="divide-y divide-border">
                                {filtered.map((m) => (
                                    <tr key={m.id} className="hover:bg-[#F9FBFD]">
                                        <td className="px-6 py-4">
                                            <div className="flex items-center gap-3">
                                                <div className="w-8 h-8 rounded-full bg-[#1B6B7D] flex items-center justify-center text-white text-xs font-bold flex-shrink-0">
                                                    {initials(m.name)}
                                                </div>
                                                <div>
                                                    {m.slug ? (
                                                        <Link href={`/members/${m.slug}`} className="font-medium text-[#0A2647] hover:text-[#df2548] transition-colors">
                                                            {[m.title, m.name].filter(Boolean).join(' ')}
                                                        </Link>
                                                    ) : (
                                                        <span className="font-medium text-[#0A2647]">{[m.title, m.name].filter(Boolean).join(' ')}</span>
                                                    )}
                                                </div>
                                            </div>
                                        </td>
                                        <td className="px-6 py-4 text-muted-foreground hidden sm:table-cell">{m.profession ?? '—'}</td>
                                        <td className="px-6 py-4 text-muted-foreground hidden lg:table-cell">{m.institution?.name ?? '—'}</td>
                                        <td className="px-6 py-4 text-muted-foreground hidden lg:table-cell">{m.institution?.country?.name ?? '—'}</td>
                                    </tr>
                                ))}
                            </tbody>
                        </table>
                    </div>
                )}
            </div>
        </>
    );
}

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