import { Head, Link } from '@inertiajs/react';
import { Building2, Link2, Pencil, User } from 'lucide-react';
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, CardHeader, CardTitle } from '@/components/ui/card';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import { edit, index } from '@/routes/contacts';

type Contact = {
    id: string;
    kind: string;
    kind_label: string;
    name: string;
    email: string | null;
    phone: string | null;
    address: string | null;
    city: string | null;
    state: string | null;
    category: string | null;
    nin_masked: string | null;
    rc_number: string | null;
    notes: string | null;
    is_client: boolean;
};

type AppearsIn = { context: string; label: string; role: string };

function Field({ label, value }: { label: string; value: string | null }) {
    return (
        <div>
            <p className="text-xs tracking-wide text-muted-foreground uppercase">
                {label}
            </p>
            <p className="mt-0.5 text-sm text-foreground">{value || '—'}</p>
        </div>
    );
}

export default function ContactShow({
    contact,
    appearsIn,
}: {
    contact: Contact;
    appearsIn: AppearsIn[];
}) {
    return (
        <>
            <Head title={contact.name} />
            <div className="flex flex-col gap-6 p-4">
                <PageHeader
                    title={contact.name}
                    subtitle={contact.kind_label}
                    action={
                        <div className="flex gap-2">
                            <Button asChild variant="outline">
                                <Link href={index()}>Back</Link>
                            </Button>
                            <Button asChild>
                                <Link href={edit(contact.id)}>
                                    <Pencil /> Edit
                                </Link>
                            </Button>
                        </div>
                    }
                />

                <div className="grid gap-6 lg:grid-cols-3">
                    <Card className="lg:col-span-2">
                        <CardHeader>
                            <CardTitle className="flex items-center gap-2">
                                {contact.kind === 'organization' ? (
                                    <Building2 className="size-4 text-muted-foreground" />
                                ) : (
                                    <User className="size-4 text-muted-foreground" />
                                )}
                                Contact card
                                {contact.is_client && (
                                    <StatusBadge tone="info">
                                        Client
                                    </StatusBadge>
                                )}
                            </CardTitle>
                        </CardHeader>
                        <CardContent className="grid gap-4 sm:grid-cols-2">
                            <Field label="Email" value={contact.email} />
                            <Field label="Phone" value={contact.phone} />
                            <Field label="Address" value={contact.address} />
                            <Field
                                label="Location"
                                value={
                                    [contact.city, contact.state]
                                        .filter(Boolean)
                                        .join(', ') || null
                                }
                            />
                            <Field label="Category" value={contact.category} />
                            {contact.kind === 'organization' ? (
                                <Field
                                    label="RC number"
                                    value={contact.rc_number}
                                />
                            ) : (
                                <Field label="NIN" value={contact.nin_masked} />
                            )}
                            <div className="sm:col-span-2">
                                <Field label="Notes" value={contact.notes} />
                            </div>
                        </CardContent>
                    </Card>

                    <Card>
                        <CardHeader>
                            <CardTitle className="flex items-center gap-2">
                                <Link2 className="size-4 text-muted-foreground" />{' '}
                                Appears in
                            </CardTitle>
                        </CardHeader>
                        <CardContent className="p-0">
                            {appearsIn.length === 0 ? (
                                <EmptyState
                                    title="Not referenced yet"
                                    description="This contact isn't linked to any client or matter."
                                />
                            ) : (
                                <Table>
                                    <TableHeader>
                                        <TableRow>
                                            <TableHead>Context</TableHead>
                                            <TableHead>Reference</TableHead>
                                            <TableHead>Role</TableHead>
                                        </TableRow>
                                    </TableHeader>
                                    <TableBody>
                                        {appearsIn.map((row, i) => (
                                            <TableRow key={i}>
                                                <TableCell className="text-muted-foreground">
                                                    {row.context}
                                                </TableCell>
                                                <TableCell className="font-medium text-foreground">
                                                    {row.label}
                                                </TableCell>
                                                <TableCell>
                                                    {row.role}
                                                </TableCell>
                                            </TableRow>
                                        ))}
                                    </TableBody>
                                </Table>
                            )}
                        </CardContent>
                    </Card>
                </div>
            </div>
        </>
    );
}
