import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { useResetPassword } from '@/modules/auth/hooks/use-auth-hook';
import { zUserResetPasswordDto } 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, useSearch } from '@tanstack/react-router';
import { Route as LoginRoute } from '@/routes/auth/login';
import { resetPasswordFormDefaults } from '@/modules/auth/schemas/form-schemas';

type ResetPasswordFormData = z.infer<typeof zUserResetPasswordDto>;

export function ResetPasswordForm() {
  const { t } = useTranslation();
  const { mutate, isPending } = useResetPassword();
  const search = useSearch({ from: '/auth/reset-password' });
  const form = useForm<ResetPasswordFormData>({
    resolver: zodResolver(zUserResetPasswordDto),
    defaultValues: {
      ...resetPasswordFormDefaults,
      email: search.email || '',
    },
  });

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

  return (
    <div className='flex flex-col gap-6'>
      <h1 className='text-2xl font-semibold'>{t('auth.resetPassword.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.resetPassword.email')} *</FormLabel>
                <FormControl>
                  <Input
                    type='email'
                    placeholder={t('auth.resetPassword.emailPlaceholder')}
                    autoComplete='email'
                    readOnly
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name='otp'
            render={({ field }) => (
              <FormItem>
                <FormLabel>{t('auth.resetPassword.otp')} *</FormLabel>
                <FormControl>
                  <Input
                    type='text'
                    placeholder={t('auth.resetPassword.otpPlaceholder')}
                    autoComplete='one-time-code'
                    autoFocus
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name='password'
            render={({ field }) => (
              <FormItem>
                <FormLabel>{t('auth.resetPassword.password')} *</FormLabel>
                <FormControl>
                  <Input
                    type='password'
                    placeholder={t('auth.resetPassword.passwordPlaceholder')}
                    autoComplete='new-password'
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <Button
            type='submit'
            disabled={isPending}>
            {t('auth.resetPassword.submit')}
          </Button>
        </form>
      </Form>
      <div className='text-sm text-center'>
        <Link
          to={LoginRoute.to}
          className='text-primary hover:underline'>
          {t('auth.forgotPassword.loginLink')}
        </Link>
      </div>
    </div>
  );
}
