'use client';

import { updateUserPassword } from '@/services/user/user';
import { zodResolver } from '@hookform/resolvers/zod';
import { Button } from '@workspace/ui/components/button';
import { Card } from '@workspace/ui/components/card';
import { Form, FormControl, FormField, FormItem, FormMessage } from '@workspace/ui/components/form';
import { Input } from '@workspace/ui/components/input';
import { useTranslations } from 'next-intl';
import { useForm } from 'react-hook-form';
import { toast } from 'sonner';
import { z } from 'zod';

const FormSchema = z
  .object({
    password: z.string().min(6),
    repeat_password: z.string(),
  })
  .refine((data) => data.password === data.repeat_password, {
    message: 'passwordMismatch',
    path: ['repeat_password'],
  });

export default function ChangePassword() {
  const t = useTranslations('profile.accountSettings');
  const form = useForm<z.infer<typeof FormSchema>>({
    resolver: zodResolver(FormSchema),
  });

  async function onSubmit(data: z.infer<typeof FormSchema>) {
    await updateUserPassword({ password: data.password });
    toast.success(t('updateSuccess'));
    form.reset();
  }

  return (
    <Card className='min-w-80 rounded-[20px] border border-[#D9D9D9] p-6 text-[#666666] shadow-[0px_0px_52.6px_1px_rgba(15,44,83,0.05)]'>
      <div className={'mb-3'}>
        <div className={'text-xl font-bold'}>{t('accountSettings')}</div>
        <div className={'text-[15px] font-light'}>修改登录密码</div>
      </div>
      <Form {...form}>
        <form id='password-form' onSubmit={form.handleSubmit(onSubmit)} className='space-y-4'>
          <FormField
            control={form.control}
            name='password'
            render={({ field }) => (
              <FormItem>
                <FormControl>
                  <Input
                    className={
                      'h-[60px] rounded-[20px] text-xl shadow-[inset_0_0_7.6px_0_#00000040]'
                    }
                    type='password'
                    placeholder={t('newPassword')}
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name='repeat_password'
            render={({ field }) => (
              <FormItem>
                <FormControl>
                  <Input
                    className={
                      'h-[60px] rounded-[20px] text-xl shadow-[inset_0_0_7.6px_0_#00000040]'
                    }
                    type='password'
                    placeholder={t('repeatNewPassword')}
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
        </form>
      </Form>
      <div className={'mt-8 flex justify-center'}>
        <Button
          type='submit'
          size='sm'
          form='password-form'
          className={
            'h-full rounded-[50px] border-0 border-[#0F2C53] bg-[#0F2C53] px-[55px] py-[9px] text-xl font-bold text-white transition hover:bg-[#225BA9] hover:text-white'
          }
        >
          保存
        </Button>
      </div>
    </Card>
  );
}
