import type { Route } from './+types'

import { isValidCPF } from '@brazilian-utils/brazilian-utils'
import { zodResolver } from '@hookform/resolvers/zod'
import { useForm } from 'react-hook-form'
import { Link } from 'react-router'
import { z } from 'zod'

import logo from '@repo/ui/components/logo2.svg'
import { Button } from '@repo/ui/components/ui/button'
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage
} from '@repo/ui/components/ui/form'
import { Input } from '@repo/ui/components/ui/input'

const schema = z.object({
  username: z
    .string()
    .trim()
    .nonempty('Digite seu Email ou CPF')
    .refine((val) => {
      const onlyDigits = val.replace(/\D/g, '')

      return onlyDigits.length === 11
        ? isValidCPF(val)
        : z.email().safeParse(val).success
    }, 'Deve ser um Email ou CPF válido')
})

type Schema = z.infer<typeof schema>

export function meta({}: Route.MetaArgs) {
  return [{ title: 'Redefinir senha · EDUSEG®' }]
}

export default function Forgot({}: Route.ComponentProps) {
  const form = useForm({
    resolver: zodResolver(schema)
  })
  const { control, handleSubmit, formState } = form

  const onSubmit = async (data: Schema) => {
    console.log(data)
  }

  return (
    <>
      <div className="space-y-6">
        <div className="flex justify-center">
          <div className="border border-white/15 bg-white/5 px-2.5 py-3 rounded-xl">
            <img src={logo} alt="EDUSEG®" className="block size-12" />
          </div>
        </div>

        <div className="text-center space-y-1.5">
          <h1 className="text-2xl font-semibold font-display text-balance">
            Redefinir senha
          </h1>
          <p className="text-white/50 text-sm">
            Digite seu email e lhe enviaremos um email com as instruções para
            redefinir sua senha.
          </p>
        </div>

        <Form {...form}>
          <form onSubmit={handleSubmit(onSubmit)} className="grid gap-6">
            <FormField
              control={control}
              name="username"
              defaultValue=""
              render={({ field }) => (
                <FormItem>
                  <FormLabel>Email ou CPF</FormLabel>
                  <FormControl>
                    <Input
                      autoFocus={true}
                      placeholder="seu@email.com"
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />

            <Button
              type="submit"
              className="w-full cursor-pointer"
              disabled={formState.isSubmitting}
            >
              Enviar instruções
            </Button>
          </form>
        </Form>

        <p className="text-white/50 text-xs text-center">
          Lembrou da senha?{' '}
          <Link to="/" className="underline hover:no-underline">
            Faça login
          </Link>
          .
        </p>
      </div>
    </>
  )
}
