import { get } from '../../http'

export interface AiHomeworkOverview {
  assignedHomeworkCount: number
  submittedHomeworkCount: number
  unsubmittedHomeworkCount: number
  excellentCount: number
  goodCount: number
  passCount: number
  unqualifiedCount: number
  completionRate: number
  excellentRate: number
  goodRate: number
  passRate: number
  unqualifiedRate: number
  aiCorrectedTitleCount: number
  participantTeacherCount: number
  participantStudentCount: number
  participantClassCount: number
  teacherTimeSavedMinutes: number
  aiAccuracyRate: string
  studentAppealRate: string
  aiAvgResponseTime: string
}

export interface OverviewParams {
  schoolId: string
  startTime?: string
  endTime?: string
}

function normalizeOverview(raw: any): AiHomeworkOverview {
  const toNum = (v: any) => {
    const n = Number(v)
    return Number.isNaN(n) ? 0 : n
  }
  return {
    assignedHomeworkCount: toNum(raw?.assignedHomeworkCount),
    submittedHomeworkCount: toNum(raw?.submittedHomeworkCount),
    unsubmittedHomeworkCount: toNum(raw?.unsubmittedHomeworkCount),
    excellentCount: toNum(raw?.excellentCount),
    goodCount: toNum(raw?.goodCount),
    passCount: toNum(raw?.passCount),
    unqualifiedCount: toNum(raw?.unqualifiedCount),
    completionRate: toNum(raw?.completionRate),
    excellentRate: toNum(raw?.excellentRate),
    goodRate: toNum(raw?.goodRate),
    passRate: toNum(raw?.passRate),
    unqualifiedRate: toNum(raw?.unqualifiedRate),
    aiCorrectedTitleCount: toNum(raw?.aiCorrectedTitleCount),
    participantTeacherCount: toNum(raw?.participantTeacherCount),
    participantStudentCount: toNum(raw?.participantStudentCount),
    participantClassCount: toNum(raw?.participantClassCount),
    teacherTimeSavedMinutes: toNum(raw?.teacherTimeSavedMinutes),
    aiAccuracyRate: String(raw?.aiAccuracyRate ?? ''),
    studentAppealRate: String(raw?.studentAppealRate ?? ''),
    aiAvgResponseTime: String(raw?.aiAvgResponseTime ?? ''),
  }
}

export async function getAiHomeworkOverview(
  params: OverviewParams,
): Promise<AiHomeworkOverview> {
  const raw = await get<any>('/school/report/aiHomeworkData/overview', params)
  return normalizeOverview(raw)
}

export interface GradeAiHomeworkDataItem extends AiHomeworkOverview {
  gradeId: string
  gradeName: string
  gradeSort: number
}

export interface ClassAiHomeworkDataItem extends AiHomeworkOverview {
  classId: string
  className: string
}

export interface SubjectAiHomeworkDataItem extends AiHomeworkOverview {
  subjectId: string
  subjectName: string
}

export interface TimeRangeParams {
  startTime?: string
  endTime?: string
}

export interface ClassOverviewParams extends TimeRangeParams {
  gradeId: string | number
}

function normalizeGradeItem(raw: any): GradeAiHomeworkDataItem {
  return {
    ...normalizeOverview(raw),
    gradeId: String(raw?.gradeId ?? ''),
    gradeName: String(raw?.gradeName ?? ''),
    gradeSort: Number(raw?.gradeSort ?? 0),
  }
}

function normalizeClassItem(raw: any): ClassAiHomeworkDataItem {
  return {
    ...normalizeOverview(raw),
    classId: String(raw?.classId ?? ''),
    className: String(raw?.className ?? ''),
  }
}

function normalizeSubjectItem(raw: any): SubjectAiHomeworkDataItem {
  return {
    ...normalizeOverview(raw),
    subjectId: String(raw?.subjectId ?? ''),
    subjectName: String(raw?.subjectName ?? ''),
  }
}

export async function getGradeOverview(
  params: TimeRangeParams,
): Promise<GradeAiHomeworkDataItem[]> {
  const raw = await get<any>(
    '/school/report/aiHomeworkDataByGrade/overview',
    params,
  )
  const list = raw?.gradeDataList ?? raw ?? []
  return (Array.isArray(list) ? list : []).map(normalizeGradeItem)
}

export async function getClassOverview(
  params: ClassOverviewParams,
): Promise<ClassAiHomeworkDataItem[]> {
  const raw = await get<any>(
    '/school/report/aiHomeworkDataByClass/overview',
    params,
  )
  const list = raw?.classDataList ?? raw ?? []
  return (Array.isArray(list) ? list : []).map(normalizeClassItem)
}

export interface SubjectOverviewParams extends TimeRangeParams {
  gradeId: string | number
  classId?: string | number
}

export async function getSubjectOverview(
  params: SubjectOverviewParams,
): Promise<SubjectAiHomeworkDataItem[]> {
  const raw = await get<any>(
    '/school/report/aiHomeworkDataBySubject/overview',
    params,
  )
  const list = raw?.subjectDataList ?? raw ?? []
  return (Array.isArray(list) ? list : []).map(normalizeSubjectItem)
}
