import { http } from './index';

// ==================== 课程套餐（CourseCollection）管理 ====================

/**
 * 课程套餐响应（对应后端 CourseCollectionResponse）
 * 课程套餐包含多个课程包
 */
export interface CourseCollection {
  id: number | string;  // 后端 Long 序列化为 string，避免 JS 精度丢失
  name: string;
  description?: string;
  price: number;        // 价格（分）
  discountPrice?: number;  // 折后价格（分）
  discountType?: string;   // 折扣类型：PERCENTAGE、FIXED
  gradeLevels: string[];   // 适用年级
  packageCount: number;    // 课程包数量
  tenantCount?: number;    // 使用学校数
  status: string;          // DRAFT, PENDING, APPROVED, REJECTED, PUBLISHED, OFFLINE
  createdAt: string;
  publishedAt?: string;
  submittedAt?: string;
  reviewedAt?: string;
  reviewComment?: string;
  updatedAt?: string;
  startDate?: string;      // 开始日期（租户套餐）
  endDate?: string;        // 结束日期（租户套餐）
  packages?: CoursePackageItem[];  // 包含的课程包列表
}

/**
 * 课程包项（对应后端 CourseCollectionResponse.CoursePackageItem）
 * 套餐中的课程包项
 */
export interface CoursePackageItem {
  id: number | string;     // 课程包 ID
  name: string;            // 课程包名称
  description?: string;    // 课程包描述
  gradeLevels: string[];   // 适用年级
  courseCount: number;     // 课程数量
  sortOrder: number;       // 排序号
}

/**
 * 课程包（CoursePackage）响应
 * 课程包是通过 7 步流程创建的教学资源
 */
export interface CoursePackage {
  id: number | string;
  name: string;
  description?: string;
  pictureBookName?: string;     // 绘本名称
  gradeTags?: string[];         // 年级标签
  status: string;
  version?: string;
  usageCount?: number;
  teacherCount?: number;
  avgRating?: number;
  createdAt?: string;
  submittedAt?: string;
  reviewedAt?: string;
  reviewComment?: string;
  duration?: number;            // 时长（分钟）
  courseLessons?: CourseLesson[]; // 课程环节
}

/**
 * 课程环节
 */
export interface CourseLesson {
  id: number | string;
  courseId: number | string;
  lessonType: string;   // COLLECTIVE, DOMAIN, INTRO
  name: string;
  description?: string;
  duration: number;
  sortOrder: number;
  videoPath?: string;
  videoName?: string;
  pptPath?: string;
  pptName?: string;
  pdfPath?: string;
  pdfName?: string;
  objectives?: string;
  preparation?: string;
  extension?: string;
  assessmentData?: string;
}

export interface PackageListParams {
  status?: string;
  gradeLevels?: string;  // 逗号分隔的年级列表，如 "小班，中班，大班"
  pageNum?: number;
  pageSize?: number;
}

export interface CreateCollectionData {
  name: string;
  description?: string;
  price: number;        // 价格（分）
  discountPrice?: number;  // 折后价格（分）
  discountType?: string;   // 折扣类型
  gradeLevels: string[];   // 适用年级
}

// ==================== 课程套餐 API（超管端） ====================

// 获取课程套餐列表
export function getCollectionList(params?: PackageListParams) {
  return http.get<{ list: CourseCollection[]; total: number; pageNum: number; pageSize: number; pages: number }>(
    '/v1/admin/collections',
    { params }
  );
}

// 获取课程套餐详情（id 支持 number | string，避免大整数精度丢失）
export function getCollectionDetail(id: number | string) {
  return http.get<CourseCollection>(`/v1/admin/collections/${id}`);
}

// 创建课程套餐
export function createCollection(data: CreateCollectionData) {
  return http.post('/v1/admin/collections', data);
}

// 更新课程套餐
export function updateCollection(id: number | string, data: Partial<CreateCollectionData>) {
  return http.put(`/v1/admin/collections/${id}`, data);
}

// 删除课程套餐
export function deleteCollection(id: number | string) {
  return http.delete(`/v1/admin/collections/${id}`);
}

// 设置课程套餐的课程包（后端期望 JSON 数组 [packageId1, packageId2, ...]）
export function setCollectionPackages(
  collectionId: number | string,
  packageIds: (number | string)[],
) {
  return http.put(`/v1/admin/collections/${collectionId}/packages`, packageIds);
}

// 提交审核（课程套餐）
export function submitCollection(id: number | string) {
  return http.post(`/v1/admin/collections/${id}/submit`);
}

// 发布课程套餐
export function publishCollection(id: number | string) {
  return http.post(`/v1/admin/collections/${id}/publish`);
}

// 下架课程套餐
export function archiveCollection(id: number | string) {
  return http.post(`/v1/admin/collections/${id}/archive`);
}

// 驳回课程套餐审核
export function rejectCollection(id: number | string, data: { comment: string }) {
  return http.post(`/v1/admin/collections/${id}/reject`, data);
}

// 重新发布课程套餐
export function republishCollection(id: number | string) {
  return http.post(`/v1/admin/collections/${id}/republish`);
}

// 撤销审核
export function withdrawCollection(id: number | string) {
  return http.post(`/v1/admin/collections/${id}/withdraw`);
}

// ==================== 课程包 API（超管端） ====================

// 获取课程包列表（7 步创建的教学资源）
export function getCoursePackageList(params?: { status?: string; pageNum?: number; pageSize?: number }) {
  return http.get<{ list: CoursePackage[]; total: number; pageNum: number; pageSize: number; pages: number }>(
    '/v1/admin/packages',
    { params }
  );
}

// 获取课程包详情
export function getCoursePackage(id: number | string) {
  return http.get<CoursePackage>(`/v1/admin/packages/${id}`);
}

// 创建课程包
export function createCoursePackage(data: any) {
  return http.post('/v1/admin/packages', data);
}

// 更新课程包
export function updateCoursePackage(id: number | string, data: any) {
  return http.put(`/v1/admin/packages/${id}`, data);
}

// 删除课程包
export function deleteCoursePackage(id: number | string) {
  return http.delete(`/v1/admin/packages/${id}`);
}

// 发布课程包
export function publishCoursePackage(id: number | string) {
  return http.post(`/v1/admin/packages/${id}/publish`);
}

// ==================== 学校端套餐 API ====================

export interface TenantPackage {
  id: number;
  tenantId: number;
  collectionId: number;  // 课程套餐 ID（使用 collectionId 而非 packageId）
  startDate: string;
  endDate: string;
  status: string;
  pricePaid: number;
}

// 获取学校已授权课程套餐列表
export function getTenantCollections() {
  return http.get('/v1/school/packages');
}

// 获取课程套餐下的课程包列表
export function getCollectionPackages(collectionId: number | string) {
  return http.get<CoursePackageItem[]>(`/v1/school/packages/${collectionId}/packages`);
}

// 获取课程包下的课程环节
export function getPackageCourses(packageId: number | string) {
  return http.get<CoursePackage>(`/v1/school/packages/${packageId}/courses`);
}

// 续订课程套餐
export function renewCollection(collectionId: number | string, data: { endDate: string; pricePaid?: number }) {
  return http.post(`/v1/school/packages/${collectionId}/renew`, data);
}
