import { getTranslations } from "next-intl/server"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getStudentGradeSummary } from "@/modules/grades/data-access"
import { getRankingTrend, getClassAverageTrend } from "@/modules/grades/data-access-ranking"
import { getSubjectOptions } from "@/modules/school/data-access"
import { StudentGradeSummary } from "@/modules/grades/components/student-grade-summary"
import { GradeFilters } from "@/modules/grades/components/grade-filters"
import { GradeTrendCard } from "@/modules/grades/components/grade-trend-card"
import { RankingTrendCard } from "@/modules/grades/components/ranking-trend-card"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { UserX } from "lucide-react"
import { getParam, type SearchParams } from "@/shared/lib/search-params"

export const dynamic = "force-dynamic"

export default async function StudentGradesPage({
  searchParams,
}: {
  searchParams: Promise<SearchParams>
}) {
  const ctx = await requirePermission(Permissions.GRADE_RECORD_READ)
  const [sp, summary, rankingTrend, classAverageTrend, subjectOptions] = await Promise.all([
    searchParams,
    getStudentGradeSummary(ctx.userId, ctx.dataScope),
    // v3-P1-3：接入排名趋势图
    getRankingTrend(ctx.userId, undefined, undefined, ctx.dataScope),
    // v3-P2-2：接入班级平均趋势对比线
    getClassAverageTrend(ctx.userId, undefined, undefined, ctx.dataScope),
    // v3-P2-1：获取科目列表用于过滤器
    getSubjectOptions(),
  ])

  if (!summary) {
    const t = await getTranslations("grades")
    return (
      <div className="space-y-8">
        <div>
          <h2 className="text-2xl font-bold tracking-tight">{t("title.myGrades")}</h2>
          <p className="text-muted-foreground">{t("summary.noDataDescription")}</p>
        </div>
        <EmptyState
          title={t("summary.noDataTitle")}
          description={t("summary.noDataDescription")}
          icon={UserX}
          className="border-none shadow-none"
        />
      </div>
    )
  }

  // 应用筛选
  const q = (getParam(sp, "q") || "").toLowerCase().trim()
  const subjectFilter = getParam(sp, "subject") || "all"
  const typeFilter = getParam(sp, "type") || "all"
  const semesterFilter = getParam(sp, "semester") || "all"

  const filteredRecords = summary.records.filter((r) => {
    if (q && !r.title.toLowerCase().includes(q)) return false
    // v3-P2-1 修复：按 subjectId 而非 subjectName 过滤
    if (subjectFilter !== "all" && r.subjectId !== subjectFilter) return false
    if (typeFilter !== "all" && r.type !== typeFilter) return false
    if (semesterFilter !== "all" && r.semester !== semesterFilter) return false
    return true
  })

  const filteredSummary = {
    ...summary,
    records: filteredRecords,
  }

  return (
    <div className="space-y-8">
      <div>
        <h2 className="text-2xl font-bold tracking-tight">{summary.studentName}</h2>
        <p className="text-muted-foreground">{summary.records.length} 条成绩记录</p>
      </div>
      <GradeFilters subjects={subjectOptions.map((s) => ({ id: s.id, name: s.name }))} />
      {filteredSummary.records.length > 0 && (
        <div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
          <GradeTrendCard summary={filteredSummary} classAverageData={classAverageTrend} />
          <RankingTrendCard trend={rankingTrend} />
        </div>
      )}
      <StudentGradeSummary summary={filteredSummary} />
    </div>
  )
}
