import { readingApi } from './client'
import type {
  GrowthRecord as ApiGrowthRecord,
  ResultGrowthRecord,
  ResultPageResultGrowthRecord,
} from './generated/model'

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

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

// 统一使用 OpenAPI 中的成长档案实体类型
export type GrowthRecord = ApiGrowthRecord

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?: {
  page?: number;
  pageSize?: number;
  studentId?: number;
  classId?: number;
  recordType?: RecordType;
  keyword?: string;
}) =>
  readingApi.getGrowthRecordPage1(params as any).then((res) => {
    const wrapped = res as ResultPageResultGrowthRecord
    const page = wrapped.data

    return {
      items: (page?.items as GrowthRecord[]) ?? [],
      total: page?.total ?? 0,
      page: page?.page ?? params?.page ?? 1,
      pageSize: page?.pageSize ?? params?.pageSize ?? 10,
    }
  });

export const getGrowthRecord = (id: number) =>
  readingApi.getGrowthRecord1(id).then((res) => {
    const wrapped = res as ResultGrowthRecord
    return (wrapped.data ?? res) as GrowthRecord
  });

export const createGrowthRecord = (data: CreateGrowthRecordDto) =>
  readingApi.createGrowthRecord1(data as any).then((res) => {
    const wrapped = res as ResultGrowthRecord
    return (wrapped.data ?? res) as GrowthRecord
  });

export const updateGrowthRecord = (id: number, data: UpdateGrowthRecordDto) =>
  readingApi.updateGrowthRecord1(id, data as any).then((res) => {
    const wrapped = res as ResultGrowthRecord
    return (wrapped.data ?? res) as GrowthRecord
  });

export const deleteGrowthRecord = (id: number) =>
  readingApi.deleteGrowthRecord1(id).then((res) => res);

export const getStudentGrowthRecords = (studentId: number, params?: {
  page?: number;
  pageSize?: number;
}) =>
  // OpenAPI 暂未提供该维度接口，这里先仅修正为带 /api/v1 前缀的路径
  import('./index').then(({ http }) =>
    http.get<{ items: GrowthRecord[]; total: number; page: number; pageSize: number }>(
      `/api/v1/school/students/${studentId}/growth-records`,
      { params }
    )
  );

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

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

export const getTeacherGrowthRecords = (params?: {
  page?: number;
  pageSize?: number;
  studentId?: number;
  classId?: number;
  recordType?: RecordType;
  keyword?: string;
}) =>
  readingApi.getGrowthRecordPage(params as any).then((res) => {
    const wrapped = res as ResultPageResultGrowthRecord
    const page = wrapped.data

    return {
      items: (page?.items as GrowthRecord[]) ?? [],
      total: page?.total ?? 0,
      page: page?.page ?? params?.page ?? 1,
      pageSize: page?.pageSize ?? params?.pageSize ?? 10,
    }
  });

export const getTeacherGrowthRecord = (id: number) =>
  readingApi.getGrowthRecord(id).then((res) => {
    const wrapped = res as ResultGrowthRecord
    return (wrapped.data ?? res) as GrowthRecord
  });

export const createTeacherGrowthRecord = (data: CreateGrowthRecordDto) =>
  readingApi.createGrowthRecord(data as any).then((res) => {
    const wrapped = res as ResultGrowthRecord
    return (wrapped.data ?? res) as GrowthRecord
  });

export const updateTeacherGrowthRecord = (id: number, data: UpdateGrowthRecordDto) =>
  readingApi.updateGrowthRecord(id, data as any).then((res) => {
    const wrapped = res as ResultGrowthRecord
    return (wrapped.data ?? res) as GrowthRecord
  });

export const deleteTeacherGrowthRecord = (id: number) =>
  readingApi.deleteGrowthRecord(id).then((res) => res);

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