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

export interface Student {
  id: number;
  userId: number;
  tenantId: number;
  classId: number;
  studentNo?: string;
  phone?: string;
  idCard?: string;
  gender?: number; // 1-男，2-女
  birthDate?: string;
  enrollmentDate?: string;
  parentName?: string;
  parentPhone?: string;
  address?: string;
  description?: string;
  validState: number;
  createBy?: number;
  updateBy?: number;
  createTime?: string;
  updateTime?: string;
  user?: {
    id: number;
    username: string;
    nickname: string;
    email?: string;
    avatar?: string;
    validState: number;
  };
  class?: {
    id: number;
    name: string;
    code: string;
    type: number;
    grade?: {
      id: number;
      name: string;
      code: string;
      level: number;
    };
  };
  interestClasses?: Array<{
    id: number;
    class: {
      id: number;
      name: string;
      code: string;
      type: number;
      grade?: {
        id: number;
        name: string;
        code: string;
        level: number;
      };
    };
  }>;
}

export interface CreateStudentForm {
  username: string;
  password: string;
  nickname: string;
  email?: string;
  avatar?: string;
  classId: number;
  studentNo?: string;
  phone?: string;
  idCard?: string;
  gender?: number;
  birthDate?: string;
  enrollmentDate?: string;
  parentName?: string;
  parentPhone?: string;
  address?: string;
  description?: string;
  interestClassIds?: number[];
}

export interface UpdateStudentForm {
  nickname?: string;
  email?: string;
  avatar?: string;
  classId?: number;
  studentNo?: string;
  phone?: string;
  idCard?: string;
  gender?: number;
  birthDate?: string;
  enrollmentDate?: string;
  parentName?: string;
  parentPhone?: string;
  address?: string;
  description?: string;
  validState?: number;
  interestClassIds?: number[];
}

// 获取学生列表
export async function getStudentsList(
  params: PaginationParams & { classId?: number }
): Promise<PaginationResponse<Student>> {
  const response = await request.get<any, PaginationResponse<Student>>("/api/students", {
    params,
  });
  return response;
}

// 获取单个学生详情
export async function getStudentDetail(id: number): Promise<Student> {
  const response = await request.get<any, Student>(`/api/students/${id}`);
  return response;
}

// 根据用户ID获取学生信息
export async function getStudentByUserId(userId: number): Promise<Student> {
  const response = await request.get<any, Student>(`/api/students/user/${userId}`);
  return response;
}

// 创建学生
export async function createStudent(data: CreateStudentForm): Promise<Student> {
  const response = await request.post<any, Student>("/api/students", data);
  return response;
}

// 更新学生
export async function updateStudent(
  id: number,
  data: UpdateStudentForm
): Promise<Student> {
  const response = await request.put<any, Student>(`/api/students/${id}`, data);
  return response;
}

// 删除学生
export async function deleteStudent(id: number): Promise<void> {
  return await request.delete<any, void>(`/api/students/${id}`);
}

export const studentsApi = {
  getList: getStudentsList,
  getDetail: getStudentDetail,
  getByUserId: getStudentByUserId,
  create: createStudent,
  update: updateStudent,
  delete: deleteStudent,
};

