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 EditSurveyForm = {
    title: string;
    is_active: boolean;
};

export default function Edit({ survey }: { survey: any }) {
    const titleInput = useRef<HTMLInputElement>(null);

    const { data, setData, errors, put, reset, processing } = useForm<EditSurveyForm>({
        title: survey.title || '',
        is_active: survey.is_active || false,
    });

    const editSurvey: FormEventHandler = (e) => {
        e.preventDefault();
        put(route('survey.update', survey.id), {
            preserveScroll: true,
            onError: (errs) => {
                if (errs.title) {
                    titleInput.current?.focus();
                }
            }
        });
    };

    const breadcrumbs: BreadcrumbItem[] = [
        {
            title: 'Survey Edit',
            href: `/survey/${survey.id}/edit`,
        },
    ];

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Survey Edit" />
            <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>Edit Survey</CardTitle>
                    </CardHeader>
                    <form onSubmit={editSurvey}>
                        <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 || survey.type === 'initial'}
                                        />
                                        <Label htmlFor="is_active">
                                            {survey.type === 'initial' 
                                                ? 'Is Active (Initial survey is always active)' 
                                                : 'Is Active (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}>
                                Update
                            </Button>
                        </CardFooter>
                    </form>
                </Card>
            </div>
        </AppLayout>
    );
}
