import { http } from "./index";

// ==================== 类型定义 ====================

/**
 * 产品套餐
 */
export interface ProductBundle {
  id: string;
  name: string;
  description?: string;
  coverUrl?: string;
  price: number; // 单位：分
  discountPrice?: number; // 单位：分
  discountType?: string; // PERCENT-折扣 / FIXED-立减
  gradeLevels: string[];
  status: string; // DRAFT, PENDING_REVIEW, APPROVED, REJECTED, PUBLISHED, OFFLINE
  coursePackageCount: number;
  tenantCount: number;
  publishedAt?: string;
  isSystem: number;
  createdAt: string;
  updatedAt: string;
  bundleCoursePackages?: BundleCoursePackage[];
}

/**
 * 套餐 - 课程包关联
 */
export interface BundleCoursePackage {
  id: string;
  productBundleId: string;
  coursePackageId: string;
  gradeLevel?: string;
  sortOrder: number;
  coursePackage?: CoursePackageSimple;
}

/**
 * 课程包（简化版）
 */
export interface CoursePackageSimple {
  id: string;
  name: string;
  description?: string;
  coverUrl?: string;
  courseCount?: number;
}

/**
 * 学校套餐购买记录
 */
export interface TenantProductBundle {
  id: string;
  tenantId: string;
  productBundleId: string;
  productId: string;
  startDate: string;
  endDate: string;
  status: string; // ACTIVE, EXPIRED, SUSPENDED
  pricePaid: number; // 单位：分
  purchaseTime?: string;
  renewedFrom?: string;
  createdAt: string;
  updatedAt: string;
  productBundle?: ProductBundle;
}

// ==================== 请求参数 ====================

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

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

export interface ReviewBundleData {
  approved: boolean;
  comment?: string;
}

// ==================== 管理员端 - 套餐管理 ====================

/**
 * 获取套餐列表（分页）
 */
export function getBundleList(params: BundleListParams) {
  return http.get("/api/v1/admin/bundles", { params });
}

/**
 * 获取套餐详情
 */
export function getBundleDetail(id: string): Promise<ProductBundle> {
  return http.get(`/api/v1/admin/bundles/${id}`);
}

/**
 * 创建套餐
 */
export function createBundle(data: CreateBundleData): Promise<ProductBundle> {
  return http.post("/api/v1/admin/bundles", data);
}

/**
 * 更新套餐
 */
export function updateBundle(id: string, data: Partial<CreateBundleData>): Promise<ProductBundle> {
  return http.put(`/api/v1/admin/bundles/${id}`, data);
}

/**
 * 删除套餐
 */
export function deleteBundle(id: string): Promise<void> {
  return http.delete(`/api/v1/admin/bundles/${id}`);
}

/**
 * 提交套餐审核
 */
export function submitBundle(id: string): Promise<void> {
  return http.post(`/api/v1/admin/bundles/${id}/submit`);
}

/**
 * 审核套餐（通过或拒绝）
 */
export function reviewBundle(id: string, data: ReviewBundleData): Promise<void> {
  return http.post(`/api/v1/admin/bundles/${id}/review`, data);
}

/**
 * 发布套餐
 */
export function publishBundle(id: string): Promise<void> {
  return http.post(`/api/v1/admin/bundles/${id}/publish`);
}

/**
 * 下架套餐
 */
export function offlineBundle(id: string): Promise<void> {
  return http.post(`/api/v1/admin/bundles/${id}/offline`);
}

/**
 * 获取系统套餐列表
 */
export function getSystemBundles(params: { page?: number; pageSize?: number; gradeLevel?: string }) {
  return http.get("/api/v1/admin/bundles/system", { params });
}

// ==================== 管理员端 - 套餐课程包关联管理 ====================

/**
 * 获取套餐包含的课程包列表
 */
export function getBundleCoursePackages(bundleId: string) {
  return http.get(`/api/v1/admin/bundles/${bundleId}/packages`);
}

/**
 * 添加课程包到套餐
 */
export function addBundleCoursePackage(
  bundleId: string,
  data: { coursePackageId: string; gradeLevel?: string }
) {
  return http.post(`/api/v1/admin/bundles/${bundleId}/packages`, data);
}

/**
 * 从套餐移除课程包
 */
export function removeBundleCoursePackage(bundleId: string, id: string): Promise<void> {
  return http.delete(`/api/v1/admin/bundles/${bundleId}/packages/${id}`);
}

/**
 * 更新课程包排序
 */
export function updateBundleCoursePackageSort(
  bundleId: string,
  id: string,
  sortOrder: number
): Promise<void> {
  return http.put(`/api/v1/admin/bundles/${bundleId}/packages/${id}/sort`, { sortOrder });
}

/**
 * 批量添加课程包到套餐
 */
export function batchAddBundleCoursePackages(
  bundleId: string,
  coursePackageIds: string[]
): Promise<void> {
  return http.post(`/api/v1/admin/bundles/${bundleId}/packages/batch`, coursePackageIds);
}

// ==================== 管理员端 - 课程包课程关联管理 ====================

/**
 * 获取课程包包含的课程列表
 */
export function getPackageCourses(packageId: string) {
  return http.get(`/api/v1/admin/packages/${packageId}/courses`);
}

/**
 * 添加课程到课程包
 */
export function addPackageCourse(
  packageId: string,
  data: { courseId: string }
) {
  return http.post(`/api/v1/admin/packages/${packageId}/courses`, data);
}

/**
 * 从课程包移除课程
 */
export function removePackageCourse(packageId: string, id: string): Promise<void> {
  return http.delete(`/api/v1/admin/packages/${packageId}/courses/${id}`);
}

/**
 * 更新课程排序
 */
export function updatePackageCourseSort(
  packageId: string,
  id: string,
  sortOrder: number
): Promise<void> {
  return http.put(`/api/v1/admin/packages/${packageId}/courses/${id}/sort`, { sortOrder });
}

/**
 * 批量添加课程到课程包
 */
export function batchAddPackageCourses(
  packageId: string,
  courseIds: string[]
): Promise<void> {
  return http.post(`/api/v1/admin/packages/${packageId}/courses/batch`, courseIds);
}

// ==================== 管理员端 - 学校套餐购买记录管理 ====================

/**
 * 获取学校套餐购买记录列表（分页）
 */
export function getTenantBundleList(params: {
  page?: number;
  pageSize?: number;
  tenantId?: string;
  status?: string;
}) {
  return http.get("/api/v1/admin/tenant-bundles", { params });
}

/**
 * 获取购买记录详情
 */
export function getTenantBundleDetail(id: string) {
  return http.get(`/api/v1/admin/tenant-bundles/${id}`);
}

/**
 * 获取学校的套餐列表
 */
export function getTenantBundlesByTenantId(tenantId: string) {
  return http.get(`/api/v1/admin/tenant-bundles/tenant/${tenantId}`);
}

/**
 * 获取学校当前有效的套餐
 */
export function getActiveTenantBundle(tenantId: string) {
  return http.get(`/api/v1/admin/tenant-bundles/tenant/${tenantId}/active`);
}

/**
 * 创建购买记录
 */
export function createTenantBundle(data: Partial<TenantProductBundle>) {
  return http.post("/api/v1/admin/tenant-bundles", data);
}

/**
 * 更新购买记录
 */
export function updateTenantBundle(id: string, data: Partial<TenantProductBundle>) {
  return http.put(`/api/v1/admin/tenant-bundles/${id}`, data);
}

/**
 * 激活套餐
 */
export function activateTenantBundle(id: string): Promise<void> {
  return http.post(`/api/v1/admin/tenant-bundles/${id}/activate`);
}

/**
 * 过期套餐
 */
export function expireTenantBundle(id: string): Promise<void> {
  return http.post(`/api/v1/admin/tenant-bundles/${id}/expire`);
}

/**
 * 暂停套餐
 */
export function suspendTenantBundle(id: string): Promise<void> {
  return http.post(`/api/v1/admin/tenant-bundles/${id}/suspend`);
}
