import RecaptchaWidget from '@/components/common/recaptcha-widget';
import SocialLoginButtons from '@/components/common/social-login-buttons';
import InputError from '@/components/input-error';
import TextLink from '@/components/text-link';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import AuthLayout from '@/layouts/auth-layout';
import { register } from '@/routes';
import { store } from '@/routes/login';
import { request } from '@/routes/password';
import { Form, Head } from '@inertiajs/react';
import { CaretRightIcon } from '@phosphor-icons/react';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';

interface LoginProps {
  status?: string;
  canResetPassword: boolean;
  canRegister: boolean;
}

export default function Login({
  status,
  canResetPassword,
  canRegister,
}: LoginProps) {
  const { t } = useTranslation();
  const [recaptchaToken, setRecaptchaToken] = useState<string | null>(null);

  return (
    <AuthLayout
      title={t('Log in to your account')}
      description={t('Welcome back, sign in to access your account.')}
    >
      <Head title={t('Log in')} />

      <Form
        {...store.form()}
        resetOnSuccess={['password']}
        className="flex flex-col gap-6"
      >
        {({ processing, errors }) => (
          <>
            <div className="grid gap-6">
              <div className="grid gap-2">
                <Label
                  htmlFor="email"
                  className="px-1 text-black/70 dark:text-white/50"
                >
                  {t('Email address')}
                </Label>
                <Input
                  id="email"
                  type="email"
                  name="email"
                  required
                  autoFocus
                  tabIndex={1}
                  className="rounded-xl border border-black/5 bg-black/5 dark:border-white/5 dark:bg-secondary"
                  autoComplete="email"
                  placeholder="email@example.com"
                />
                <InputError message={errors.email} />
              </div>

              <div className="grid gap-2">
                <Label
                  htmlFor="password"
                  className="px-1 text-black/70 dark:text-white/50"
                >
                  {t('Password')}
                </Label>

                <Input
                  id="password"
                  type="password"
                  name="password"
                  required
                  tabIndex={2}
                  autoComplete="current-password"
                  placeholder={t('Password')}
                  className="rounded-xl border border-black/5 bg-black/5 dark:border-white/5 dark:bg-secondary"
                />
                <InputError message={errors.password} />
              </div>

              <div className="flex items-center justify-between">
                <div className="flex items-center space-x-2">
                  <Checkbox
                    id="remember"
                    name="remember"
                    className="h-5 w-5 border-black/10 bg-white/10 data-[state=checked]:border-[#47A300] data-[state=checked]:bg-[linear-gradient(0deg,#47A300_0%,#47A300_100%)] data-[state=checked]:text-white data-[state=checked]:shadow-button dark:border-white/10"
                    tabIndex={3}
                  />
                  <Label
                    htmlFor="remember"
                    className="cursor-pointer text-sm dark:text-white/50"
                  >
                    {t('Remember me')}
                  </Label>
                </div>

                {canResetPassword && (
                  <TextLink
                    href={request()}
                    className="text-semibold ml-auto text-sm font-bold text-primary"
                    tabIndex={5}
                  >
                    {t('Forgot password?')}
                  </TextLink>
                )}
              </div>

              <RecaptchaWidget onChange={setRecaptchaToken} />
              {recaptchaToken && (
                <input
                  type="hidden"
                  name="g-recaptcha-response"
                  value={recaptchaToken}
                />
              )}

              <Button
                type="submit"
                variant="primary"
                className="mt-4 w-full"
                tabIndex={4}
                loading={processing}
                disabled={processing}
              >
                <span>{t('Log in')}</span>
                <CaretRightIcon weight="bold" />
              </Button>
            </div>

            <SocialLoginButtons />

            {canRegister && (
              <div className="text-center text-sm text-black/50 dark:text-white/50">
                {t("Don't have an account?")}{' '}
                <TextLink
                  href={register()}
                  tabIndex={5}
                  className="font-semibold text-primary"
                >
                  {t('Sign up')}
                </TextLink>
              </div>
            )}
          </>
        )}
      </Form>

      {status && (
        <div className="mb-4 text-center text-sm font-medium text-success">
          {status}
        </div>
      )}
    </AuthLayout>
  );
}
