import request from "@/utils/request";
import type { PaginationParams, PaginationResponse } from "@/types/api";

export interface Class {
  id: number;
  tenantId: number;
  gradeId: number;
  name: string;
  code: string;
  type: number; // 1-行政班级，2-兴趣班
  capacity?: number;
  description?: string;
  validState: number;
  creator?: number;
  modifier?: number;
  createTime?: string;
  modifyTime?: string;
  grade?: {
    id: number;
    name: string;
    code: string;
    level: number;
  };
  _count?: {
    students: number;
    studentInterestClasses: number;
  };
}

export interface CreateClassForm {
  gradeId: number;
  name: string;
  code: string;
  type: number;
  capacity?: number;
  description?: string;
}

export interface UpdateClassForm {
  gradeId?: number;
  name?: string;
  code?: string;
  type?: number;
  capacity?: number;
  description?: string;
}

// 获取班级列表
export async function getClassesList(
  params: PaginationParams & { gradeId?: number; type?: number }
): Promise<PaginationResponse<Class>> {
  const response = await request.get<any, PaginationResponse<Class>>("/classes", {
    params,
  });
  return response;
}

// 获取单个班级详情
export async function getClassDetail(id: number): Promise<Class> {
  const response = await request.get<any, Class>(`/classes/${id}`);
  return response;
}

// 创建班级
export async function createClass(data: CreateClassForm): Promise<Class> {
  const response = await request.post<any, Class>("/classes", data);
  return response;
}

// 更新班级
export async function updateClass(
  id: number,
  data: UpdateClassForm
): Promise<Class> {
  const response = await request.patch<any, Class>(`/classes/${id}`, data);
  return response;
}

// 删除班级
export async function deleteClass(id: number): Promise<void> {
  return await request.delete<any, void>(`/classes/${id}`);
}

export const classesApi = {
  getList: getClassesList,
  getDetail: getClassDetail,
  create: createClass,
  update: updateClass,
  delete: deleteClass,
};

