import { useTranslation } from '@/hooks/use-translation';
import { router, Head } from '@inertiajs/react';
import { Calendar, Star, Edit, Trash2 } from 'lucide-react';
import AppLayout from '@/layouts/app-layout';
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Separator } from "@/components/ui/separator";
import { type BreadcrumbItem } from '@/types';
import { useMemo } from 'react';

interface Member {
    id: number;
    first_name: string;
    last_name: string;
    middle_name?: string;
    hebrew_name?: string;
    pivot: {
        relationship: string;
    };
}

interface Yahrzeit {
    id: number;
    members: Member[];
    name: string;
    hebrew_name?: string;
    date_of_death: string;
    hebrew_day_of_death: number;
    hebrew_month_of_death: string;
    observance_type: 'standard' | 'kaddish' | 'memorial_candle' | 'other';
    notes?: string;
    next_observance_date?: string;
    previous_observance_date?: string;
    created_at: string;
    updated_at: string;
}

interface YahrzeitShowProps {
    yahrzeit: Yahrzeit;
}

const HEBREW_MONTHS = [
    '', // 0 index placeholder
    'Tishrei', 'Cheshvan', 'Kislev', 'Tevet', 'Shevat', 'Adar', 'Adar II',
    'Nisan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul'
];

const OBSERVANCE_COLORS = {
    standard: 'bg-blue-100 text-blue-800',
    kaddish: 'bg-purple-100 text-purple-800',
    memorial_candle: 'bg-yellow-100 text-yellow-800',
    other: 'bg-gray-100 text-gray-800'
} as const;

const OBSERVANCE_LABELS = {
    standard: 'Standard Yahrzeit',
    kaddish: 'Kaddish Observance',
    memorial_candle: 'Memorial Candle',
    other: 'Other Observance'
} as const;

export default function YahrzeitShow({ yahrzeit }: YahrzeitShowProps) {
    const { t } = useTranslation();
    const handleDelete = () => {
        if (confirm(`Are you sure you want to delete the yahrzeit record for ${yahrzeit.name}? This action cannot be undone.`)) {
            router.delete(`/admin/yahrzeits/${yahrzeit.id}`, {
                onSuccess: () => {
                    router.get('/admin/yahrzeits');
                }
            });
        }
    };

    const formatDate = (dateString: string) => {
        return new Date(dateString).toLocaleDateString('en-US', {
            year: 'numeric',
            month: 'long',
            day: 'numeric'
        });
    };

    const formatHebrewDate = (day: number, month: string) => {
        const monthName = HEBREW_MONTHS.find(m => m === month) || 'Unknown';
        return `${day} ${monthName}`;
    };

    const formatDateTime = (dateString: string) => {
        return new Date(dateString).toLocaleDateString('en-US', {
            year: 'numeric',
            month: 'short',
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit'
        });
    };

    const breadcrumbs: BreadcrumbItem[] = useMemo(() => [
        {
            title: 'Dashboard',
            href: '/dashboard',
        },
        {
            title: 'Yahrzeits',
            href: '/admin/yahrzeits',
        },
        {
            title: yahrzeit.name,
            href: `/admin/yahrzeits/${yahrzeit.id}`,
        },
    ], [yahrzeit.name, yahrzeit.id]);

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={`Yahrzeit - ${yahrzeit.name}`} />
            <div className="flex h-full flex-1 flex-col gap-4 overflow-x-auto rounded-xl p-4">
         {/* Header */}
                <div className="flex items-center justify-between">
                    <div className="flex items-center gap-4">
                        <div>
                            <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">{t('Yahrzeit Record')}</h1>
                            <p className="text-gray-600 dark:text-gray-400">{t('Memorial observance details')}</p>
                        </div>
                    </div>
                    <div className="flex gap-2">
                        <Button
                            variant="outline"
                            onClick={() => router.get(`/admin/yahrzeits/${yahrzeit.id}/edit`)}
                        >
                            <Edit className="h-4 w-4 me-2" />
                            {t('Edit')}
                        </Button>
                        <Button 
                            variant="destructive"
                            onClick={handleDelete}
                        >
                            <Trash2 className="h-4 w-4 me-2" />
                            {t('Delete')}
                        </Button>
                    </div>
                </div>

                <div className="grid gap-6 md:grid-cols-2">
                    {/* Deceased Information */}
                    <Card>
                        <CardHeader>
                            <CardTitle className="flex items-center gap-2">
                                <Star className="h-5 w-5" />
                                {t('Deceased Information')}
                            </CardTitle>
                        </CardHeader>
                        <CardContent className="space-y-4">
                            <div>
                                <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Name')}</label>
                                <p className="text-lg font-semibold text-gray-900 dark:text-gray-100">{yahrzeit.name}</p>
                            </div>

                            {yahrzeit.hebrew_name && (
                                <div>
                                    <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Hebrew Name')}</label>
                                    <p className="text-lg text-gray-900 dark:text-gray-100">{yahrzeit.hebrew_name}</p>
                                </div>
                            )}
                        </CardContent>
                    </Card>

                    {/* Related Members */}
                    <Card>
                        <CardHeader>
                            <CardTitle className="flex items-center gap-2">
                                {t('Related Family Members')}
                            </CardTitle>
                        </CardHeader>
                        <CardContent>
                            {yahrzeit.members && yahrzeit.members.length > 0 ? (
                                <div className="space-y-3">
                                    {yahrzeit.members.map((member) => (
                                        <div key={member.id} className="flex items-center justify-between p-3 bg-gray-50 dark:bg-gray-800 rounded-lg">
                                            <div>
                                                <p className="font-medium text-gray-900 dark:text-gray-100">
                                                    {member.first_name} {member.last_name}
                                                </p>
                                                {member.hebrew_name && (
                                                    <p className="text-sm text-gray-500 dark:text-gray-400">
                                                        {member.hebrew_name}
                                                    </p>
                                                )}
                                            </div>
                                            <Badge variant="secondary" className="text-sm">
                                                {member.pivot.relationship}
                                            </Badge>
                                        </div>
                                    ))}
                                </div>
                            ) : (
                                <p className="text-gray-500 dark:text-gray-400 text-sm">
                                    {t('No family members associated with this yahrzeit.')}
                                </p>
                            )}
                        </CardContent>
                    </Card>
                    
                </div>

                {/* Date and Observance Information */}
                <Card>
                    <CardHeader>
                        <CardTitle className="flex items-center gap-2">
                            <Calendar className="h-5 w-5" />
                            {t('Date and Observance Details')}
                        </CardTitle>
                    </CardHeader>
                    <CardContent>
                        <div className="grid gap-6 md:grid-cols-2">
                            <div className="space-y-4">
                                <div>
                                    <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Gregorian Date of Death')}</label>
                                    <p className="text-lg text-gray-900 dark:text-gray-100 mt-1">{yahrzeit.date_of_death ? formatDate(yahrzeit.date_of_death) : 'Unavailable'}</p>
                                </div>

                                <div>
                                    <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Hebrew Date of Death')}</label>
                                    <p className="text-lg text-gray-900 dark:text-gray-100 mt-1">
                                        {formatHebrewDate(yahrzeit.hebrew_day_of_death, yahrzeit.hebrew_month_of_death)}
                                    </p>
                                </div>

                                {yahrzeit.previous_observance_date && (
                                    <div>
                                        <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Previous Observance')}</label>
                                        <p className="text-lg text-gray-900 dark:text-gray-100 mt-1">{formatDate(yahrzeit.previous_observance_date)}</p>
                                    </div>
                                )}
                            </div>

                            <div className="space-y-4">
                                <div>
                                    <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Observance Type')}</label>
                                    <div className="mt-1">
                                        <Badge className={OBSERVANCE_COLORS[yahrzeit.observance_type]}>
                                            {OBSERVANCE_LABELS[yahrzeit.observance_type]}
                                        </Badge>
                                    </div>
                                </div>

                                {yahrzeit.next_observance_date && (
                                    <div>
                                        <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Next Observance')}</label>
                                        <p className="text-lg text-gray-900 dark:text-gray-100 mt-1">{formatDate(yahrzeit.next_observance_date)}</p>
                                    </div>
                                )}
                            </div>
                        </div>

                        {yahrzeit.notes && (
                            <>
                                <Separator className="my-6" />
                                <div>
                                    <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Notes')}</label>
                                    <p className="text-gray-900 dark:text-gray-100 mt-2 whitespace-pre-line">{yahrzeit.notes}</p>
                                </div>
                            </>
                        )}
                    </CardContent>
                </Card>

                {/* Record Information */}
                <Card>
                    <CardHeader>
                        <CardTitle>{t('Record Information')}</CardTitle>
                    </CardHeader>
                    <CardContent>
                        <div className="grid gap-4 md:grid-cols-2">
                            <div>
                                <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Created')}</label>
                                <p className="text-gray-900 dark:text-gray-100 mt-1">{formatDateTime(yahrzeit.created_at)}</p>
                            </div>
                            <div>
                                <label className="text-sm font-medium text-gray-700 dark:text-gray-400">{t('Last Updated')}</label>
                                <p className="text-gray-900 dark:text-gray-100 mt-1">{formatDateTime(yahrzeit.updated_at)}</p>
                            </div>
                        </div>
                    </CardContent>
                </Card>
            </div>
        </AppLayout>
    );
}