/* eslint-disable @typescript-eslint/no-explicit-any */
"use client";

import { useState } from "react";

import { Head, useForm } from "@inertiajs/react";

import InputError from "@/components/input-error";
import TextLink from "@/components/text-link";

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

import { Eye, EyeOff, LoaderCircle, ShieldCheck } from "lucide-react";

import {usePage} from "@inertiajs/react";

import styles from "./admin-login.module.scss";

interface LoginProps {
  status?: string;
}

export default function AdminLogin({ status }: LoginProps) {
  const [showPassword, setShowPassword] = useState(false);
  const { appSettings } =
    usePage().props as unknown as any

  const { data, setData, post, processing, errors, reset } = useForm({
    email: "",
    password: "",
  });

  const submit = (e: React.FormEvent) => {
    e.preventDefault();

    post("/admin/login", {
      onFinish: () => reset("password"),
    });
  };

  return (
    <>
      <Head title="Administrator Login" />

      <div className={styles.login}>
        {/* ========================================
            HEADER
        ======================================== */}

        <header className={styles.header}>
          <div className={styles.headerInner}>
            <TextLink href="/">
              <img
                src={appSettings?.website_logo}
                alt="TraderPlus"
                className={styles.logo}
              />
            </TextLink>

            <TextLink href="/">
              Return to Website
            </TextLink>
          </div>
        </header>

        {/* ========================================
            CONTENT
        ======================================== */}

        <main className={styles.content}>
          <div className={styles.wrapper}>
            {/* COPY */}

            <div className={styles.copy}>
              <div className={styles.adminBadge}>
                <ShieldCheck size={15} />
                <span>ADMIN PORTAL</span>
              </div>

              <h1>Administrator Login</h1>

              <p>
                Access admin features and manage the entire platform seamlessly from one dashboard.
              </p>
            </div>

            {/* FORM */}

            <form
              onSubmit={submit}
              className={styles.form}
            >
              <input
                type="text"
                name="fakeusernameremembered"
                style={{ display: "none" }}
              />

              {/* EMAIL */}

              <div className={styles.field}>
                <Label htmlFor="email">
                  Email Address
                </Label>

                <Input
                  id="email"
                  type="email"
                  placeholder="admin@traderplus.com"
                  value={data.email}
                  onChange={(e) =>
                    setData("email", e.target.value)
                  }
                  autoComplete="email"
                  autoFocus
                  required
                />

                <InputError
                  message={errors.email}
                />
              </div>

              {/* PASSWORD */}

              <div className={styles.field}>
                <div
                  className={styles.passwordHeader}
                >
                  <Label htmlFor="password">
                    Password
                  </Label>
                </div>

                <div
                  className={styles.passwordWrap}
                >
                  <Input
                    id="password"
                    type={
                      showPassword
                        ? "text"
                        : "password"
                    }
                    placeholder="Password"
                    value={data.password}
                    onChange={(e) =>
                      setData(
                        "password",
                        e.target.value
                      )
                    }
                    autoComplete="current-password"
                    required
                  />

                  <span
                    className={styles.toggle}
                    onClick={() =>
                      setShowPassword(
                        !showPassword
                      )
                    }
                  >
                    {showPassword ? (
                      <EyeOff size={18} />
                    ) : (
                      <Eye size={18} />
                    )}
                  </span>
                </div>

                <InputError
                  message={errors.password}
                />
              </div>

              {/* SUBMIT */}

              <Button
                type="submit"
                disabled={processing}
                className={styles.submitBtn}
              >
                {processing && (
                  <LoaderCircle className="h-4 w-4 animate-spin" />
                )}

                Sign In to Admin Panel
              </Button>

              {/* FOOTER */}

              <div className={styles.footerText}>
                Restricted to authorized
                administrators only.
              </div>
            </form>

            {status && (
              <div className={styles.status}>
                {status}
              </div>
            )}
          </div>
        </main>
      </div>
    </>
  );
}