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

export interface Grade {
  id: number;
  tenantId: number;
  name: string;
  code: string;
  level: number;
  description?: string;
  validState: number;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  _count?: {
    classes: number;
  };
}

export interface CreateGradeForm {
  name: string;
  code: string;
  level: number;
  description?: string;
}

export interface UpdateGradeForm {
  name?: string;
  code?: string;
  level?: number;
  description?: string;
}

// 获取年级列表
export async function getGradesList(
  params: PaginationParams
): Promise<PaginationResponse<Grade>> {
  const response = await request.get<any, PaginationResponse<Grade>>("/api/grades", {
    params,
  });
  return response;
}

// 获取单个年级详情
export async function getGradeDetail(id: number): Promise<Grade> {
  const response = await request.get<any, Grade>(`/api/grades/${id}`);
  return response;
}

// 创建年级
export async function createGrade(data: CreateGradeForm): Promise<Grade> {
  const response = await request.post<any, Grade>("/api/grades", data);
  return response;
}

// 更新年级
export async function updateGrade(
  id: number,
  data: UpdateGradeForm
): Promise<Grade> {
  const response = await request.put<any, Grade>(`/api/grades/${id}`, data);
  return response;
}

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

export const gradesApi = {
  getList: getGradesList,
  getDetail: getGradeDetail,
  create: createGrade,
  update: updateGrade,
  delete: deleteGrade,
};

