import { Head, router, usePage } from '@inertiajs/react';
import { useState } from 'react';
import { Mail, Trash2, RefreshCw, UserPlus } from 'lucide-react';

type Member = { id: number; name: string; email: string; role: string; joined_at: string };
type Invite = { id: number; email: string; role: string; expired: boolean; expires_at: string };

export default function OrganizationShow() {
    const props = usePage().props as unknown as {
        organization: { id: number; name: string };
        members: Member[]; invitations: Invite[]; roles: string[];
    };
    const { organization, members, invitations, roles } = props;
    const [email, setEmail] = useState('');
    const [role, setRole] = useState('viewer');

    const invite = () => {
        if (!email.trim()) return;
        router.post(`/admin/organizations/${organization.id}/invitations`, { email, org_role: role },
            { preserveScroll: true, onSuccess: () => setEmail('') });
    };

    return (
        <>
            <Head title={organization.name} />
            <div className="mx-auto w-full max-w-4xl px-6 py-8">
                <h1 className="mb-6 text-2xl font-semibold tracking-tight text-foreground">{organization.name}</h1>

                <section className="mb-8 rounded-xl border border-border bg-card p-5">
                    <h2 className="mb-3 flex items-center gap-2 font-medium text-foreground"><UserPlus className="size-4 text-primary" /> Invite someone</h2>
                    <div className="flex gap-2">
                        <input value={email} onChange={(e) => setEmail(e.target.value)} type="email" placeholder="email@example.com"
                            className="flex-1 rounded-md border border-border bg-background px-3 py-2 text-sm" />
                        <select value={role} onChange={(e) => setRole(e.target.value)}
                            className="rounded-md border border-border bg-background px-3 py-2 text-sm capitalize">
                            {roles.map((r) => <option key={r} value={r}>{r}</option>)}
                        </select>
                        <button onClick={invite} disabled={!email.trim()}
                            className="inline-flex items-center gap-1.5 rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground hover:opacity-90 disabled:opacity-50">
                            <Mail className="size-4" /> Send
                        </button>
                    </div>
                </section>

                <section className="mb-8">
                    <h2 className="mb-3 font-medium text-foreground">Members ({members.length})</h2>
                    <div className="overflow-hidden rounded-xl border border-border">
                        <table className="w-full text-sm">
                            <thead className="bg-muted/50 text-left text-xs text-muted-foreground">
                                <tr><th className="px-4 py-2.5">Name</th><th className="px-4 py-2.5">Email</th><th className="px-4 py-2.5">Role</th></tr>
                            </thead>
                            <tbody className="divide-y divide-border">
                                {members.map((m) => (
                                    <tr key={m.id}>
                                        <td className="px-4 py-3 font-medium text-foreground">{m.name}</td>
                                        <td className="px-4 py-3 text-muted-foreground">{m.email}</td>
                                        <td className="px-4 py-3 capitalize">{m.role}</td>
                                    </tr>
                                ))}
                            </tbody>
                        </table>
                    </div>
                </section>

                {invitations.length > 0 && (
                    <section>
                        <h2 className="mb-3 font-medium text-foreground">Pending invitations</h2>
                        <div className="space-y-2">
                            {invitations.map((i) => (
                                <div key={i.id} className="flex items-center justify-between rounded-lg border border-border bg-card p-3">
                                    <div>
                                        <span className="text-sm font-medium text-foreground">{i.email}</span>
                                        <span className="ml-2 text-xs capitalize text-muted-foreground">{i.role}</span>
                                        {i.expired && <span className="ml-2 text-xs text-destructive">expired</span>}
                                    </div>
                                    <div className="flex gap-1">
                                        <button title="Resend" onClick={() => router.post(`/admin/organizations/${organization.id}/invitations/${i.id}/resend`, {}, { preserveScroll: true })}
                                            className="rounded p-1.5 text-muted-foreground hover:bg-muted"><RefreshCw className="size-4" /></button>
                                        <button title="Revoke" onClick={() => router.delete(`/admin/organizations/${organization.id}/invitations/${i.id}`, { preserveScroll: true })}
                                            className="rounded p-1.5 text-muted-foreground hover:text-destructive"><Trash2 className="size-4" /></button>
                                    </div>
                                </div>
                            ))}
                        </div>
                    </section>
                )}
            </div>
        </>
    );
}
