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

export interface Teacher {
  id: number;
  userId: number;
  tenantId: number;
  departmentId: number;
  employeeNo?: string;
  phone?: string;
  idCard?: string;
  gender?: number; // 1-男，2-女
  birthDate?: string;
  hireDate?: string;
  subject?: string;
  title?: 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;
  };
  department?: {
    id: number;
    name: string;
    code: string;
  };
}

export interface CreateTeacherForm {
  username: string;
  password: string;
  nickname: string;
  email?: string;
  avatar?: string;
  departmentId: number;
  employeeNo?: string;
  phone?: string;
  idCard?: string;
  gender?: number;
  birthDate?: string;
  hireDate?: string;
  subject?: string;
  title?: string;
  description?: string;
}

export interface UpdateTeacherForm {
  nickname?: string;
  email?: string;
  avatar?: string;
  departmentId?: number;
  employeeNo?: string;
  phone?: string;
  idCard?: string;
  gender?: number;
  birthDate?: string;
  hireDate?: string;
  subject?: string;
  title?: string;
  description?: string;
  validState?: number;
}

// 获取教师列表
export async function getTeachersList(
  params: PaginationParams & { departmentId?: number; nickname?: string; username?: string }
): Promise<PaginationResponse<Teacher>> {
  const response = await request.get<any, PaginationResponse<Teacher>>("/api/teachers", {
    params,
  });
  return response;
}

// 获取单个教师详情
export async function getTeacherDetail(id: number): Promise<Teacher> {
  const response = await request.get<any, Teacher>(`/api/teachers/${id}`);
  return response;
}

// 根据用户ID获取教师信息
export async function getTeacherByUserId(userId: number): Promise<Teacher> {
  const response = await request.get<any, Teacher>(`/api/teachers/user/${userId}`);
  return response;
}

// 创建教师
export async function createTeacher(data: CreateTeacherForm): Promise<Teacher> {
  const response = await request.post<any, Teacher>("/api/teachers", data);
  return response;
}

// 更新教师
export async function updateTeacher(
  id: number,
  data: UpdateTeacherForm
): Promise<Teacher> {
  const response = await request.put<any, Teacher>(`/api/teachers/${id}`, data);
  return response;
}

// 删除教师
export async function deleteTeacher(id: number): Promise<void> {
  return await request.delete<any, void>(`/api/teachers/${id}`);
}

export const teachersApi = {
  getList: getTeachersList,
  getDetail: getTeacherDetail,
  getByUserId: getTeacherByUserId,
  create: createTeacher,
  update: updateTeacher,
  delete: deleteTeacher,
};

