import "server-only"

import { getTranslations } from "next-intl/server"

import { exportToExcel } from "@/shared/lib/excel"

import { getElectiveCourses } from "./data-access"
import { getCourseSelections } from "./data-access-selections"

/**
 * 导出选修课课程列表到 Excel
 * Sheet 1: 课程明细
 */
export async function exportElectiveCoursesToExcel(params: {
  status?: string
  teacherId?: string
}): Promise<Buffer> {
  const t = await getTranslations("elective")

  const courses = await getElectiveCourses({
    status: params.status as "draft" | "open" | "closed" | "cancelled" | undefined,
    teacherId: params.teacherId,
  })

  const rows = courses.map((c) => ({
    [t("fields.name")]: c.name,
    [t("fields.teacher")]: c.teacherName ?? "",
    [t("fields.subject")]: c.subjectName ?? "",
    [t("fields.grade")]: c.gradeName ?? "",
    [t("fields.capacity")]: c.capacity,
    [t("fields.enrolled")]: c.enrolledCount,
    [t("fields.classroom")]: c.classroom ?? "",
    [t("fields.schedule")]: c.schedule ?? "",
    [t("fields.credit")]: c.credit,
    [t("fields.selectionMode")]: t(`selectionMode.${c.selectionMode}`),
    status: t(`status.${c.status}`),
    [t("fields.startDate")]: c.startDate ?? "",
    [t("fields.endDate")]: c.endDate ?? "",
  }))

  return exportToExcel({
    sheets: [
      {
        name: t("title.adminList"),
        columns: [
          { header: t("fields.name"), key: t("fields.name"), width: 24 },
          { header: t("fields.teacher"), key: t("fields.teacher"), width: 16 },
          { header: t("fields.subject"), key: t("fields.subject"), width: 14 },
          { header: t("fields.grade"), key: t("fields.grade"), width: 12 },
          { header: t("fields.capacity"), key: t("fields.capacity"), width: 10 },
          { header: t("fields.enrolled"), key: t("fields.enrolled"), width: 10 },
          { header: t("fields.classroom"), key: t("fields.classroom"), width: 14 },
          { header: t("fields.schedule"), key: t("fields.schedule"), width: 20 },
          { header: t("fields.credit"), key: t("fields.credit"), width: 8 },
          { header: t("fields.selectionMode"), key: t("fields.selectionMode"), width: 16 },
          { header: "Status", key: "status", width: 12 },
          { header: t("fields.startDate"), key: t("fields.startDate"), width: 14 },
          { header: t("fields.endDate"), key: t("fields.endDate"), width: 14 },
        ],
        rows,
      },
    ],
  })
}

/**
 * 导出课程选课名单到 Excel
 * Sheet 1: 选课名单
 */
export async function exportCourseSelectionsToExcel(params: {
  courseId: string
}): Promise<Buffer> {
  const t = await getTranslations("elective")

  const selections = await getCourseSelections(params.courseId)

  const rows = selections.map((s, idx) => ({
    "#": idx + 1,
    [t("fields.name")]: s.studentName ?? "",
    status: t(`selectionStatus.${s.status}`),
    priority: s.priority ?? 1,
    selectedAt: s.selectedAt.split("T")[0],
    enrolledAt: s.enrolledAt ? s.enrolledAt.split("T")[0] : "",
  }))

  return exportToExcel({
    sheets: [
      {
        name: t("student.mySelections"),
        columns: [
          { header: "#", key: "#", width: 6 },
          { header: t("fields.name"), key: t("fields.name"), width: 18 },
          { header: "Status", key: "status", width: 12 },
          { header: "Priority", key: "priority", width: 10 },
          { header: "Selected At", key: "selectedAt", width: 14 },
          { header: "Enrolled At", key: "enrolledAt", width: 14 },
        ],
        rows,
      },
    ],
  })
}
