import { Head, Link, usePage } from '@inertiajs/react';
import AppLayout from '@/layouts/app-layout';
import { type BreadcrumbItem } from '@/types';

const breadcrumbsBase: BreadcrumbItem[] = [
    { title: 'Entries', href: '/entries' }
];

interface Entry { id: number;[k: string]: unknown }

const GROUPS: { title: string; fields: string[] }[] = [
    { title: 'Meta & Status', fields: ['form_name', 'slug', 'origin_site', 'seven_days_from', 'last_page_filled', 'approved', 'approved_at', 'death_cert_received', 'pm_med_records_received', 'created_at', 'updated_at'] },
    { title: 'Respondent Details', fields: ['title', 'title_other', 'first_name', 'last_name', 'address_1', 'address_2', 'city', 'county', 'country', 'postcode', 'telephone', 'email', 'relationship_type', 'relationship_if_family', 'relationship_if_friend', 'relationship_if_professional'] },
    { title: 'Consent', fields: ['consent', 'agree_study_publications', 'agree_further_research', 'agree_use_of_info', 'agree_use_of_info_part2', 'allowed_to_contact', 'preferred_contact', 'wants_more_info_on_helping', 'receive_enews', 'allowed_to_contact_med_team'] },
    { title: 'Medical Notice', fields: ['medical_notice_details', 'medical_notice_permission'] },
    { title: 'Deceased Details', fields: ['deceased_title', 'deceased_first_name', 'deceased_last_name', 'deceased_known_as', 'deceased_address_1', 'deceased_address_2', 'deceased_city', 'deceased_county', 'deceased_country', 'deceased_postcode', 'age_at_death', 'exact_age_at_death', 'deceased_gender', 'deceased_gender_other', 'date_of_birth', 'date_of_death'] },
    { title: 'Death Circumstances', fields: ['knows_where_death_occurred', 'where_death_occurred', 'coroner_or_fiscal', 'gp', 'knows_certified_cause', 'type_of_death_certificate', 'cause_1a', 'cause_1b', 'cause_1c', 'cause_2', 'other_type_certificate', 'no_certificate_but_told_cause', 'agree_to_contact_for_copy'] },
    { title: 'Demographics', fields: ['language', 'nationality', 'ethnicity', 'ethnicity_other', 'occupation', 'occupation_other'] },
    { title: 'Other', fields: ['other_comments'] },
];

export default function ShowEntry() {
    const { entry } = usePage<{ entry: Entry }>().props;
    const formName = String(entry.form_name ?? 'Entry');
    const breadcrumbs = [...breadcrumbsBase, { title: formName, href: `/entries/${entry.id}` }];
    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={formName} />
            <div className="space-y-6 max-w-3xl">
                <div className="flex items-center justify-between">
                    <h1 className="text-2xl font-semibold">{formName}</h1>
                    <Link href={route('entries.edit', entry.id)} className="text-sm underline">Edit</Link>
                </div>
                <div className="space-y-10">
                    {GROUPS.map(group => {
                        const visible = group.fields.filter(f => entry[f] !== undefined && entry[f] !== null && entry[f] !== '');
                        if (visible.length === 0) return null;
                        return (
                            <div key={group.title} className="space-y-4">
                                <h2 className="text-lg font-semibold">{group.title}</h2>
                                <div className="grid gap-2 md:grid-cols-2">
                                    {visible.map(f => (
                                        <div key={f} className="text-sm">
                                            <span className="font-medium capitalize">{f.replace(/_/g, ' ')}: </span>
                                            <span>{typeof entry[f] === 'boolean' ? (entry[f] ? 'Yes' : 'No') : String(entry[f])}</span>
                                        </div>
                                    ))}
                                </div>
                            </div>
                        );
                    })}
                </div>
            </div>
        </AppLayout>
    );
}
