import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { useLogin } from '@/modules/auth/hooks/use-auth-hook';
import { zUserLoginDto } from '@/sdk/zod.gen';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import type { z } from 'zod';
import { Link } from '@tanstack/react-router';
import { Route as SignupRoute } from '@/routes/auth/signup';
import { Route as ForgotPasswordRoute } from '@/routes/auth/forgot-password';
import { loginFormDefaults } from '@/modules/auth/schemas/form-schemas';

type LoginFormData = z.infer<typeof zUserLoginDto>;

export function LoginForm() {
  const { t } = useTranslation();
  const { mutate, isPending } = useLogin();
  const form = useForm<LoginFormData>({
    resolver: zodResolver(zUserLoginDto),
    defaultValues: loginFormDefaults,
  });

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

  return (
    <div className='flex flex-col gap-6'>
      <h1 className='text-2xl font-semibold'>{t('auth.login.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.login.email')} *</FormLabel>
                <FormControl>
                  <Input
                    type='email'
                    placeholder={t('auth.login.emailPlaceholder')}
                    autoComplete='email'
                    autoFocus
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name='password'
            render={({ field }) => (
              <FormItem>
                <FormLabel>{t('auth.login.password')} *</FormLabel>
                <FormControl>
                  <Input
                    type='password'
                    placeholder={t('auth.login.passwordPlaceholder')}
                    autoComplete='current-password'
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <Button
            type='submit'
            disabled={isPending}>
            {t('auth.login.submit')}
          </Button>
        </form>
      </Form>
      <div className='flex flex-col gap-2 text-sm text-center'>
        <Link
          to={ForgotPasswordRoute.to}
          className='text-primary hover:underline'>
          {t('auth.login.forgotPasswordLink')}
        </Link>
        <Link
          to={SignupRoute.to}
          className='text-primary hover:underline'>
          {t('auth.login.signupLink')}
        </Link>
      </div>
    </div>
  );
}
