import { Head, Link, usePage } from '@inertiajs/react';
import { ArrowLeft, Plus } from 'lucide-react';
import { useCallback, useMemo } from 'react';
import { index as permanentIndex } from '@/actions/App/Http/Controllers/PermanentEmployeeController';
import {
    create as punishmentCreate,
    edit as punishmentEdit,
    index as punishmentsIndex,
    updateStatus as punishmentUpdateStatus,
} from '@/actions/App/Http/Controllers/PermanentEmployeePunishmentController';
import { EmployeePhoto } from '@/components/employees/employee-photo';
import { PermanentEmployeePunishmentsTable } from '@/components/employees/permanent-employee-punishments-table';
import { Button } from '@/components/ui/button';
import {
    Card,
    CardContent,
    CardDescription,
    CardHeader,
    CardTitle,
} from '@/components/ui/card';
import { useLanguage } from '@/contexts/language-context';
import { useInitials } from '@/hooks/use-initials';
import AppLayout from '@/layouts/app-layout';
import { dashboard } from '@/routes';

export default function PermanentEmployeePunishmentsIndex() {
    const { t } = useLanguage();
    const p = t.permanentEmployeePunishmentsPage;
    const ep = t.employeesPermanentPage;
    const getInitials = useInitials();
    const page = usePage();
    const {
        employee,
        punishments,
        filters = { search: '', per_page: 10 },
    } = page.props;

    const breadcrumbs = useMemo(
        () => [
            { title: t.dashboard.breadcrumb, href: dashboard() },
            { title: t.nav.employeeManagement, href: '#' },
            { title: ep.breadcrumb, href: permanentIndex.url() },
            {
                title: p.breadcrumb,
                href: punishmentsIndex.url({ permanentEmployee: employee.id }),
            },
        ],
        [
            t.dashboard.breadcrumb,
            t.nav.employeeManagement,
            ep.breadcrumb,
            p.breadcrumb,
            employee.id,
        ],
    );

    const punishmentsIndexUrl = useCallback(
        (opts) => punishmentsIndex.url({ permanentEmployee: employee.id }, opts),
        [employee.id],
    );

    const tableLabels = useMemo(
        () => ({
            ...p,
            cancel: t.appActions.cancel,
        }),
        [p, t.appActions.cancel],
    );

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={`${p.headTitle} — ${employee.full_name}`} />
            <div className="flex h-full flex-1 flex-col gap-6 overflow-x-auto p-4">
                <Button variant="outline" size="sm" asChild className="w-fit">
                    <Link href={permanentIndex.url()} prefetch>
                        <ArrowLeft className="size-4" />
                        {p.backToStaff}
                    </Link>
                </Button>

                <Card>
                    <CardHeader className="bg-muted/45 dark:bg-muted/20 flex flex-col gap-4 rounded-t-xl border-b border-border px-6 py-5 sm:flex-row sm:items-start sm:justify-between sm:space-y-0">
                        <div className="flex min-w-0 flex-1 gap-4">
                            <EmployeePhoto
                                src={employee.image_url}
                                initials={getInitials(employee.full_name)}
                                size="md"
                            />
                            <div className="min-w-0 space-y-2">
                                <CardTitle className="text-xl font-semibold tracking-tight">
                                    {employee.full_name}
                                </CardTitle>
                                {employee.designation_name ? (
                                    <p className="text-muted-foreground text-sm">
                                        {employee.designation_name}
                                    </p>
                                ) : null}
                                <CardDescription className="text-muted-foreground max-w-xl text-sm leading-relaxed">
                                    {p.subtitle}
                                </CardDescription>
                            </div>
                        </div>
                        <Button asChild className="shrink-0">
                            <Link
                                href={punishmentCreate.url({ permanentEmployee: employee.id })}
                                prefetch
                            >
                                <Plus className="size-4" />
                                {p.addPunishment}
                            </Link>
                        </Button>
                    </CardHeader>
                    <CardContent className="px-4 pb-4 sm:px-6">
                        <PermanentEmployeePunishmentsTable
                            punishments={punishments}
                            filters={filters}
                            labels={tableLabels}
                            indexUrl={punishmentsIndexUrl}
                            editUrl={(id) =>
                                punishmentEdit.url({
                                    permanentEmployee: employee.id,
                                    punishment: id,
                                })
                            }
                            statusUrl={(id) =>
                                punishmentUpdateStatus.url({
                                    permanentEmployee: employee.id,
                                    punishment: id,
                                })
                            }
                        />
                    </CardContent>
                </Card>
            </div>
        </AppLayout>
    );
}
