import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { useSignup } from '@/modules/auth/hooks/use-auth-hook';
import { zUserSignupDto } from '@/sdk/zod.gen';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { Link } from '@tanstack/react-router';
import { Route as LoginRoute } from '@/routes/auth/login';
import { z } from 'zod';
import { signupFormDefaults } from '@/modules/auth/schemas/form-schemas';

type SignupFormData = z.infer<typeof zUserSignupDto>;

export function SignupForm() {
  const { t } = useTranslation();
  const { mutate, isPending } = useSignup();
  const extendedSchema = zUserSignupDto.extend({
    password: z.string().min(6).max(32),
  });
  const form = useForm<SignupFormData>({
    resolver: zodResolver(extendedSchema),
    defaultValues: signupFormDefaults,
  });

  const onSubmit = (data: SignupFormData) => {
    mutate({ body: data });
  };

  return (
    <div className='flex flex-col gap-6'>
      <h1 className='text-2xl font-semibold'>{t('auth.signup.title')}</h1>
      <Form {...form}>
        <form
          noValidate
          className='flex flex-col gap-4'
          onSubmit={form.handleSubmit(onSubmit)}>
          <FormField
            control={form.control}
            name='email'
            render={({ field }) => (
              <FormItem>
                <FormLabel>{t('auth.signup.email')} *</FormLabel>
                <FormControl>
                  <Input
                    type='email'
                    placeholder={t('auth.signup.emailPlaceholder')}
                    autoComplete='email'
                    autoFocus
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name='password'
            render={({ field }) => (
              <FormItem>
                <FormLabel>{t('auth.signup.password')} *</FormLabel>
                <FormControl>
                  <Input
                    type='password'
                    placeholder={t('auth.signup.passwordPlaceholder')}
                    autoComplete='new-password'
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <Button
            type='submit'
            disabled={isPending}>
            {t('auth.signup.submit')}
          </Button>
        </form>
      </Form>
      <div className='text-sm text-center'>
        <Link
          to={LoginRoute.to}
          className='text-primary hover:underline'>
          {t('auth.signup.loginLink')}
        </Link>
      </div>
    </div>
  );
}
