import { http } from './index';

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

export type RecordType = 'STUDENT' | 'CLASS';

export interface GrowthRecord {
  id: number;
  tenantId: number;
  studentId: number;
  classId?: number;
  recordType: RecordType;
  title: string;
  content?: string;
  images: string[];
  recordDate: string;
  createdBy: number;
  createdAt: string;
  updatedAt: string;
  student?: {
    id: number;
    name: string;
    gender?: string;
  };
  class?: {
    id: number;
    name: string;
    grade?: string;
  };
}

export interface CreateGrowthRecordDto {
  studentId: number;
  classId?: number;
  recordType: RecordType;
  title: string;
  content?: string;
  images?: string[];
  recordDate: string;
}

export interface UpdateGrowthRecordDto {
  title?: string;
  content?: string;
  images?: string[];
  recordDate?: string;
}

// ==================== 学校端 API ====================

export const getGrowthRecords = (params?: {
  pageNum?: number;
  pageSize?: number;
  studentId?: number;
  classId?: number;
  recordType?: RecordType;
  keyword?: string;
}) =>
  http.get<{ items: GrowthRecord[]; total: number; page: number; pageSize: number }>(
    '/v1/school/growth-records',
    { params }
  );

export const getGrowthRecord = (id: number) =>
  http.get<GrowthRecord>(`/school/growth-records/${id}`);

export const createGrowthRecord = (data: CreateGrowthRecordDto) =>
  http.post<GrowthRecord>('/v1/school/growth-records', data);

export const updateGrowthRecord = (id: number, data: UpdateGrowthRecordDto) =>
  http.put<GrowthRecord>(`/school/growth-records/${id}`, data);

export const deleteGrowthRecord = (id: number) =>
  http.delete(`/school/growth-records/${id}`);

export const getStudentGrowthRecords = (studentId: number, params?: {
  pageNum?: number;
  pageSize?: number;
}) =>
  http.get<{ items: GrowthRecord[]; total: number; page: number; pageSize: number }>(
    `/v1/school/students/${studentId}/growth-records`,
    { params }
  );

export const getClassGrowthRecords = (classId: number, params?: {
  pageNum?: number;
  pageSize?: number;
  recordDate?: string;
}) =>
  http.get<{ items: GrowthRecord[]; total: number; page: number; pageSize: number }>(
    `/v1/school/classes/${classId}/growth-records`,
    { params }
  );

// ==================== 教师端 API ====================

export const getTeacherGrowthRecords = (params?: {
  pageNum?: number;
  pageSize?: number;
  studentId?: number;
  classId?: number;
  recordType?: RecordType;
  keyword?: string;
}) =>
  http.get<{ items: GrowthRecord[]; total: number; page: number; pageSize: number }>(
    '/v1/teacher/growth-records',
    { params }
  );

export const getTeacherGrowthRecord = (id: number) =>
  http.get<GrowthRecord>(`/v1/teacher/growth-records/${id}`);

export const createTeacherGrowthRecord = (data: CreateGrowthRecordDto) =>
  http.post<GrowthRecord>('/v1/teacher/growth-records', data);

export const updateTeacherGrowthRecord = (id: number, data: UpdateGrowthRecordDto) =>
  http.put<GrowthRecord>(`/v1/teacher/growth-records/${id}`, data);

export const deleteTeacherGrowthRecord = (id: number) =>
  http.delete(`/v1/teacher/growth-records/${id}`);

export const getTeacherClassGrowthRecords = (classId: number, params?: {
  pageNum?: number;
  pageSize?: number;
  recordDate?: string;
}) =>
  http.get<{ items: GrowthRecord[]; total: number; page: number; pageSize: number }>(
    `/v1/teacher/classes/${classId}/growth-records`,
    { params }
  );
