"use client";

import { useState } from "react";
import { login } from "@/lib/api";
import { useLocale } from "@/i18n/LocaleProvider";
import { Button, Field } from "@/components/ui";

/**
 * Staff sign-in, the split-screen layout from the prototype (`screenshots/img.png`): form on one
 * side, brand panel on the other, dark-first.
 *
 * Renders instead of the dashboard whenever there's no session, so there is no way to reach an
 * ops screen unauthenticated even by deep link.
 */
export function LoginScreen({ onSignedIn }: { onSignedIn: () => void }) {
  const { t, locale } = useLocale();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState<string | null>(null);
  const [busy, setBusy] = useState(false);

  const submit = async (e: React.FormEvent) => {
    e.preventDefault();
    setBusy(true);
    setError(null);
    try {
      await login(email, password);
      onSignedIn();
    } catch (err) {
      // The server deliberately doesn't say which field was wrong, don't invent detail here.
      setError(
        err instanceof Error && err.message
          ? err.message
          : t("login.failed"),
      );
    } finally {
      setBusy(false);
    }
  };

  return (
    <div className="grid min-h-screen bg-background lg:grid-cols-2">
      {/* Form side */}
      <div className="flex items-center justify-center p-6">
        <form onSubmit={submit} className="w-full max-w-sm">
          <h1 className="text-2xl font-bold tracking-tight">{t("login.title")}</h1>
          <p className="mt-2 text-sm text-muted">{t("login.subtitle")}</p>

          <div className="mt-8 space-y-4">
            <Field
              label={t("login.email")}
              type="email"
              autoComplete="username"
              required
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              placeholder="admin@beebbeeb.sa"
            />
            <Field
              label={t("login.password")}
              type="password"
              autoComplete="current-password"
              required
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              placeholder="••••••••••"
            />
          </div>

          {error && (
            <div className="tint tone-danger mt-4 rounded-card border p-3 text-sm">
              {error}
            </div>
          )}

          <Button
            variant="primary"
            type="submit"
            disabled={busy || !email || !password}
            className="mt-6 w-full"
          >
            {busy ? t("common.loading") : t("login.submit")}
          </Button>
        </form>
      </div>

      {/* Brand side. Hidden on small screens, it's atmosphere, not information. */}
      <div className="relative hidden overflow-hidden bg-canvas lg:block">
        <div
          aria-hidden
          className="absolute inset-0 opacity-40"
          style={{
            background:
              "radial-gradient(120% 90% at 80% 10%, rgba(252,215,4,0.22), transparent 60%)",
          }}
        />
        <div className="relative flex h-full flex-col justify-center gap-6 p-14">
          <img src="/beeb-logo.png" alt="" className="h-16 w-16 rounded-full" />
          <h2 className="max-w-md text-4xl font-bold leading-tight">
            {t("login.panelTitle")}
          </h2>
          <p className="max-w-sm text-muted">{t("login.panelBody")}</p>
        </div>
      </div>

      <p className="pointer-events-none absolute bottom-4 w-full text-center text-xs text-muted-dim">
        BEEBBEEB © {new Date().getFullYear()} · {locale.toUpperCase()}
      </p>
    </div>
  );
}
