import { Head, Link, router } from '@inertiajs/react';
import { Building2, Pencil, Plus, Trash2 } from 'lucide-react';
import { ConfirmDialog } from '@/components/confirm-dialog';
import { EmptyState } from '@/components/empty-state';
import { PageHeader } from '@/components/page-header';
import { StatusBadge } from '@/components/status-badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import { create, destroy, edit } from '@/routes/offices';

type Office = {
    id: string;
    name: string;
    address: string;
    city: string | null;
    state: string | null;
    phone: string | null;
    email: string | null;
    is_head_office: boolean;
    is_active: boolean;
};

function Kpi({ label, value }: { label: string; value: string | number }) {
    return (
        <Card>
            <CardContent className="py-4">
                <p className="text-xs tracking-wide text-muted-foreground uppercase">
                    {label}
                </p>
                <p className="mt-1 text-2xl font-semibold text-foreground">
                    {value}
                </p>
            </CardContent>
        </Card>
    );
}

export default function OfficesIndex({ offices }: { offices: Office[] }) {
    const active = offices.filter((o) => o.is_active).length;
    const headOffice = offices.find((o) => o.is_head_office);

    return (
        <>
            <Head title="Offices" />
            <div className="flex flex-col gap-6 p-4">
                <PageHeader
                    title="Offices"
                    subtitle="Manage the firm's office locations."
                    action={
                        <Button asChild>
                            <Link href={create()}>
                                <Plus /> Add office
                            </Link>
                        </Button>
                    }
                />

                <div className="grid gap-4 sm:grid-cols-3">
                    <Kpi label="Total offices" value={offices.length} />
                    <Kpi label="Active" value={active} />
                    <Kpi label="Head office" value={headOffice?.name ?? '—'} />
                </div>

                <Card>
                    <CardContent className="p-0">
                        {offices.length === 0 ? (
                            <EmptyState
                                icon={Building2}
                                title="No offices yet"
                                description="Add your firm's first office to get started."
                                action={
                                    <Button asChild>
                                        <Link href={create()}>
                                            <Plus /> Add office
                                        </Link>
                                    </Button>
                                }
                            />
                        ) : (
                            <Table>
                                <TableHeader>
                                    <TableRow>
                                        <TableHead>Name</TableHead>
                                        <TableHead>Location</TableHead>
                                        <TableHead>Phone</TableHead>
                                        <TableHead>Status</TableHead>
                                        <TableHead className="text-right">
                                            Actions
                                        </TableHead>
                                    </TableRow>
                                </TableHeader>
                                <TableBody>
                                    {offices.map((office) => (
                                        <TableRow key={office.id}>
                                            <TableCell className="font-medium text-foreground">
                                                <span className="flex items-center gap-2">
                                                    {office.name}
                                                    {office.is_head_office && (
                                                        <StatusBadge tone="info">
                                                            Head office
                                                        </StatusBadge>
                                                    )}
                                                </span>
                                            </TableCell>
                                            <TableCell>
                                                {[office.city, office.state]
                                                    .filter(Boolean)
                                                    .join(', ') || '—'}
                                            </TableCell>
                                            <TableCell>
                                                {office.phone ?? '—'}
                                            </TableCell>
                                            <TableCell>
                                                <StatusBadge
                                                    tone={
                                                        office.is_active
                                                            ? 'success'
                                                            : 'neutral'
                                                    }
                                                >
                                                    {office.is_active
                                                        ? 'Active'
                                                        : 'Inactive'}
                                                </StatusBadge>
                                            </TableCell>
                                            <TableCell className="text-right">
                                                <div className="flex justify-end gap-1">
                                                    <Button
                                                        asChild
                                                        variant="ghost"
                                                        size="icon-sm"
                                                    >
                                                        <Link
                                                            href={edit(
                                                                office.id,
                                                            )}
                                                        >
                                                            <Pencil />
                                                        </Link>
                                                    </Button>
                                                    <ConfirmDialog
                                                        title={`Delete ${office.name}?`}
                                                        description="This office will be removed. This cannot be undone."
                                                        confirmLabel="Delete office"
                                                        onConfirm={() =>
                                                            router.delete(
                                                                destroy(
                                                                    office.id,
                                                                ).url,
                                                            )
                                                        }
                                                        trigger={
                                                            <Button
                                                                variant="ghost"
                                                                size="icon-sm"
                                                            >
                                                                <Trash2 />
                                                            </Button>
                                                        }
                                                    />
                                                </div>
                                            </TableCell>
                                        </TableRow>
                                    ))}
                                </TableBody>
                            </Table>
                        )}
                    </CardContent>
                </Card>
            </div>
        </>
    );
}
