import { Head, useForm } from '@inertiajs/react';
import { LoaderCircle } from 'lucide-react';
import { FormEventHandler } from 'react';

import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import NeuralBackground from '@/components/landing/neural-background';
import AIParticles from '@/components/landing/ai-particles';
import Container from '@/components/landing/container';
import AppLogoIcon from '@/components/app-logo-icon';
import ThemeToggle from '@/components/theme-toggle';

type Institution = {
    id: number;
    name: string;
};

type CompleteProfileForm = {
    institution_id: string;
    description: string;
    profile_photo: File | null;
};

type Props = {
    institutions: Institution[];
};

export default function CompleteProfile({ institutions }: Props) {
    const { data, setData, post, processing, errors } = useForm<CompleteProfileForm>({
        institution_id: '',
        description: '',
        profile_photo: null,
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();
        post(route('profile.complete'), {
            forceFormData: true, // Para manejar archivos
        });
    };

    const skip = () => {
        post(route('profile.skip'));
    };

    return (
        <>
            <Head title="Completar Perfil" />
            <div className="relative min-h-screen flex items-center justify-center overflow-hidden bg-gradient-to-br from-slate-50 via-purple-50 to-slate-50 dark:from-slate-900 dark:via-purple-900 dark:to-slate-900">
                {/* Toggle de tema flotante */}
                <div className="fixed top-4 left-4 z-50">
                    <ThemeToggle />
                </div>
                <NeuralBackground />
                <AIParticles density={30} />
                <Container className="relative z-10 flex flex-col items-center justify-center min-h-screen py-12">
                    <div className="w-full max-w-lg mx-auto bg-white/90 dark:bg-slate-900/80 rounded-3xl shadow-2xl border border-slate-200 dark:border-slate-800/60 backdrop-blur-md p-8 sm:p-10 flex flex-col gap-8">
                        <div className="flex flex-col items-center gap-3">
                            <AppLogoIcon className="w-12 h-12 mb-2 text-cyan-700 dark:text-cyan-400" />
                            <h1 className="text-2xl font-extrabold dark:text-white text-black ">Completar Perfil</h1>
                            <p className="text-sm text-slate-500 dark:text-slate-300 text-center">Completa tu perfil para personalizar tu experiencia (opcional)</p>
                        </div>
                        <form className="flex flex-col gap-6" onSubmit={submit}>
                            <div className="grid gap-6">
                                <div className="grid gap-2">
                                    <Label htmlFor="institution_id">Institución</Label>
                                    <Select value={data.institution_id} onValueChange={(value) => setData('institution_id', value)}>
                                        <SelectTrigger>
                                            <SelectValue placeholder="Selecciona tu institución" />
                                        </SelectTrigger>
                                        <SelectContent>
                                            {institutions.map((institution) => (
                                                <SelectItem key={institution.id} value={institution.id.toString()}>
                                                    {institution.name}
                                                </SelectItem>
                                            ))}
                                        </SelectContent>
                                    </Select>
                                    <InputError message={errors.institution_id} />
                                </div>

                                <div className="grid gap-2">
                                    <Label htmlFor="description">Descripción del perfil</Label>
                                    <Textarea
                                        id="description"
                                        value={data.description}
                                        onChange={(e) => setData('description', e.target.value)}
                                        disabled={processing}
                                        placeholder="Cuéntanos un poco sobre ti..."
                                        rows={4}
                                    />
                                    <InputError message={errors.description} />
                                </div>

                                <div className="grid gap-2">
                                    <Label htmlFor="profile_photo">Foto de perfil</Label>
                                    <Input
                                        id="profile_photo"
                                        type="file"
                                        accept="image/*"
                                        onChange={(e) => setData('profile_photo', e.target.files?.[0] || null)}
                                        disabled={processing}
                                    />
                                    <InputError message={errors.profile_photo} />
                                </div>

                                <div className="flex flex-col gap-3">
                                    <Button type="submit" className="w-full bg-emerald-600 text-white font-bold rounded-2xl shadow-lg hover:scale-105 transition-all duration-300 border-none" disabled={processing}>
                                        {processing && <LoaderCircle className="h-4 w-4 animate-spin" />}
                                        Guardar y continuar
                                    </Button>

                                    <Button type="button" variant="outline" className="w-full rounded-2xl" onClick={skip} disabled={processing}>
                                        Omitir por ahora
                                    </Button>
                                </div>
                            </div>
                        </form>
                    </div>
                </Container>
            </div>
        </>
    );
}
