import { loginAdmin } from './actions';

const ERROR_MESSAGES: Record<string, string> = {
  invalid: 'Incorrect username or password.',
  rate_limited: 'Too many attempts — please wait a few minutes and try again.',
};

export default function AdminLoginPage({ searchParams }: { searchParams: { error?: string; next?: string } }) {
  const error = searchParams.error ? (ERROR_MESSAGES[searchParams.error] ?? 'Something went wrong.') : null;
  const next = searchParams.next ?? '/admin/orders';

  return (
    <div className="mx-auto max-w-md px-6 py-16">
      <h1 className="font-display text-3xl">Admin login</h1>

      {error && (
        <p className="mt-6 border border-splash-pink/40 bg-splash-pink/10 px-4 py-3 text-sm text-splash-pink">
          {error}
        </p>
      )}

      <form action={loginAdmin} className="mt-8 space-y-6">
        <input type="hidden" name="next" value={next} />
        <div>
          <label htmlFor="username" className="tag-label mb-2 block">
            Username
          </label>
          <input
            id="username"
            name="username"
            required
            autoFocus
            className="w-full border border-line bg-paper px-3 py-2 text-sm text-ink"
          />
        </div>
        <div>
          <label htmlFor="password" className="tag-label mb-2 block">
            Password
          </label>
          <input
            id="password"
            name="password"
            type="password"
            required
            className="w-full border border-line bg-paper px-3 py-2 text-sm text-ink"
          />
        </div>
        <button type="submit" className="bg-clay px-6 py-3 text-sm font-medium text-paper hover:bg-clay-dark">
          Log in
        </button>
      </form>
    </div>
  );
}
