import { http } from './index';

// ==================== 类型定义 ====================

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;
  sourceCourse?: {
    id: number;
    name: string;
    coverImagePath?: string;
    description?: string;
  };
  lessons?: SchoolCourseLesson[];
}

export interface SchoolCourseLesson {
  id: number;
  schoolCourseId: number;
  sourceLessonId: number;
  lessonType: 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 ====================

// 获取校本课程包列表
export function getSchoolCourseList() {
  return http.get('/school/school-courses');
}

// 获取可创建校本课程包的源课程列表
export function getSourceCourses() {
  return http.get('/school/school-courses/source-courses');
}

// 获取校本课程包详情
export function getSchoolCourseDetail(id: number) {
  return http.get(`/school/school-courses/${id}`);
}

// 创建校本课程包
export function createSchoolCourse(data: CreateSchoolCourseData) {
  return http.post('/school/school-courses', data);
}

// 更新校本课程包
export function updateSchoolCourse(id: number, data: UpdateSchoolCourseData) {
  return http.put(`/school/school-courses/${id}`, data);
}

// 删除校本课程包
export function deleteSchoolCourse(id: number) {
  return http.delete(`/school/school-courses/${id}`);
}

// 获取校本课程列表
export function getSchoolCourseLessons(schoolCourseId: number) {
  return http.get(`/school/school-courses/${schoolCourseId}/lessons`);
}

// 更新校本课程
export function updateSchoolCourseLesson(
  schoolCourseId: number,
  lessonId: number,
  data: Partial<SchoolCourseLesson>,
) {
  return http.put(`/school/school-courses/${schoolCourseId}/lessons/${lessonId}`, data);
}

// 获取预约列表
export function getReservations(schoolCourseId: number) {
  return http.get(`/school/school-courses/${schoolCourseId}/reservations`);
}

// 创建预约
export function createReservation(schoolCourseId: number, data: CreateReservationData) {
  return http.post(`/school/school-courses/${schoolCourseId}/reservations`, data);
}

// 取消预约
export function cancelReservation(reservationId: number) {
  return http.post(`/school/school-courses/reservations/${reservationId}/cancel`);
}

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

// 获取教师端校本课程包列表
export function getTeacherSchoolCourseList() {
  return http.get('/teacher/school-courses');
}

// 获取教师端可创建校本课程包的源课程列表
export function getTeacherSourceCourses() {
  return http.get('/teacher/school-courses/source-courses');
}

// 获取教师端校本课程包详情
export function getTeacherSchoolCourseDetail(id: number) {
  return http.get(`/teacher/school-courses/${id}`);
}

// 创建教师端校本课程包
export function createTeacherSchoolCourse(data: CreateSchoolCourseData) {
  return http.post('/teacher/school-courses', data);
}

// 更新教师端校本课程包
export function updateTeacherSchoolCourse(id: number, data: UpdateSchoolCourseData) {
  return http.put(`/teacher/school-courses/${id}`, data);
}

// 删除教师端校本课程包
export function deleteTeacherSchoolCourse(id: number) {
  return http.delete(`/teacher/school-courses/${id}`);
}

// 获取教师端校本课程列表
export function getTeacherSchoolCourseLessons(schoolCourseId: number) {
  return http.get(`/teacher/school-courses/${schoolCourseId}/lessons`);
}

// 更新教师端校本课程
export function updateTeacherSchoolCourseLesson(
  schoolCourseId: number,
  lessonId: number,
  data: Partial<SchoolCourseLesson>,
) {
  return http.put(`/teacher/school-courses/${schoolCourseId}/lessons/${lessonId}`, data);
}
