// CoursePlanList：课程计划列表
// 依据：02-architecture-design.md §15 组件设计
// 每项：标题/描述/学科/班级/教师/起止日期/状态/章节数，点击跳转详情

"use client";

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

interface CoursePlanListProps {
  coursePlans: CoursePlan[];
}

type CoursePlanStatus = CoursePlan["status"];

const STATUS_LABELS: Record<CoursePlanStatus, string> = {
  active: "进行中",
  completed: "已完成",
  archived: "已归档",
};

const STATUS_STYLES: Record<CoursePlanStatus, string> = {
  active: "text-success",
  completed: "text-ink-muted",
  archived: "text-ink-muted",
};

export function CoursePlanList({
  coursePlans,
}: CoursePlanListProps): JSX.Element {
  if (coursePlans.length === 0) {
    return (
      <p className="rounded border border-rule bg-paper-elevated p-4 text-sm text-ink-muted">
        暂无课程计划
      </p>
    );
  }

  return (
    <ul
      className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3"
      aria-label="课程计划列表"
    >
      {coursePlans.map((plan) => (
        <li key={plan.id}>
          <CoursePlanCard plan={plan} />
        </li>
      ))}
    </ul>
  );
}

function CoursePlanCard({ plan }: { plan: CoursePlan }): JSX.Element {
  return (
    <Link
      href={`/parent/course-plans/${plan.id}`}
      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">
            <span className="rounded bg-accent-soft px-2 py-0.5 text-xs text-ink">
              {plan.subject}
            </span>
            <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>

        <p className="mt-2 text-sm text-ink-muted">{plan.description}</p>

        <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">{formatDate(plan.startDate)}</dd>
            </div>
            <div>
              <dt className="text-ink-muted">结束</dt>
              <dd className="mt-1">{formatDate(plan.endDate)}</dd>
            </div>
            <div>
              <dt className="text-ink-muted">章节数</dt>
              <dd className="mt-1 font-mono">{plan.chapterCount}</dd>
            </div>
          </dl>
        </div>

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

export default CoursePlanList;
