import { Head, Link, useForm, usePage } from '@inertiajs/react';
import type { FormEvent } from 'react';
import CreditCostField from '@/components/classrooms/credit-cost-field';
import Heading from '@/components/heading';
import InputError from '@/components/input-error';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { Textarea } from '@/components/ui/textarea';
import classrooms, { index as classroomsIndex } from '@/routes/classrooms';
import billing from '@/routes/billing';

type Props = {
    creditsAvailable?: number;
    defaultModerator: { name: string; email: string };
};

export default function ClassroomsCreate({
    creditsAvailable = 0,
    defaultModerator,
}: Props) {
    const form = useForm({
        name: '',
        course_title: '',
        formatemp_project_code: '',
        starts_on: '',
        ends_on: '',
        max_participants: '15',
        max_hours: '8',
        record_meetings: false,
        max_cameras: '',
        moderator_name: defaultModerator.name,
        moderator_email: defaultModerator.email,
        access_password: '',
        notes: '',
    });

    const { errors: pageErrors } = usePage<{
        errors: { credits?: string };
    }>().props;

    function submit(e: FormEvent) {
        e.preventDefault();
        form.post(classrooms.store().url);
    }

    return (
        <>
            <Head title="Nuova aula" />

            <div className="mx-auto flex w-full max-w-2xl flex-1 flex-col gap-6 p-4">
                <Heading
                    title="Nuova aula"
                    description="Scegli quanto grande fare l'aula: il costo in crediti dipende da discenti e ore massime."
                />

                {pageErrors.credits && (
                    <Alert variant="destructive">
                        <AlertTitle>Impossibile creare l&apos;aula</AlertTitle>
                        <AlertDescription>
                            {pageErrors.credits}{' '}
                            <Link
                                href={billing.index()}
                                className="underline underline-offset-4"
                            >
                                Ricarica i crediti
                            </Link>
                            .
                        </AlertDescription>
                    </Alert>
                )}

                <form onSubmit={submit} className="space-y-6">
                    <div className="grid gap-2">
                        <Label htmlFor="name">Nome aula</Label>
                        <Input
                            id="name"
                            value={form.data.name}
                            onChange={(e) =>
                                form.setData('name', e.target.value)
                            }
                            required
                        />
                        <InputError message={form.errors.name} />
                    </div>

                    <div className="grid gap-2">
                        <Label htmlFor="course_title">Titolo del corso</Label>
                        <Input
                            id="course_title"
                            value={form.data.course_title}
                            onChange={(e) =>
                                form.setData('course_title', e.target.value)
                            }
                        />
                        <InputError message={form.errors.course_title} />
                    </div>

                    <div className="grid gap-2">
                        <Label htmlFor="formatemp_project_code">
                            Codice progetto Forma.Temp
                        </Label>
                        <Input
                            id="formatemp_project_code"
                            value={form.data.formatemp_project_code}
                            onChange={(e) =>
                                form.setData(
                                    'formatemp_project_code',
                                    e.target.value,
                                )
                            }
                        />
                        <InputError
                            message={form.errors.formatemp_project_code}
                        />
                    </div>

                    <CreditCostField
                        participants={form.data.max_participants}
                        hours={form.data.max_hours}
                        onParticipantsChange={(value) =>
                            form.setData('max_participants', value)
                        }
                        onHoursChange={(value) =>
                            form.setData('max_hours', value)
                        }
                        creditsAvailable={creditsAvailable}
                        participantsError={form.errors.max_participants}
                        hoursError={form.errors.max_hours}
                    />

                    <div className="grid grid-cols-2 gap-4">
                        <div className="grid gap-2">
                            <Label htmlFor="starts_on">Data inizio</Label>
                            <Input
                                id="starts_on"
                                type="date"
                                value={form.data.starts_on}
                                onChange={(e) =>
                                    form.setData('starts_on', e.target.value)
                                }
                            />
                            <InputError message={form.errors.starts_on} />
                        </div>
                        <div className="grid gap-2">
                            <Label htmlFor="ends_on">Data fine</Label>
                            <Input
                                id="ends_on"
                                type="date"
                                value={form.data.ends_on}
                                onChange={(e) =>
                                    form.setData('ends_on', e.target.value)
                                }
                            />
                            <InputError message={form.errors.ends_on} />
                        </div>
                    </div>

                    <div className="flex items-center justify-between rounded-lg border p-3">
                        <div className="space-y-0.5">
                            <Label htmlFor="record_meetings">
                                Registra le lezioni
                            </Label>
                            <p className="text-muted-foreground text-sm">
                                Imposta la registrazione come predefinita per le
                                nuove lezioni di questa aula.
                            </p>
                        </div>
                        <Switch
                            id="record_meetings"
                            checked={form.data.record_meetings}
                            onCheckedChange={(checked) =>
                                form.setData('record_meetings', checked)
                            }
                        />
                    </div>

                    <div className="grid gap-2">
                        <Label htmlFor="max_cameras">
                            Numero massimo webcam (opzionale)
                        </Label>
                        <Input
                            id="max_cameras"
                            type="number"
                            min={0}
                            value={form.data.max_cameras}
                            onChange={(e) =>
                                form.setData('max_cameras', e.target.value)
                            }
                        />
                        <InputError message={form.errors.max_cameras} />
                    </div>

                    <div className="grid grid-cols-2 gap-4">
                        <div className="grid gap-2">
                            <Label htmlFor="moderator_name">
                                Nome moderatore
                            </Label>
                            <Input
                                id="moderator_name"
                                value={form.data.moderator_name}
                                onChange={(e) =>
                                    form.setData(
                                        'moderator_name',
                                        e.target.value,
                                    )
                                }
                            />
                            <InputError message={form.errors.moderator_name} />
                        </div>
                        <div className="grid gap-2">
                            <Label htmlFor="moderator_email">
                                Email moderatore
                            </Label>
                            <Input
                                id="moderator_email"
                                type="email"
                                value={form.data.moderator_email}
                                onChange={(e) =>
                                    form.setData(
                                        'moderator_email',
                                        e.target.value,
                                    )
                                }
                            />
                            <InputError message={form.errors.moderator_email} />
                        </div>
                    </div>

                    <div className="grid gap-2">
                        <Label htmlFor="access_password">
                            Password d&apos;accesso aggiuntiva (opzionale)
                        </Label>
                        <Input
                            id="access_password"
                            value={form.data.access_password}
                            onChange={(e) =>
                                form.setData('access_password', e.target.value)
                            }
                            maxLength={32}
                        />
                        <p className="text-muted-foreground text-sm">
                            Se impostata, sul link pubblico della lezione viene
                            richiesta oltre al codice fiscale.
                        </p>
                        <InputError message={form.errors.access_password} />
                    </div>

                    <div className="grid gap-2">
                        <Label htmlFor="notes">Note</Label>
                        <Textarea
                            id="notes"
                            value={form.data.notes}
                            onChange={(e) =>
                                form.setData('notes', e.target.value)
                            }
                        />
                        <InputError message={form.errors.notes} />
                    </div>

                    <div className="flex items-center gap-4">
                        <Button type="submit" disabled={form.processing}>
                            Crea aula
                        </Button>
                        <Link
                            href={classroomsIndex()}
                            className="text-muted-foreground text-sm hover:underline"
                        >
                            Annulla
                        </Link>
                    </div>
                </form>
            </div>
        </>
    );
}

ClassroomsCreate.layout = {
    breadcrumbs: [
        { title: 'Aule', href: classroomsIndex() },
        { title: 'Nuova aula', href: classrooms.create() },
    ],
};
