// 登录页（独立模式 / MF 未启用时使用）
// 依据：02-architecture-design.md §3.1 路由结构、ISSUE-004 登录走 REST
// MF 启用时由 Shell 提供登录；独立模式由本页提供
// mock 账号：parent@example.com / password

"use client";

import { useState, type FormEvent } from "react";
import { useRouter } from "next/navigation";
import { login } from "@/lib/auth";
import { cn } from "@/lib/utils";

export default function LoginPage() {
  const router = useRouter();
  const [email, setEmail] = useState("parent@example.com");
  const [password, setPassword] = useState("password");
  const [error, setError] = useState<string | null>(null);
  const [loading, setLoading] = useState(false);

  async function handleSubmit(e: FormEvent<HTMLFormElement>) {
    e.preventDefault();
    setError(null);
    setLoading(true);
    try {
      await login(email, password);
      router.push("/parent/dashboard");
    } catch (err) {
      setError(err instanceof Error ? err.message : "登录失败");
    } finally {
      setLoading(false);
    }
  }

  return (
    <main className="flex min-h-screen items-center justify-center px-4">
      <div className="w-full max-w-sm">
        <header className="mb-8 text-center">
          <h1 className="font-serif text-3xl">Edu 家长端</h1>
          <p className="mt-2 text-sm text-ink-muted">登录查看子女学习情况</p>
        </header>

        <form
          onSubmit={handleSubmit}
          className="space-y-4 rounded-lg border border-rule bg-paper-elevated p-6"
          aria-describedby={error ? "login-error" : undefined}
        >
          <div className="space-y-1">
            <label htmlFor="email" className="block text-sm font-medium">
              邮箱
            </label>
            <input
              id="email"
              type="email"
              required
              autoComplete="email"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              className="w-full rounded border border-rule bg-paper px-3 py-2 text-sm outline-none focus:border-accent"
            />
          </div>

          <div className="space-y-1">
            <label htmlFor="password" className="block text-sm font-medium">
              密码
            </label>
            <input
              id="password"
              type="password"
              required
              autoComplete="current-password"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              className="w-full rounded border border-rule bg-paper px-3 py-2 text-sm outline-none focus:border-accent"
            />
          </div>

          {error && (
            <p id="login-error" role="alert" className="text-sm text-danger">
              {error}
            </p>
          )}

          <button
            type="submit"
            disabled={loading}
            className={cn(
              "w-full rounded bg-accent px-4 py-2 text-sm font-medium text-paper",
              "transition-opacity hover:opacity-90",
              "disabled:cursor-not-allowed disabled:opacity-50",
            )}
          >
            {loading ? "登录中..." : "登录"}
          </button>
        </form>

        <p className="mt-4 text-center text-xs text-ink-muted">
          mock 账号：parent@example.com / password
        </p>
      </div>
    </main>
  );
}
