import type { Metadata } from "next"
import type { JSX } from "react"

import { getCoursePlans } from "@/modules/course-plans/data-access"
import { CoursePlanList } from "@/modules/course-plans/components/course-plan-list"
import { getSearchParam, type SearchParams } from "@/shared/lib/utils"
import type { CoursePlanStatus } from "@/modules/course-plans/types"

export const metadata: Metadata = {
  title: "课程计划 - Next_Edu",
  description: "管理课程教学计划与周课时安排",
}

export const dynamic = "force-dynamic"

const isValidStatus = (v?: string): v is CoursePlanStatus =>
  v === "planning" || v === "active" || v === "completed" || v === "paused"

export default async function AdminCoursePlansPage({
  searchParams,
}: {
  searchParams: Promise<SearchParams>
}): Promise<JSX.Element> {
  const sp = await searchParams
  const statusParam = getSearchParam(sp, "status")
  const status = isValidStatus(statusParam) ? statusParam : undefined

  const plans = await getCoursePlans({ status })

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div className="space-y-1">
        <h2 className="text-2xl font-bold tracking-tight">课程计划</h2>
        <p className="text-muted-foreground">
          管理课程教学计划与周课时安排。
        </p>
      </div>
      <CoursePlanList
        plans={plans}
        canManage
        createHref="/admin/course-plans/create"
        detailBaseHref="/admin/course-plans"
        initialStatus={status}
      />
    </div>
  )
}
