import { useState } from 'react';
import { toast } from 'sonner';
import { useNavigate } from 'react-router';
import { useClient } from 'hooks/useClient';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Card } from '@/components/Card';
import { useForm } from 'hooks/useForm';

export function Bootstrap() {
  const authClient = useClient('/api/auth');
  const navigate = useNavigate();
  const [isSubmitting, setIsSubmitting] = useState(false);
  const { formRef, state, isValid } = useForm(
    {},
    (s) => !!s.email && !!s.name && !!s.username && !!s.password && s.password === s.confirmPassword,
  );

  const handleBootstrap = async (ev: React.FormEvent) => {
    ev.preventDefault();
    if (!isValid || isSubmitting) return;

    setIsSubmitting(true);
    try {
      await authClient.post('/bootstrap', {
        email: state.email.trim(),
        name: state.name.trim(),
        username: state.username.trim(),
        password: state.password,
        confirmPassword: state.confirmPassword,
      });
      toast.success('Administrator account created. Please sign in.');
      navigate('/');
    } catch (ex) {
      const error = ex as { message?: string };
      toast.error(error.message || 'Bootstrap failed. Please try again.');
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <Card className="md:min-w-[480px] flex flex-col gap-6">
      <div className="text-center">
        <div className="text-duck-dark text-2xl font-bold">Welcome, Admin</div>
        <div className="text-duck-dark/60">Create your administrator account</div>
      </div>

      <form ref={formRef} onSubmit={handleBootstrap} className="grid gap-4">
        <Label className="grid gap-2">
          <span className="text-duck-dark/70">Name</span>
          <Input
            className="h-11 bg-background/60 border-duck-dark/20 text-duck-dark placeholder:text-duck-dark/40"
            type="text"
            name="name"
            placeholder="Your name"
            autoComplete="name"
          />
        </Label>

        <Label className="grid gap-2">
          <span className="text-duck-dark/70">Email</span>
          <Input
            className="h-11 bg-background/60 border-duck-dark/20 text-duck-dark placeholder:text-duck-dark/40"
            type="email"
            name="email"
            placeholder="admin@example.com"
            autoComplete="email"
          />
        </Label>

        <Label className="grid gap-2">
          <span className="text-duck-dark/70">Username</span>
          <Input
            className="h-11 bg-background/60 border-duck-dark/20 text-duck-dark placeholder:text-duck-dark/40"
            type="text"
            name="username"
            placeholder="your-username"
            autoComplete="username"
          />
        </Label>

        <div className="grid md:flex gap-4">
          <Label className="grid gap-2 flex-1">
            <span className="text-duck-dark/70">Password</span>
            <Input
              className="h-11 bg-background/60 border-duck-dark/20 text-duck-dark placeholder:text-duck-dark/40"
              type="password"
              name="password"
              placeholder="Min 12 characters"
              autoComplete="new-password"
            />
          </Label>

          <Label className="grid gap-2 flex-1">
            <span className="text-duck-dark/70">Confirm Password</span>
            <Input
              className="h-11 bg-background/60 border-duck-dark/20 text-duck-dark placeholder:text-duck-dark/40"
              type="password"
              name="confirmPassword"
              placeholder="Confirm your password"
              autoComplete="new-password"
            />
          </Label>
        </div>

        <div className="pt-2">
          <Button
            type="submit"
            disabled={!isValid || isSubmitting}
            className="w-full h-11 bg-duck-yellow hover:bg-duck-yellow/90 text-duck-teal font-bold text-lg transition-all duration-200 hover:scale-105 cursor-pointer disabled:opacity-50 disabled:hover:scale-100"
          >
            {isSubmitting ? 'Creating account...' : 'Create Account'}
          </Button>
        </div>
      </form>
    </Card>
  );
}
