import { zodResolver } from '@hookform/resolvers/zod'
import { useEffect } from 'react'
import toast from 'react-hot-toast'
import { Link, useFetcher } from 'react-router'
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
import { z } from 'zod'

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

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

export type TLoginSchema = z.infer<typeof loginSchema>

export const AdminLoginPage = () => {
  const fetcher = useFetcher()
  const formMethods = useRemixForm<TLoginSchema>({
    mode: 'onSubmit',
    fetcher,
    resolver: zodResolver(loginSchema),
  })

  const { handleSubmit } = formMethods

  useEffect(() => {
    if (!fetcher.data?.success && fetcher.data?.message) {
      toast.error(fetcher.data.message)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [fetcher.data])

  return (
    <div className="flex min-h-dvh min-w-dvw flex-col items-center justify-center space-y-8">
      <div className="grid max-w-lg items-center justify-center space-y-7 rounded-[20px] border border-[#E6E6E6] bg-white p-8">
        <div className="flex flex-col items-center">
          <Link to="/">
            <img
              src={APP.logo}
              alt={APP.title}
              className="h-[80px]"
            />
          </Link>
        </div>
        <p className="text-center">
          Selamat Datang, silakan masukkan akun Anda untuk melanjutkan!
        </p>
        <div>
          <RemixFormProvider {...formMethods}>
            <fetcher.Form
              method="post"
              onSubmit={handleSubmit}
              className="space-y-4"
              action="/actions/admin/login"
            >
              <Input
                id="email"
                label="Email"
                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="mb-4 flex justify-between">
                <span className="text-gray-600">Lupa Kata Sandi?</span>
                <Button
                  as={Link}
                  variant={'link'}
                  size="fit"
                  to="/lg-admin/auth/reset-password"
                >
                  Reset Kata Sandi
                </Button>
              </div>

              <Button
                isLoading={fetcher.state !== 'idle'}
                disabled={fetcher.state !== 'idle'}
                type="submit"
                className="w-full rounded-md py-2"
              >
                Masuk
              </Button>
            </fetcher.Form>
          </RemixFormProvider>
        </div>
      </div>
    </div>
  )
}
