import AppLayout from '@/layouts/app-layout';
import { type BreadcrumbItem } from '@/types';
import { Head, Link, useForm } from '@inertiajs/react';
import {
    Card,
    CardContent,
    CardFooter,
    CardHeader,
    CardTitle,
} from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import InputError from '@/components/input-error';
import { Button, buttonVariants } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { FormEventHandler, useRef } from 'react';
import { Switch } from '@/components/ui/switch';

type CreateSurveyForm = {
    title: string;
    is_active: boolean;
};

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Survey Create',
        href: '/survey/create',
    },
];

export default function Create() {
    const titleInput = useRef<HTMLInputElement>(null);

    const { data, setData, errors, post, reset, processing } = useForm<CreateSurveyForm>({
        title: '',
        is_active: true,
    });

    const createSurvey: FormEventHandler = (e) => {
        e.preventDefault();
        post(route('survey.store'), {
            preserveScroll: true,
            onSuccess: () => {
                reset();
            },
            onError: (errs) => {
                if (errs.title) {
                    titleInput.current?.focus();
                }
            }
        });
    };

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Survey Create" />
            <div className="m-5">
                <div className="flex">
                    <Link className={(buttonVariants({ variant: 'default', className: 'mb-2 ml-auto' }))} href={route('survey.index')}>
                        Survey List
                    </Link>
                </div>

                <Card>
                    <CardHeader>
                        <CardTitle>Create Survey</CardTitle>
                    </CardHeader>
                    <form onSubmit={createSurvey}>
                        <CardContent>
                            <div className="grid w-full items-center gap-4">
                                
                                {/* Title Field */}
                                <div className="flex flex-col space-y-1.5">
                                    <Label htmlFor="title">Title</Label>
                                    <Input
                                        id="title"
                                        ref={titleInput}
                                        value={data.title}
                                        onChange={(e) => setData('title', e.target.value)}
                                        placeholder="Enter the survey title"
                                        disabled={processing}
                                    />
                                    <InputError message={errors.title} />
                                </div>

                                {/* Active Toggle */}
                                {/* <div className="flex flex-col space-y-1.5">
                                    <div className="flex items-center space-x-2">
                                        <Switch
                                            id="is_active"
                                            checked={data.is_active}
                                            onCheckedChange={(checked) => setData('is_active', checked)}
                                            disabled={processing}
                                        />
                                        <Label htmlFor="is_active">Is Active (Note: Only one regular survey can be active at a time)</Label>
                                    </div>
                                    <InputError message={errors.is_active} />
                                </div> */}
                            </div>
                        </CardContent>
                        <CardFooter className="flex justify-between mt-5">
                            <div className="space-x-2">
                                <Link className={(buttonVariants({ variant: 'default', className: 'ml-auto' }))} href={route('survey.index')}>
                                    Cancel
                                </Link>
                                <Button type="button" variant="outline" onClick={() => reset()}>
                                    Reset
                                </Button>
                            </div>
                            <Button type="submit" disabled={processing}>
                                Create
                            </Button>
                        </CardFooter>
                    </form>
                </Card>
            </div>
        </AppLayout>
    );
}
