// LessonPlanList：备课列表（只读）
// 依据：02-architecture-design.md §15 组件设计
// 每项：标题/学科/班级/教师/教材/章节/状态/预计时长，点击跳转只读详情

"use client";

import Link from "next/link";
import type { LessonPlan } from "@/types";
import { cn, formatDateTime } from "@/lib/utils";

interface LessonPlanListProps {
  lessonPlans: LessonPlan[];
}

type LessonPlanStatus = LessonPlan["status"];

const STATUS_LABELS: Record<LessonPlanStatus, string> = {
  draft: "草稿",
  published: "已发布",
  archived: "已归档",
};

const STATUS_STYLES: Record<LessonPlanStatus, string> = {
  draft: "text-ink-muted",
  published: "text-success",
  archived: "text-ink-muted",
};

export function LessonPlanList({
  lessonPlans,
}: LessonPlanListProps): JSX.Element {
  if (lessonPlans.length === 0) {
    return (
      <p className="rounded border border-rule bg-paper-elevated p-4 text-sm text-ink-muted">
        暂无备课记录
      </p>
    );
  }

  return (
    <ul className="space-y-3" aria-label="备课列表">
      {lessonPlans.map((plan) => (
        <li key={plan.id}>
          <LessonPlanCard plan={plan} />
        </li>
      ))}
    </ul>
  );
}

function LessonPlanCard({ plan }: { plan: LessonPlan }): JSX.Element {
  return (
    <Link
      href={`/parent/lesson-plans/${plan.id}/view`}
      className="block h-full focus-visible:outline-2"
      aria-label={`${plan.title} - 查看备课`}
    >
      <article
        className="h-full rounded border border-rule bg-paper-elevated p-4 transition-shadow hover:shadow-md"
        aria-label={`${plan.title} - ${STATUS_LABELS[plan.status]}`}
      >
        <div className="flex items-start justify-between gap-3">
          <div className="min-w-0 flex-1">
            <div className="flex items-center gap-2">
              <span className="rounded bg-accent-soft px-2 py-0.5 text-xs text-ink">
                {plan.subject}
              </span>
              <span className="text-xs text-ink-muted">
                预计 {plan.estimatedMinutes} 分钟
              </span>
            </div>
            <h3 className="mt-2 font-serif text-base">{plan.title}</h3>
          </div>
          <span
            className={cn(
              "flex-shrink-0 text-xs font-medium",
              STATUS_STYLES[plan.status],
            )}
          >
            {STATUS_LABELS[plan.status]}
          </span>
        </div>

        <div className="mt-3 border-t border-rule pt-3">
          <dl className="grid grid-cols-2 gap-y-2 text-xs">
            <div>
              <dt className="text-ink-muted">班级</dt>
              <dd className="mt-1">{plan.className}</dd>
            </div>
            <div>
              <dt className="text-ink-muted">教师</dt>
              <dd className="mt-1">{plan.teacherName}</dd>
            </div>
            <div>
              <dt className="text-ink-muted">教材</dt>
              <dd className="mt-1">{plan.textbookTitle}</dd>
            </div>
            <div>
              <dt className="text-ink-muted">章节</dt>
              <dd className="mt-1">{plan.chapterTitle}</dd>
            </div>
            {plan.publishedAt && (
              <div>
                <dt className="text-ink-muted">发布时间</dt>
                <dd className="mt-1">{formatDateTime(plan.publishedAt)}</dd>
              </div>
            )}
          </dl>
        </div>

        <p className="mt-3 text-xs text-accent">查看备课 →</p>
      </article>
    </Link>
  );
}

export default LessonPlanList;
