import { getReadingPlatformAPI } from './generated';

// ============= 类型定义（保持向后兼容） =============

export interface SchoolCourse {
  id: number;
  tenantId: number;
  sourceCourseId: number;
  name: string;
  description?: string;
  createdBy: number;
  changesSummary?: string;
  usageCount: number;
  status: string;
  createdAt: string;
  updatedAt?: string;
  // 保存位置和审核状态
  saveLocation?: string;
  reviewStatus?: string;
  reviewedBy?: number;
  reviewedAt?: string;
  reviewComment?: string;
  // 课程配置字段
  themeId?: number;
  gradeTags?: string;
  domainTags?: string;
  duration?: number;
  coverImagePath?: string;
  coreContent?: string;
  introSummary?: string;
  introHighlights?: string;
  introGoals?: string;
  introSchedule?: string;
  introKeyPoints?: string;
  introMethods?: string;
  introEvaluation?: string;
  introNotes?: string;
  scheduleRefData?: string;
  environmentConstruction?: string;
  sourceCourse?: {
    id: number;
    name: string;
    coverImagePath?: string;
    description?: string;
  };
  lessons?: SchoolCourseLesson[];
}

export interface SchoolCourseLesson {
  id: number;
  schoolCourseId: number;
  sourceLessonId: number;
  lessonType: string;
  // 基本信息字段
  name: string;
  description?: string;
  duration: number;
  // 资源文件
  videoPath?: string;
  videoName?: string;
  pptPath?: string;
  pptName?: string;
  pdfPath?: string;
  pdfName?: string;
  // 课程配置
  objectives?: string;
  preparation?: string;
  extension?: string;
  reflection?: string;
  changeNote?: string;
  stepsData?: string;
}

export interface SchoolCourseReservation {
  id: number;
  schoolCourseId: number;
  teacherId: number;
  classId: number;
  scheduledDate: string;
  scheduledTime?: string;
  status: string;
  note?: string;
  createdAt: string;
}

export interface CreateSchoolCourseData {
  sourceCourseId: number;
  name: string;
  description?: string;
  changesSummary?: string;
}

export interface UpdateSchoolCourseData {
  name?: string;
  description?: string;
  changesSummary?: string;
  status?: string;
}

export interface CreateReservationData {
  teacherId: number;
  classId: number;
  scheduledDate: string;
  scheduledTime?: string;
  note?: string;
}

// ============= API 客户端实例 =============

const api = getReadingPlatformAPI();

// ==================== 学校端 API ====================

// 获取校本课程包列表（分页）
export function getSchoolCourseList(params?: {
  pageNum?: number;
  pageSize?: number;
  keyword?: string;
  grade?: string;
  domain?: string;
  lessonType?: string;
}) {
  return api.getSchoolCourses(params) as any;
}

// 获取校本课程包详情
export function getSchoolCourseDetail(id: number) {
  return api.getSchoolCourse(id) as any;
}

// 创建校本课程包
export function createSchoolCourse(data: CreateSchoolCourseData) {
  return api.schoolCourseControllerCreate(data) as any;
}

// 更新校本课程包
export function updateSchoolCourse(id: number, data: UpdateSchoolCourseData) {
  return api.schoolCourseControllerUpdate(id, data) as any;
}

// 删除校本课程包
export function deleteSchoolCourse(id: number) {
  return api.schoolCourseControllerRemove(id) as any;
}

// 获取校本课程列表
export function getSchoolCourseLessons(schoolCourseId: number) {
  return api.schoolCourseControllerFindLessons(schoolCourseId) as any;
}

// 更新校本课程
export function updateSchoolCourseLesson(
  schoolCourseId: number,
  lessonId: number,
  data: Partial<SchoolCourseLesson>,
) {
  return api.schoolCourseControllerUpdateLesson(schoolCourseId, lessonId, data) as any;
}

// 获取预约列表
export function getReservations(schoolCourseId: number) {
  return api.schoolCourseControllerFindReservations(schoolCourseId) as any;
}

// 创建预约
export function createReservation(schoolCourseId: number, data: CreateReservationData) {
  return api.schoolCourseControllerCreateReservation(schoolCourseId, data) as any;
}

// 取消预约
export function cancelReservation(reservationId: number) {
  return api.schoolCourseControllerCancelReservation(reservationId) as any;
}

// ==================== 教师端 API ====================

// 获取教师端校本课程列表
export function getTeacherSchoolCourseList() {
  return api.teacherSchoolCourseControllerFindAll() as any;
}

// 获取教师端可创建校本课程包的源课程列表
export function getTeacherSourceCourses() {
  return api.teacherSchoolCourseControllerGetSourceCourses() as any;
}

// 获取学校端可创建校本课程包的源课程列表
export function getSourceCourses() {
  return api.schoolCourseControllerGetSourceCourses() as any;
}

// 获取教师端校本课程包详情
export function getTeacherSchoolCourseDetail(id: number) {
  return api.teacherSchoolCourseControllerFindOne(id) as any;
}

// 创建教师端校本课程包
export function createTeacherSchoolCourse(data: CreateSchoolCourseData) {
  return api.teacherSchoolCourseControllerCreate(data) as any;
}

// 更新教师端校本课程包
export function updateTeacherSchoolCourse(id: number, data: UpdateSchoolCourseData) {
  return api.teacherSchoolCourseControllerUpdate(id, data) as any;
}

// 删除教师端校本课程包
export function deleteTeacherSchoolCourse(id: number) {
  return api.teacherSchoolCourseControllerRemove(id) as any;
}

// 获取教师端校本课程列表
export function getTeacherSchoolCourseLessons(schoolCourseId: number) {
  return api.teacherSchoolCourseControllerFindLessons(schoolCourseId) as any;
}

// 更新教师端校本课程
export function updateTeacherSchoolCourseLesson(
  schoolCourseId: number,
  lessonId: number,
  data: Partial<SchoolCourseLesson>,
) {
  return api.teacherSchoolCourseControllerUpdateLesson(schoolCourseId, lessonId, data) as any;
}

// ==================== 新增：完整数据API ====================

// 从源课程包创建校本课程包
export function createSchoolCourseFromSource(sourceCourseId: number, saveLocation: 'PERSONAL' | 'SCHOOL') {
  return api.schoolCourseControllerCreateFromSource({ sourceCourseId, saveLocation }) as any;
}

export function createTeacherSchoolCourseFromSource(sourceCourseId: number, saveLocation: 'PERSONAL' | 'SCHOOL') {
  return api.teacherSchoolCourseControllerCreateFromSource({ sourceCourseId, saveLocation }) as any;
}

// 获取校本课程包完整详情（含课程配置）
export function getSchoolCourseFullDetail(id: number) {
  return api.schoolCourseControllerGetFullDetail(id) as any;
}

export function getTeacherSchoolCourseFullDetail(id: number | string) {
  return api.teacherSchoolCourseControllerGetFullDetail(id as any) as any;
}

// 更新校本课程包完整数据
export function updateSchoolCourseFull(id: number, data: any) {
  return api.schoolCourseControllerUpdateFull(id, data) as any;
}

export function updateTeacherSchoolCourseFull(id: number, data: any) {
  return api.teacherSchoolCourseControllerUpdateFull(id, data) as any;
}

// ==================== 审核API（仅学校端）====================

// 审核通过
export function approveSchoolCourse(id: number, comment?: string) {
  return api.schoolCourseControllerApprove(id, { comment }) as any;
}

// 审核驳回
export function rejectSchoolCourse(id: number, comment: string) {
  return api.schoolCourseControllerReject(id, { comment }) as any;
}
