import { zodResolver } from '@hookform/resolvers/zod'
import { useEffect, useState } from 'react'
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, 'Kata sandi 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 [error, setError] = useState<string>()
  const [disabled, setDisabled] = useState(false)

  const { handleSubmit } = formMethods

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

  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"
              />

              {error && (
                <div className="text-sm text-red-500 capitalize">{error}</div>
              )}

              {/* Lupa Kata Sandi */}
              <div className="mb-4 flex justify-between">
                <span className="text-gray-600">Lupa Kata Sandi?</span>
                <Link
                  to="/lg-admin/auth/reset-password"
                  className="font-semibold text-[#2E2F7C]"
                >
                  Reset Kata Sandi
                </Link>
              </div>

              <Button
                disabled={disabled}
                type="submit"
                className="w-full rounded-md bg-[#2E2F7C] py-2 text-white transition hover:bg-blue-800"
              >
                Masuk
              </Button>
            </fetcher.Form>
          </RemixFormProvider>
        </div>
      </div>
    </div>
  )
}
