import { Head, Link, router, useForm } from '@inertiajs/react';
import { useState } from 'react';
import { ConfirmDialog } from '@/components/admin/confirm-dialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';

interface School {
    id: number;
    title: string;
    year: number;
    location: string | null;
    start_date: string | null;
    end_date: string | null;
    summary: string | null;
    sort_order: number;
}

export default function SummerSchoolEdit({ school }: { school: School }) {
    const [confirmDelete, setConfirmDelete] = useState(false);

    const { data, setData, put, processing, errors } = useForm({
        title:      school.title,
        year:       school.year.toString(),
        location:   school.location ?? '',
        start_date: school.start_date ?? '',
        end_date:   school.end_date ?? '',
        summary:    school.summary ?? '',
        sort_order: school.sort_order.toString(),
    });

    function submit(e: React.FormEvent) {
        e.preventDefault();
        put(`/admin/summer-schools/${school.id}`);
    }

    return (
        <>
            <Head title={`Edit — ${school.title}`} />
            <form onSubmit={submit} className="space-y-0">
                <div className="sticky top-16 z-10 -mx-6 mb-6 flex items-center justify-between border-b bg-background px-6 pb-4 pt-2 lg:-mx-8 lg:px-8">
                    <div>
                        <div className="flex items-center gap-2 text-sm text-muted-foreground">
                            <Link href="/admin/summer-schools">Summer Schools</Link>
                            <span>/</span>
                            <span className="text-foreground truncate max-w-[220px]">{school.title}</span>
                        </div>
                        <h1 className="text-2xl font-bold">Edit Edition</h1>
                    </div>
                    <div className="flex items-center gap-3">
                        <Button type="button" variant="outline" asChild>
                            <Link href="/admin/summer-schools">Cancel</Link>
                        </Button>
                        <Button type="button" variant="destructive" onClick={() => setConfirmDelete(true)}>
                            Delete
                        </Button>
                        <Button type="submit" disabled={processing}>
                            {processing ? 'Saving…' : 'Save Changes'}
                        </Button>
                    </div>
                </div>

                <div className="grid grid-cols-1 gap-6 lg:grid-cols-[1fr_300px]">
                    {/* Main */}
                    <div className="space-y-6">
                        <div className="rounded-xl border border-border bg-card p-6 space-y-4">
                            <div className="space-y-1.5">
                                <Label htmlFor="title">Title *</Label>
                                <Input id="title" value={data.title} onChange={(e) => setData('title', e.target.value)} required />
                                {errors.title && <p className="text-xs text-destructive">{errors.title}</p>}
                            </div>
                            <div className="space-y-1.5">
                                <Label htmlFor="location">Location</Label>
                                <Input id="location" value={data.location} onChange={(e) => setData('location', e.target.value)} placeholder="e.g. Nairobi, Kenya" />
                                {errors.location && <p className="text-xs text-destructive">{errors.location}</p>}
                            </div>
                            <div className="space-y-1.5">
                                <Label htmlFor="summary">Summary</Label>
                                <Textarea id="summary" value={data.summary} onChange={(e) => setData('summary', e.target.value)} rows={4} />
                                {errors.summary && <p className="text-xs text-destructive">{errors.summary}</p>}
                            </div>
                        </div>
                    </div>

                    {/* Sidebar */}
                    <div className="space-y-6">
                        <div className="rounded-xl border border-border bg-card p-6 space-y-4">
                            <div className="space-y-1.5">
                                <Label htmlFor="year">Year *</Label>
                                <Input id="year" type="number" value={data.year} onChange={(e) => setData('year', e.target.value)} required />
                                {errors.year && <p className="text-xs text-destructive">{errors.year}</p>}
                            </div>
                            <div className="space-y-1.5">
                                <Label htmlFor="start_date">Start Date</Label>
                                <Input id="start_date" type="date" value={data.start_date} onChange={(e) => setData('start_date', e.target.value)} />
                                {errors.start_date && <p className="text-xs text-destructive">{errors.start_date}</p>}
                            </div>
                            <div className="space-y-1.5">
                                <Label htmlFor="end_date">End Date</Label>
                                <Input id="end_date" type="date" value={data.end_date} onChange={(e) => setData('end_date', e.target.value)} />
                                {errors.end_date && <p className="text-xs text-destructive">{errors.end_date}</p>}
                            </div>
                            <div className="space-y-1.5">
                                <Label htmlFor="sort_order">Sort Order</Label>
                                <Input id="sort_order" type="number" value={data.sort_order} onChange={(e) => setData('sort_order', e.target.value)} />
                            </div>
                        </div>
                    </div>
                </div>
            </form>

            <ConfirmDialog
                open={confirmDelete}
                onOpenChange={setConfirmDelete}
                title="Delete summer school?"
                description="This is permanent and cannot be undone."
                confirmLabel="Delete"
                variant="danger"
                onConfirm={() => router.delete(`/admin/summer-schools/${school.id}`)}
            />
        </>
    );
}
