import { http } from './index';

// ==================== 套餐管理 ====================

export interface CoursePackage {
  id: number;
  name: string;
  description?: string;
  price: number;
  discountPrice?: number;
  discountType?: string;
  gradeLevels: string[];
  status: string;
  courseCount: number;
  tenantCount: number;
  createdAt: string;
  publishedAt?: string;
  courses?: PackageCourse[];
}

export interface PackageCourse {
  packageId: number;
  courseId: number;
  gradeLevel: string;
  sortOrder: number;
  course: {
    id: number;
    name: string;
    coverImagePath?: string;
    duration?: number;
    gradeTags?: string;
  };
}

export interface PackageListParams {
  status?: string;
  page?: number;
  pageSize?: number;
}

export interface CreatePackageData {
  name: string;
  description?: string;
  price: number;
  discountPrice?: number;
  discountType?: string;
  gradeLevels: string[];
}

// 获取套餐列表
export function getPackageList(params?: PackageListParams) {
  return http.get('/admin/packages', { params });
}

// 获取套餐详情
export function getPackageDetail(id: number) {
  return http.get(`/admin/packages/${id}`);
}

// 创建套餐
export function createPackage(data: CreatePackageData) {
  return http.post('/admin/packages', data);
}

// 更新套餐
export function updatePackage(id: number, data: Partial<CreatePackageData>) {
  return http.put(`/admin/packages/${id}`, data);
}

// 删除套餐
export function deletePackage(id: number) {
  return http.delete(`/admin/packages/${id}`);
}

// 设置套餐课程
export function setPackageCourses(
  packageId: number,
  courses: { courseId: number; gradeLevel: string; sortOrder?: number }[],
) {
  return http.put(`/admin/packages/${packageId}/courses`, { courses });
}

// 添加课程到套餐
export function addCourseToPackage(
  packageId: number,
  data: { courseId: number; gradeLevel: string; sortOrder?: number },
) {
  return http.post(`/admin/packages/${packageId}/courses`, data);
}

// 从套餐移除课程
export function removeCourseFromPackage(packageId: number, courseId: number) {
  return http.delete(`/admin/packages/${packageId}/courses/${courseId}`);
}

// 提交审核
export function submitPackage(id: number) {
  return http.post(`/admin/packages/${id}/submit`);
}

// 审核套餐
export function reviewPackage(id: number, data: { approved: boolean; comment?: string }) {
  return http.post(`/admin/packages/${id}/review`, data);
}

// 发布套餐
export function publishPackage(id: number) {
  return http.post(`/admin/packages/${id}/publish`);
}

// 下架套餐
export function offlinePackage(id: number) {
  return http.post(`/admin/packages/${id}/offline`);
}

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

export interface TenantPackage {
  id: number;
  tenantId: number;
  packageId: number;
  startDate: string;
  endDate: string;
  status: string;
  pricePaid: number;
  package: CoursePackage;
}

// 获取学校已授权套餐
export function getTenantPackages() {
  return http.get('/school/packages');
}

// 续订套餐
export function renewPackage(packageId: number, data: { endDate: string; pricePaid?: number }) {
  return http.post(`/school/packages/${packageId}/renew`, data);
}
