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

import { Link } from 'react-router'

import logo from '@/components/logo.svg'
import { Button } from '@/components/ui/button'
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { isValidCPF } from '@brazilian-utils/brazilian-utils'
import { zodResolver } from '@hookform/resolvers/zod'
import { useForm } from 'react-hook-form'
import { z } from 'zod'

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

      return onlyDigits.length === 11
        ? isValidCPF(val)
        : z.string().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="w-full max-w-xs grid gap-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 bg-lime-400 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>
    </>
  )
}
