import { Link } from "@tanstack/react-router";
import { AlertCircle } from "lucide-react";
import type { ReactNode } from "react";
import { TerraceMark, Wordmark } from "./brand";
import { DeveloperFooter } from "./developer-footer";

/** Split-screen layout for sign-in and account pages (frontend prompt §5.1.2). */
export function AuthLayout({
  title,
  subtitle,
  children,
  footer,
}: {
  title: string;
  subtitle?: ReactNode;
  children: ReactNode;
  footer?: ReactNode;
}) {
  return (
    <div className="grid min-h-screen lg:grid-cols-2">
      <div className="relative hidden flex-col justify-between overflow-hidden bg-(image:--gradient-hero) p-10 text-primary-foreground lg:flex">
        <TerraceMark className="absolute -bottom-24 -right-24 h-[480px] w-[480px] opacity-20" />
        <p className="font-display text-xl font-bold">PBPS MIS</p>
        <p className="max-w-md font-display text-3xl font-bold leading-tight">
          “Every trainee counted, every result verified.”
        </p>
        <p className="text-[13px] opacity-80">Monitoring &amp; Verification</p>
      </div>
      <div className="flex items-center justify-center px-4 py-10">
        <div className="w-full max-w-sm">
          <Link to="/">
            <Wordmark />
          </Link>
          <h1 className="mt-8 font-display text-2xl font-bold">{title}</h1>
          {subtitle && <p className="mt-1 text-muted-foreground">{subtitle}</p>}
          <div className="mt-6">{children}</div>
          {footer && <div className="mt-6 text-[13px]">{footer}</div>}
          <DeveloperFooter className="mt-10" />
        </div>
      </div>
    </div>
  );
}
export function AuthError({ message }: { message?: string | undefined }) {
  if (!message) return null;
  return (
    <p
      role="alert"
      className="mb-4 flex items-center gap-2 rounded-md bg-danger-soft px-3 py-2 text-[13px] text-danger"
    >
      <AlertCircle className="h-4 w-4 shrink-0" />
      {message}
    </p>
  );
}
/** Password rules shared by reset, invite and change (≥ 10 chars, not trivially common, not the email). */
export function passwordProblems(pw: string, email?: string): string[] {
  const out: string[] = [];
  if (pw.length < 10) out.push("At least 10 characters");
  if (/^(password|1234567890|qwertyuiop|pbps)/i.test(pw)) out.push("Not a common password");
  if (email && pw.toLowerCase().includes(email.split("@")[0]!.toLowerCase()))
    out.push("Must not contain your email name");
  if (!/[0-9]/.test(pw) || !/[a-zA-Z]/.test(pw)) out.push("Letters and at least one number");
  return out;
}
export function PasswordChecklist({ pw, email }: { pw: string; email?: string }) {
  const rules = [
    "At least 10 characters",
    "Letters and at least one number",
    "Not a common password",
    ...(email ? ["Must not contain your email name"] : []),
  ];
  const bad = passwordProblems(pw, email);
  return (
    <ul className="mt-2 space-y-0.5 text-[12px]">
      {rules.map((r) => (
        <li key={r} className={bad.includes(r) || !pw ? "text-muted-foreground" : "text-success"}>
          {bad.includes(r) || !pw ? "○" : "✓"} {r}
        </li>
      ))}
    </ul>
  );
}
