import { http } from './index';

export interface CourseQueryParams {
  page?: number;
  pageSize?: number;
  grade?: string;
  status?: string;
  keyword?: string;
}

export interface Course {
  id: number;
  name: string;
  description?: string;
  pictureBookName?: string;
  grades: string[];
  status: string;
  version: string;
  usageCount: number;
  teacherCount: number;
  avgRating: number;
  createdAt: Date;
  updatedAt: Date;
  submittedAt?: Date;
  reviewedAt?: Date;
  reviewComment?: string;
  // 新增字段
  themeId?: number;
  theme?: { id: number; name: string };
  coreContent?: string;
  coverImagePath?: string;
  domainTags?: string[];
  gradeTags?: string[];
  duration?: number;
  // 课程介绍字段
  introSummary?: string;
  introHighlights?: string;
  introGoals?: string;
  introSchedule?: string;
  introKeyPoints?: string;
  introMethods?: string;
  introEvaluation?: string;
  introNotes?: string;
  // 排课计划参考
  scheduleRefData?: string;
  // 环创建设
  environmentConstruction?: string;
  // 关联课程
  courseLessons?: CourseLesson[];
}

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;
}

export interface ValidationResult {
  valid: boolean;
  errors: ValidationError[];
  warnings: ValidationWarning[];
}

export interface ValidationError {
  field: string;
  message: string;
  code: string;
}

export interface ValidationWarning {
  field: string;
  message: string;
  code: string;
}

// 获取课程包列表
export function getCourses(params: CourseQueryParams): Promise<{
  items: Course[];
  total: number;
  page: number;
  pageSize: number;
}> {
  return http.get('/courses', { params });
}

// 获取审核列表
export function getReviewList(params: CourseQueryParams): Promise<{
  items: Course[];
  total: number;
  page: number;
  pageSize: number;
}> {
  return http.get('/courses/review', { params });
}

// 获取课程包详情
export function getCourse(id: number): Promise<any> {
  return http.get(`/courses/${id}`);
}

// 创建课程包
export function createCourse(data: any): Promise<any> {
  return http.post('/courses', data);
}

// 更新课程包
export function updateCourse(id: number, data: any): Promise<any> {
  return http.put(`/courses/${id}`, data);
}

// 删除课程包
export function deleteCourse(id: number): Promise<any> {
  return http.delete(`/courses/${id}`);
}

// 验证课程完整性
export function validateCourse(id: number): Promise<ValidationResult> {
  return http.get(`/courses/${id}/validate`);
}

// 提交审核
export function submitCourse(id: number, copyrightConfirmed: boolean): Promise<any> {
  return http.post(`/courses/${id}/submit`, { copyrightConfirmed });
}

// 撤销审核
export function withdrawCourse(id: number): Promise<any> {
  return http.post(`/courses/${id}/withdraw`);
}

// 审核通过
export function approveCourse(id: number, data: { checklist?: any; comment?: string }): Promise<any> {
  return http.post(`/courses/${id}/approve`, data);
}

// 审核驳回
export function rejectCourse(id: number, data: { checklist?: any; comment: string }): Promise<any> {
  return http.post(`/courses/${id}/reject`, data);
}

// 直接发布（超级管理员）
export function directPublishCourse(id: number, skipValidation?: boolean): Promise<any> {
  return http.post(`/courses/${id}/direct-publish`, { skipValidation });
}

// 发布课程包（兼容旧API）
export function publishCourse(id: number): Promise<any> {
  return http.post(`/courses/${id}/publish`);
}

// 下架课程包
export function unpublishCourse(id: number): Promise<any> {
  return http.post(`/courses/${id}/unpublish`);
}

// 重新发布
export function republishCourse(id: number): Promise<any> {
  return http.post(`/courses/${id}/republish`);
}

// 获取课程包统计数据
export function getCourseStats(id: number): Promise<any> {
  return http.get(`/courses/${id}/stats`);
}

// 获取版本历史
export function getCourseVersions(id: number): Promise<any[]> {
  return http.get(`/courses/${id}/versions`);
}

// 课程状态映射
export const COURSE_STATUS_MAP: Record<string, { label: string; color: string }> = {
  DRAFT: { label: '草稿', color: 'default' },
  PENDING: { label: '审核中', color: 'processing' },
  REJECTED: { label: '已驳回', color: 'error' },
  PUBLISHED: { label: '已发布', color: 'success' },
  ARCHIVED: { label: '已下架', color: 'warning' },
};

// 获取状态显示信息
export function getCourseStatusInfo(status: string) {
  return COURSE_STATUS_MAP[status] || { label: status, color: 'default' };
}
