import { http } from './index';

// ==================== 课程类型 ====================

export interface CourseLesson {
  id: number;
  courseId: 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;
  assessmentData?: string;
  useTemplate: boolean;
  sortOrder: number;
  steps?: LessonStep[];
}

export interface LessonStep {
  id: number;
  lessonId: number;
  name: string;
  content?: string;
  duration: number;
  objective?: string;
  resourceIds?: string;
  sortOrder: number;
  stepResources?: StepResource[];
}

export interface StepResource {
  id: number;
  stepId: number;
  resourceId: number;
  sortOrder: number;
  resource: CourseResource;
}

export interface CourseResource {
  id: number;
  courseId: number;
  resourceType: string;
  resourceName: string;
  fileUrl: string;
  fileSize?: number;
  mimeType?: string;
}

export interface CreateLessonData {
  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;
  assessmentData?: string;
  useTemplate?: boolean;
}

export interface CreateStepData {
  name: string;
  content?: string;
  duration?: number;
  objective?: string;
  resourceIds?: number[];
}

// ==================== 超管端 API ====================

// 获取课程列表
export function getLessonList(courseId: number) {
  return http.get(`/v1/admin/courses/${courseId}/lessons`);
}

// 获取课程详情
export function getLessonDetail(courseId: number, lessonId: number) {
  return http.get(`/v1/admin/courses/${courseId}/lessons/${lessonId}`);
}

// 按类型获取课程
export function getLessonByType(courseId: number, lessonType: string) {
  return http.get(`/v1/admin/courses/${courseId}/lessons/type/${lessonType}`);
}

// 创建课程
export function createLesson(courseId: number, data: CreateLessonData) {
  return http.post(`/v1/admin/courses/${courseId}/lessons`, data);
}

// 更新课程
export function updateLesson(courseId: number, lessonId: number, data: Partial<CreateLessonData>) {
  return http.put(`/v1/admin/courses/${courseId}/lessons/${lessonId}`, data);
}

// 删除课程
export function deleteLesson(courseId: number, lessonId: number) {
  return http.delete(`/v1/admin/courses/${courseId}/lessons/${lessonId}`);
}

// 重新排序课程
export function reorderLessons(courseId: number, lessonIds: number[]) {
  return http.put(`/v1/admin/courses/${courseId}/lessons/reorder`, { lessonIds });
}

// ==================== 教学环节 API ====================

// 获取环节列表
export function getStepList(courseId: number, lessonId: number) {
  return http.get(`/v1/admin/courses/${courseId}/lessons/${lessonId}/steps`);
}

// 创建环节
export function createStep(courseId: number, lessonId: number, data: CreateStepData) {
  return http.post(`/v1/admin/courses/${courseId}/lessons/${lessonId}/steps`, data);
}

// 更新环节
export function updateStep(courseId: number, stepId: number, data: Partial<CreateStepData>) {
  return http.put(`/v1/admin/courses/${courseId}/lessons/steps/${stepId}`, data);
}

// 删除环节
export function deleteStep(courseId: number, _lessonId: number, stepId: number) {
  return http.delete(`/v1/admin/courses/${courseId}/lessons/steps/${stepId}`);
}

// 重新排序环节
export function reorderSteps(courseId: number, lessonId: number, stepIds: number[]) {
  return http.put(`/v1/admin/courses/${courseId}/lessons/${lessonId}/steps/reorder`, { stepIds });
}

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

// 获取教师端课程列表
export function getTeacherLessonList(courseId: number) {
  return http.get(`/teacher/courses/${courseId}/lessons`);
}

// 获取教师端课程详情
export function getTeacherLessonDetail(courseId: number, lessonId: number) {
  return http.get(`/teacher/courses/${courseId}/lessons/${lessonId}`);
}
