// import { EyeIcon, EyeOffIcon } from 'lucide-react'
import { zodResolver } from '@hookform/resolvers/zod'
import { type Dispatch, type SetStateAction } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { z } from 'zod'

import { Button } from '~/components/ui/button'
import { Input } from '~/components/ui/input'

const loginSchema = z.object({
  email: z.string().email('Email tidak valid'),
  password: z.string().min(6, 'Kata sandi minimal 6 karakter'),
})

export type TLoginSchema = z.infer<typeof loginSchema>

type TProperties = {
  setIsRegisterOpen: Dispatch<SetStateAction<boolean>>
  setIsLoginOpen: Dispatch<SetStateAction<boolean>>
  setIsForgetOpen: Dispatch<SetStateAction<boolean>>
  setIsSuccessModalOpen: Dispatch<SetStateAction<boolean>>
}

export const FormLogin = (properties: TProperties) => {
  const {
    setIsRegisterOpen,
    setIsLoginOpen,
    setIsForgetOpen,
    setIsSuccessModalOpen,
  } = properties

  const formMethods = useForm<TLoginSchema>({
    resolver: zodResolver(loginSchema),
  })

  const { handleSubmit } = formMethods

  const onSubmit = handleSubmit((data) => {
    console.log('data', data) // eslint-disable-line no-console
    setIsSuccessModalOpen(true)
    setIsLoginOpen(false)
  })

  return (
    <div className="flex items-center justify-center">
      <div className="w-full max-w-md">
        <FormProvider {...formMethods}>
          <form
            onSubmit={onSubmit}
            className="space-y-4"
          >
            <Input
              id="email"
              label="Email / No Telepon"
              placeholder="Contoh: legal@legalgo.id"
              name="email"
            />

            <Input
              id="password"
              label="Kata Sandi"
              placeholder="Masukkan Kata Sandi"
              name="password"
              type="password"
            />

            {/* Lupa Kata Sandi */}
            <div className="flex items-center justify-between text-sm">
              <span className="text-gray-600">Lupa Kata Sandi?</span>
              <Button
                onClick={() => {
                  setIsLoginOpen(false)
                  setIsForgetOpen(true)
                }}
                className="font-semibold text-[#2E2F7C]"
                variant="link"
              >
                Reset Kata Sandi
              </Button>
            </div>

            {/* Tombol Masuk */}
            <Button
              type="submit"
              className="w-full rounded-md bg-[#2E2F7C] py-2 text-white transition hover:bg-blue-800"
            >
              Masuk
            </Button>
          </form>
        </FormProvider>

        {/* Link Daftar */}
        <div className="mt-4 text-center text-sm">
          Belum punya akun?{' '}
          <Button
            onClick={() => {
              setIsLoginOpen(false)
              setIsRegisterOpen(true)
            }}
            className="font-semibold text-[#2E2F7C]"
            variant="link"
            size="fit"
          >
            Daftar Disini
          </Button>
        </div>
      </div>
    </div>
  )
}
