import { Head, useForm } from '@inertiajs/react';
import { lookup } from '@/routes/join';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import {
    Card,
    CardContent,
    CardDescription,
    CardHeader,
    CardTitle,
} from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import AppLogoIcon from '@/components/app-logo-icon';

type Meeting = {
    uuid: string;
    title: string;
    scheduled_at: string;
    status: 'scheduled' | 'running' | 'ended';
};

type Classroom = {
    name: string;
    course_title: string;
    code: string;
};

type Props = {
    meeting: Meeting;
    classroom: Classroom;
    requiresPassword: boolean;
};

function formatDateTime(dateString: string) {
    return new Intl.DateTimeFormat('it-IT', {
        dateStyle: 'medium',
        timeStyle: 'short',
    }).format(new Date(dateString));
}

export default function JoinMeeting({
    meeting,
    classroom,
    requiresPassword,
}: Props) {
    const { data, setData, post, processing, errors } = useForm({
        fiscal_code: '',
        access_password: '',
    });

    const handleSubmit = (e: React.FormEvent) => {
        e.preventDefault();
        post(lookup(meeting.uuid).url);
    };

    return (
        <>
            <Head title={`${meeting.title} - EasyForma`} />

            <div className="bg-background flex min-h-screen flex-col items-center justify-center gap-6 p-6 md:p-10">
                <div className="flex w-full max-w-md flex-col gap-6">
                    {/* Header with Logo */}
                    <div className="flex items-center gap-2 self-center">
                        <div className="flex h-9 w-9 items-center justify-center">
                            <AppLogoIcon className="text-foreground size-9 fill-current" />
                        </div>
                    </div>

                    {/* Main Card */}
                    <Card className="rounded-xl">
                        <CardHeader className="px-10 pt-8 pb-0 text-center">
                            <CardTitle className="text-xl">
                                {meeting.title}
                            </CardTitle>
                            <CardDescription>
                                {classroom.name}
                                {classroom.course_title && (
                                    <>
                                        {' - '}
                                        {classroom.course_title}
                                    </>
                                )}
                            </CardDescription>
                            <div className="text-muted-foreground mt-2 text-xs">
                                {formatDateTime(meeting.scheduled_at)}
                            </div>
                        </CardHeader>

                        <CardContent className="px-10 py-8">
                            <form onSubmit={handleSubmit} className="space-y-6">
                                <div className="space-y-4">
                                    <p className="text-muted-foreground text-sm">
                                        Inserisci il codice fiscale con cui sei
                                        stato iscritto per ricevere il tuo link
                                        personale.
                                    </p>

                                    <div className="grid gap-2">
                                        <Label htmlFor="fiscal_code">
                                            Codice fiscale
                                        </Label>
                                        <Input
                                            id="fiscal_code"
                                            type="text"
                                            name="fiscal_code"
                                            value={data.fiscal_code.toUpperCase()}
                                            onChange={(e) =>
                                                setData(
                                                    'fiscal_code',
                                                    e.target.value,
                                                )
                                            }
                                            maxLength={16}
                                            placeholder="Es. RSSMRA85T10A001Z"
                                            className="uppercase"
                                            autoFocus
                                            required
                                        />
                                        <InputError
                                            message={
                                                errors.fiscal_code as string
                                            }
                                        />
                                    </div>

                                    {requiresPassword && (
                                        <div className="grid gap-2">
                                            <Label htmlFor="access_password">
                                                Password d&apos;accesso
                                            </Label>
                                            <Input
                                                id="access_password"
                                                type="password"
                                                name="access_password"
                                                value={data.access_password}
                                                onChange={(e) =>
                                                    setData(
                                                        'access_password',
                                                        e.target.value,
                                                    )
                                                }
                                                maxLength={32}
                                                required
                                            />
                                            <InputError
                                                message={
                                                    errors.access_password as string
                                                }
                                            />
                                        </div>
                                    )}
                                </div>

                                <Button
                                    type="submit"
                                    className="w-full"
                                    disabled={processing}
                                >
                                    {processing && <Spinner />}
                                    Continua
                                </Button>
                            </form>
                        </CardContent>
                    </Card>
                </div>
            </div>
        </>
    );
}
