import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getStudentGradeSummary } from "@/modules/grades/data-access"
import { getClassAverageTrend } from "@/modules/grades/data-access-ranking"
import { getStudentGrowthArchive } from "@/modules/grades/data-access-analytics"
import { StudentGradeSummary } from "@/modules/grades/components/student-grade-summary"
import { GradeTrendCard } from "@/modules/grades/components/grade-trend-card"
import { GrowthArchiveChart } from "@/modules/grades/components/growth-archive-chart"
import {
  ParentChildrenDataPage,
  ParentNoChildrenPage,
} from "@/modules/parent/components/parent-children-data-page"
import { ParentExportButton } from "@/modules/parent/components/parent-export-button"
import { GraduationCap } from "lucide-react"
import type { ClassAverageTrendResult, StudentGrowthArchiveResult } from "@/modules/grades/types"
import { getTranslations } from "next-intl/server"

export const dynamic = "force-dynamic"

interface ChildGradeItem {
  studentId: string
  summary: NonNullable<Awaited<ReturnType<typeof getStudentGradeSummary>>>
  classAverageTrend: ClassAverageTrendResult | null
  growthArchive: StudentGrowthArchiveResult | null
}

export default async function ParentGradesPage() {
  const ctx = await requirePermission(Permissions.GRADE_RECORD_READ)
  const t = await getTranslations("grades")

  if (ctx.dataScope.type !== "children" || ctx.dataScope.childrenIds.length === 0) {
    return (
      <ParentNoChildrenPage
        title={t("parent.title")}
        description={t("parent.description")}
        icon={GraduationCap}
        emptyTitle={t("parent.noChildren")}
        emptyDescription={t("parent.noChildrenDesc")}
      />
    )
  }

  // 使用 allSettled 容错：单个子女查询失败不影响其他子女展示
  const results = await Promise.allSettled(
    ctx.dataScope.childrenIds.map(async (id) => {
      const [summary, classAverageTrend, growthArchive] = await Promise.all([
        getStudentGradeSummary(id, ctx.dataScope),
        // v3-P2-8：家长页面补齐趋势图，复用班级平均对比线
        getClassAverageTrend(id, undefined, undefined, ctx.dataScope),
        // P3-4：子女纵向成长档案
        getStudentGrowthArchive(id, ctx.dataScope),
      ])
      return { summary, classAverageTrend, growthArchive, studentId: id }
    }),
  )
  // P1-8 修复：用循环 + 类型守卫替代 `as` 断言
  const validItems: ChildGradeItem[] = []
  for (const r of results) {
    if (r.status !== "fulfilled") continue
    const { summary, classAverageTrend, growthArchive, studentId } = r.value
    if (summary === null) continue
    validItems.push({ studentId, summary, classAverageTrend, growthArchive })
  }

  return (
    <ParentChildrenDataPage
      title={t("parent.title")}
      description={t("parent.description")}
      icon={GraduationCap}
      noRecordsTitle={t("parent.noGrades")}
      noRecordsDescription={t("parent.noGradesDesc")}
      items={validItems}
      renderItem={({ studentId, summary, classAverageTrend, growthArchive }) => (
        <>
          <div className="flex items-center justify-between border-b pb-2">
            <h3 className="text-lg font-semibold">{summary.studentName}</h3>
            {/* v4-P1-12: 接入 exportGradesAction，支持按 studentId 导出 */}
            <ParentExportButton studentId={studentId} studentName={summary.studentName} />
          </div>
          {summary.records.length > 0 && (
            <GradeTrendCard summary={summary} classAverageData={classAverageTrend} />
          )}
          {/* P3-4：子女纵向成长档案 */}
          <GrowthArchiveChart data={growthArchive} />
          <StudentGradeSummary summary={summary} />
        </>
      )}
    />
  )
}
