// ChildSummaryCard：子女概览卡片
// 依据：02-architecture-design.md §15.4 ChildSummaryCard 设计
// - 平均分 / 班级排名 / 出勤率 / 待完成作业
// - 成绩趋势指示
// - 即将到来的事件

"use client";

import type { ChildSummary } from "@/types";
import { formatPercent, formatNumber, formatDate } from "@/lib/utils";
import { cn } from "@/lib/utils";

interface ChildSummaryCardProps {
  summary: ChildSummary;
}

const TREND_LABELS = {
  up: { text: "上升", color: "text-success" },
  down: { text: "下降", color: "text-danger" },
  stable: { text: "稳定", color: "text-ink-muted" },
} as const;

const DEFAULT_TREND = { text: "稳定", color: "text-ink-muted" };

export function ChildSummaryCard({ summary }: ChildSummaryCardProps) {
  const trend = TREND_LABELS[summary.recentGradeTrend] ?? DEFAULT_TREND;

  return (
    <div className="space-y-4">
      {/* 指标网格 */}
      <div className="grid grid-cols-2 gap-4 lg:grid-cols-4">
        <Metric label="平均分" value={formatNumber(summary.avgScore)} />
        <Metric
          label="班级排名"
          value={`${summary.classRank} / ${summary.classSize}`}
        />
        <Metric label="出勤率" value={formatPercent(summary.attendanceRate)} />
        <Metric
          label="待完成作业"
          value={formatNumber(summary.pendingHomeworkCount)}
          highlight={summary.pendingHomeworkCount > 0}
        />
      </div>

      {/* 成绩趋势 */}
      <div className="rounded border border-rule bg-paper-elevated p-4">
        <div className="flex items-center justify-between">
          <h3 className="font-serif text-base">成绩趋势</h3>
          <span className={cn("text-sm", trend.color)}>{trend.text}</span>
        </div>
        <ul className="mt-3 space-y-2">
          {summary.recentScores.slice(0, 5).map((score) => (
            <li
              key={score.examId}
              className="flex items-center justify-between border-b border-rule pb-2 text-sm last:border-b-0 last:pb-0"
            >
              <div>
                <span className="font-medium">{score.subject}</span>
                <span className="ml-2 text-ink-muted">{score.examName}</span>
              </div>
              <div className="flex items-center gap-3">
                <span className="font-mono">{score.studentScore}</span>
                {score.classAverage !== undefined && (
                  <span className="text-xs text-ink-muted">
                    班均 {score.classAverage}
                  </span>
                )}
              </div>
            </li>
          ))}
        </ul>
      </div>

      {/* 即将到来事件 */}
      {summary.upcomingEvents && summary.upcomingEvents.length > 0 && (
        <div className="rounded border border-rule bg-paper-elevated p-4">
          <h3 className="font-serif text-base">即将到来</h3>
          <ul className="mt-3 space-y-2">
            {summary.upcomingEvents.map((event) => (
              <li
                key={event.id}
                className="flex items-center justify-between text-sm"
              >
                <span className="mark-left">{event.title}</span>
                <span className="text-ink-muted">
                  {formatDate(event.dueDate)}
                </span>
              </li>
            ))}
          </ul>
        </div>
      )}
    </div>
  );
}

function Metric({
  label,
  value,
  highlight,
}: {
  label: string;
  value: string;
  highlight?: boolean;
}) {
  return (
    <div
      className={cn(
        "rounded border border-rule bg-paper-elevated p-4",
        highlight && "border-warning",
      )}
    >
      <p className="text-xs text-ink-muted">{label}</p>
      <p className="mt-1 font-serif text-2xl">{value}</p>
    </div>
  );
}

export default ChildSummaryCard;
