import { http } from "./index";
import {
  readingApi,
  UnwrapResult,
  ApiResultOf,
  GetPackages1Result,
} from "./client";

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

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

type AdminPackageResult = UnwrapResult<ApiResultOf<"getPackage1">>;

// 获取套餐列表（管理员端）
export const getPackageList = readingApi.getPackages1;
// 获取套餐详情（管理员端）
export function getPackageDetail(id: number): Promise<AdminPackageResult> {
  return readingApi.getPackage1(id).then((res) => res.data as any);
}

// 创建套餐（管理员端）
export function createPackage(
  data: CreatePackageData,
): Promise<AdminPackageResult> {
  return readingApi.createPackage(data as any).then((res) => res.data as any);
}

// 更新套餐（管理员端）
export function updatePackage(
  id: number,
  data: Partial<CreatePackageData>,
): Promise<AdminPackageResult> {
  return readingApi
    .updatePackage(id, data as any)
    .then((res) => res.data as any);
}

// 删除套餐（管理员端）
export function deletePackage(id: number): Promise<void> {
  return readingApi.deletePackage(id).then(() => undefined);
}

// 设置套餐课程（仍使用自定义 HTTP 接口）
export function setPackageCourses(
  packageId: number,
  courses: { courseId: number; gradeLevel: string; sortOrder?: number }[],
) {
  return http.put(`/api/v1/admin/packages/${packageId}/courses`, { courses });
}

// 添加课程到套餐（仍使用自定义 HTTP 接口）
export function addCourseToPackage(
  packageId: number,
  data: { courseId: number; gradeLevel: string; sortOrder?: number },
) {
  return http.post(`/api/v1/admin/packages/${packageId}/courses`, data);
}

// 从套餐移除课程（仍使用自定义 HTTP 接口）
export function removeCourseFromPackage(packageId: number, courseId: number) {
  return http.delete(`/api/v1/admin/packages/${packageId}/courses/${courseId}`);
}

// 提交审核
export function submitPackage(id: number): Promise<void> {
  return readingApi.submitPackage(id).then(() => undefined);
}

// 审核套餐
export function reviewPackage(
  id: number,
  data: { approved: boolean; comment?: string },
): Promise<void> {
  return readingApi
    .reviewPackage(id, {
      approved: data.approved,
      comment: data.comment,
    } as any)
    .then(() => undefined);
}

// 发布套餐
export function publishPackage(id: number): Promise<void> {
  return readingApi.publishPackage(id).then(() => undefined);
}

// 下架套餐
export function offlinePackage(id: number): Promise<void> {
  return readingApi.offlinePackage(id).then(() => undefined);
}

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

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("/api/v1/school/packages");
}

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