import { Form, Head } from '@inertiajs/react';
import { Button } from '@/components/ui/button';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';

type Course = { id: number; title: string; slug: string };
type Props = { courses: Course[] };

const breadcrumbs: BreadcrumbItem[] = [
    { title: 'Live Class Saya', href: '/instructor/live-classes' },
    { title: 'Buat Live Class', href: '#' },
];

export default function InstructorLiveClassCreate({ courses }: Props) {
    return (
        <>
            <Head title="Buat Live Class" />

            <div className="flex flex-col gap-6 p-4 md:p-6 max-w-2xl">
                <div>
                    <h1 className="text-xl font-bold">Buat Live Class</h1>
                    <p className="mt-0.5 text-sm text-muted-foreground">Jadwalkan sesi live bersama siswa.</p>
                </div>

                <Form action="/instructor/live-classes" method="post" className="rounded-xl border bg-card p-5">
                    {({ errors, processing }) => (
                        <div className="flex flex-col gap-4">
                            <div>
                                <label className="mb-1.5 block text-sm font-medium">Judul Sesi *</label>
                                <input
                                    name="title"
                                    required
                                    className="border-input focus-visible:border-ring w-full rounded-md border bg-transparent px-3 py-2 text-sm outline-none"
                                    placeholder="Mis: Sesi Tanya Jawab Manajemen Keuangan"
                                />
                                {errors.title && <p className="mt-1 text-xs text-destructive">{errors.title}</p>}
                            </div>

                            <div>
                                <label className="mb-1.5 block text-sm font-medium">Deskripsi</label>
                                <textarea
                                    name="description"
                                    rows={3}
                                    className="border-input focus-visible:border-ring w-full rounded-md border bg-transparent px-3 py-2 text-sm outline-none"
                                    placeholder="Topik yang akan dibahas..."
                                />
                            </div>

                            <div>
                                <label className="mb-1.5 block text-sm font-medium">Course Terkait</label>
                                <select
                                    name="course_id"
                                    className="border-input focus-visible:border-ring w-full rounded-md border bg-transparent px-3 py-2 text-sm outline-none"
                                >
                                    <option value="">-- Umum (tidak terikat course) --</option>
                                    {courses.map((c) => (
                                        <option key={c.id} value={c.id}>{c.title}</option>
                                    ))}
                                </select>
                            </div>

                            <div className="grid grid-cols-2 gap-4">
                                <div>
                                    <label className="mb-1.5 block text-sm font-medium">Tanggal & Waktu *</label>
                                    <input
                                        name="scheduled_at"
                                        type="datetime-local"
                                        required
                                        className="border-input focus-visible:border-ring w-full rounded-md border bg-transparent px-3 py-2 text-sm outline-none"
                                    />
                                    {errors.scheduled_at && <p className="mt-1 text-xs text-destructive">{errors.scheduled_at}</p>}
                                </div>
                                <div>
                                    <label className="mb-1.5 block text-sm font-medium">Durasi (menit) *</label>
                                    <input
                                        name="duration_minutes"
                                        type="number"
                                        defaultValue={60}
                                        min={15}
                                        max={480}
                                        required
                                        className="border-input focus-visible:border-ring w-full rounded-md border bg-transparent px-3 py-2 text-sm outline-none"
                                    />
                                    {errors.duration_minutes && <p className="mt-1 text-xs text-destructive">{errors.duration_minutes}</p>}
                                </div>
                            </div>

                            <div>
                                <label className="mb-1.5 block text-sm font-medium">Meeting Link *</label>
                                <input
                                    name="meeting_url"
                                    type="url"
                                    required
                                    className="border-input focus-visible:border-ring w-full rounded-md border bg-transparent px-3 py-2 text-sm outline-none"
                                    placeholder="https://meet.google.com/... atau https://zoom.us/j/..."
                                />
                                {errors.meeting_url && <p className="mt-1 text-xs text-destructive">{errors.meeting_url}</p>}
                            </div>

                            <div className="flex items-center gap-2">
                                <input
                                    type="checkbox"
                                    name="is_published"
                                    id="is_published"
                                    value="1"
                                    className="h-4 w-4 rounded border"
                                />
                                <label htmlFor="is_published" className="text-sm">Publikasikan sekarang (siswa bisa melihat dan daftar)</label>
                            </div>

                            <div className="flex gap-2 pt-2">
                                <Button type="submit" disabled={processing}>
                                    Buat Live Class
                                </Button>
                            </div>
                        </div>
                    )}
                </Form>
            </div>
        </>
    );
}

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