import type { JSX } from "react"
import Link from "next/link"
import { PlusCircle, BarChart3, ClipboardList } from "lucide-react"
import { Button } from "@/shared/components/ui/button"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { ListPagination, computePagination, paginate } from "@/shared/components/ui/list-pagination"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getParam, type SearchParams } from "@/shared/lib/search-params"
import { getTeacherClasses } from "@/modules/classes/data-access"
import { getGradeRecords } from "@/modules/grades/data-access"
import { getSubjectOptions } from "@/modules/school/data-access"
import { GradeQueryFilters } from "@/modules/grades/components/grade-query-filters"
import { GradeRecordList } from "@/modules/grades/components/grade-record-list"
import { ExportButton } from "@/modules/grades/components/export-button"
import type { GradeRecordType, GradeRecordSemester } from "@/modules/grades/types"

export const dynamic = "force-dynamic"

const VALID_GRADE_TYPES: ReadonlySet<string> = new Set(["exam", "quiz", "homework", "other"])
const VALID_SEMESTERS: ReadonlySet<string> = new Set(["1", "2"])

function parseGradeType(v?: string): GradeRecordType | undefined {
  return v && VALID_GRADE_TYPES.has(v) ? (v as GradeRecordType) : undefined
}

function parseSemester(v?: string): GradeRecordSemester | undefined {
  return v && VALID_SEMESTERS.has(v) ? (v as GradeRecordSemester) : undefined
}

const PAGE_SIZE = 20

export default async function TeacherGradesPage({
  searchParams,
}: {
  searchParams: Promise<SearchParams>
}): Promise<JSX.Element> {
  const sp = await searchParams
  const ctx = await requirePermission(Permissions.GRADE_RECORD_READ)

  const classId = getParam(sp, "classId")
  const subjectId = getParam(sp, "subjectId")
  const type = getParam(sp, "type")
  const semester = getParam(sp, "semester")

  const [classes, allSubjects, records] = await Promise.all([
    getTeacherClasses(),
    getSubjectOptions(),
    getGradeRecords({
      scope: ctx.dataScope,
      currentUserId: ctx.userId,
      classId: classId && classId !== "all" ? classId : undefined,
      subjectId: subjectId && subjectId !== "all" ? subjectId : undefined,
      type: type && type !== "all" ? parseGradeType(type) : undefined,
      semester: semester && semester !== "all" ? parseSemester(semester) : undefined,
    }),
  ])

  const classOptions = classes.map((c) => ({ id: c.id, name: c.name }))
  const subjectOptions = allSubjects.map((s) => ({ id: s.id, name: s.name }))

  // 分页计算
  const { page } = computePagination(sp, PAGE_SIZE)
  const total = records.length
  const totalPages = Math.max(1, Math.ceil(total / PAGE_SIZE))
  const currentPage = Math.min(page, totalPages)
  const pagedRecords = paginate(records, currentPage, PAGE_SIZE)
  const hasFilters = Boolean(classId || subjectId || type || semester)

  return (
    <div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
      <div className="flex items-center justify-between space-y-2">
        <div>
          <h1 className="text-2xl font-bold tracking-tight">成绩记录</h1>
          <p className="text-muted-foreground">管理学生成绩记录。</p>
        </div>
        <div className="flex items-center gap-2">
          <Button asChild variant="outline">
            <Link href="/teacher/grades/stats">
              <BarChart3 className="mr-2 h-4 w-4" aria-hidden="true" />
              统计
            </Link>
          </Button>
          <Button asChild variant="outline">
            <Link href="/teacher/grades/entry">
              <ClipboardList className="mr-2 h-4 w-4" aria-hidden="true" />
              批量录入
            </Link>
          </Button>
          <ExportButton
            classId={classId && classId !== "all" ? classId : ""}
            subjectId={subjectId && subjectId !== "all" ? subjectId : undefined}
            variant="outline"
          />
          <Button asChild>
            <Link href="/teacher/grades/entry">
              <PlusCircle className="mr-2 h-4 w-4" aria-hidden="true" />
              录入成绩
            </Link>
          </Button>
        </div>
      </div>

      <GradeQueryFilters classes={classOptions} subjects={subjectOptions} />

      {records.length === 0 && !hasFilters ? (
        <EmptyState
          title="暂无成绩记录"
          description="开始为您的班级录入成绩。"
          icon={ClipboardList}
          action={{
            label: "录入成绩",
            href: "/teacher/grades/entry",
          }}
        />
      ) : (
        <div className="space-y-4">
          <GradeRecordList records={pagedRecords} />
          {total > 0 ? (
            <ListPagination
              page={currentPage}
              pageSize={PAGE_SIZE}
              total={total}
              totalPages={totalPages}
              basePath="/teacher/grades"
              searchParams={sp}
              itemLabel="条记录"
            />
          ) : null}
        </div>
      )}
    </div>
  )
}
