import { Form, Head } from '@inertiajs/react';
import { ClipboardList } from 'lucide-react';
import { useState } from 'react';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Spinner } from '@/components/ui/spinner';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';

type Course = { id: number; title: string };
type LearningPath = { id: number; title: string };

type Props = {
    courses: Course[];
    learningPaths: LearningPath[];
};

const breadcrumbs: BreadcrumbItem[] = [
    { title: 'Admin', href: '/admin' },
    { title: 'To Do List Siswa', href: '/admin/assigned-tasks' },
    { title: 'Buat Group', href: '/admin/assigned-tasks/create' },
];

export default function AdminAssignedTasksCreate({ courses, learningPaths }: Props) {
    const [type, setType] = useState<'course' | 'learning_path'>('course');

    return (
        <>
            <Head title="Buat Group To Do List" />

            <div className="flex flex-col gap-6 p-4 md:p-6">
                <div className="flex items-center gap-3">
                    <div className="flex h-10 w-10 items-center justify-center rounded-xl bg-blue-100 dark:bg-blue-800/60">
                        <ClipboardList className="h-5 w-5 text-blue-600 dark:text-blue-300" />
                    </div>
                    <div>
                        <h1 className="text-xl font-bold">Buat Group To Do List</h1>
                        <p className="text-sm text-muted-foreground">
                            Setelah buat group, tambahkan item to-do di dalamnya lalu distribusikan ke siswa.
                        </p>
                    </div>
                </div>

                <Form action="/admin/assigned-tasks" method="post" className="max-w-xl">
                    {({ processing, errors }) => (
                        <div className="flex flex-col gap-5 rounded-2xl border bg-card p-6">
                            <div className="grid gap-1.5">
                                <Label htmlFor="title" className="font-semibold">Nama Group</Label>
                                <Input id="title" name="title" placeholder="Contoh: Tugas Minggu 1 — Bab Pengantar" required />
                                <InputError message={errors.title} />
                            </div>

                            <div className="grid gap-1.5">
                                <Label htmlFor="description" className="font-semibold">
                                    Deskripsi <span className="font-normal text-muted-foreground">(opsional)</span>
                                </Label>
                                <textarea
                                    id="description"
                                    name="description"
                                    rows={2}
                                    placeholder="Penjelasan singkat tentang group ini..."
                                    className="border-input placeholder:text-muted-foreground focus-visible:border-ring flex w-full resize-none rounded-xl border bg-background px-3 py-2 text-sm shadow-xs outline-none focus-visible:ring-[3px]"
                                />
                                <InputError message={errors.description} />
                            </div>

                            <div className="grid gap-1.5">
                                <Label className="font-semibold">Target Siswa</Label>
                                <input type="hidden" name="type" value={type} />
                                <div className="grid grid-cols-2 gap-2">
                                    {(['course', 'learning_path'] as const).map((t) => (
                                        <button
                                            key={t}
                                            type="button"
                                            onClick={() => setType(t)}
                                            className={`rounded-xl border p-3 text-left text-sm transition-colors ${
                                                type === t
                                                    ? 'border-blue-400 bg-blue-50 dark:border-blue-600 dark:bg-blue-950/40'
                                                    : 'border-border hover:bg-muted/50'
                                            }`}
                                        >
                                            <p className="font-medium">{t === 'course' ? 'Per Kelas' : 'Per Paket Belajar'}</p>
                                            <p className="mt-0.5 text-xs text-muted-foreground">
                                                {t === 'course' ? 'Semua siswa di kelas tertentu' : 'Siswa di paket belajar tertentu'}
                                            </p>
                                        </button>
                                    ))}
                                </div>
                                <InputError message={errors.type} />
                            </div>

                            {type === 'course' && (
                                <div className="grid gap-1.5">
                                    <Label htmlFor="course_id" className="font-semibold">Pilih Kelas</Label>
                                    <Select name="course_id" required>
                                        <SelectTrigger className="rounded-xl">
                                            <SelectValue placeholder="Pilih kelas..." />
                                        </SelectTrigger>
                                        <SelectContent>
                                            {courses.map((c) => (
                                                <SelectItem key={c.id} value={String(c.id)}>{c.title}</SelectItem>
                                            ))}
                                        </SelectContent>
                                    </Select>
                                    <InputError message={errors.course_id} />
                                </div>
                            )}

                            {type === 'learning_path' && (
                                <div className="grid gap-1.5">
                                    <Label htmlFor="learning_path_id" className="font-semibold">Pilih Paket Belajar</Label>
                                    <Select name="learning_path_id" required>
                                        <SelectTrigger className="rounded-xl">
                                            <SelectValue placeholder="Pilih paket belajar..." />
                                        </SelectTrigger>
                                        <SelectContent>
                                            {learningPaths.map((lp) => (
                                                <SelectItem key={lp.id} value={String(lp.id)}>{lp.title}</SelectItem>
                                            ))}
                                        </SelectContent>
                                    </Select>
                                    <InputError message={errors.learning_path_id} />
                                </div>
                            )}

                            <div className="flex gap-2 pt-1">
                                <Button type="submit" disabled={processing} className="gap-1.5 bg-blue-600 hover:bg-blue-700">
                                    {processing && <Spinner />}
                                    Buat Group →
                                </Button>
                                <Button type="button" variant="outline" onClick={() => history.back()}>Batal</Button>
                            </div>
                        </div>
                    )}
                </Form>
            </div>
        </>
    );
}

AdminAssignedTasksCreate.layout = (page: React.ReactNode) => <AppLayout breadcrumbs={breadcrumbs}>{page}</AppLayout>;
