import type { PageResponse } from '#/api/http'

import { get, post } from '#/api/http'

export type ClassroomItem = {
  id: number | string
  name: string
  /** 1 监控开启，0 关闭 */
  monitorState?: number
}

export type ClassMonitorCurrent = {
  classId: number | string
  intervalMs: number
  monitoring: boolean
  sessionId: number | string
  startTime: string
  teacherId: number | string
}

export type ClassMonitorSnapshot = {
  avatar: string
  latestTs: number
  latestUrl: string
  staleSeconds: number
  studentId: number | string
  studentName: string
}

export type ClassMonitorStudentSnapshot = {
  ts: number
  url: string
}

export type ClassMonitorSessionRow = {
  classId: number | string
  className: string
  durationSec: number
  endTime: string
  intervalMs: number
  schoolId: number | string
  sessionId: number | string
  snapshotCount: number
  startTime: string
  status: number
  studentCount: number
  teacherId: number | string
  teacherName: string
}

/** `/school/classMonitor/sessionDetail` 中 data 内单条学生（与列表快照字段接近，可能含 snapshotCount） */
export type ClassMonitorSessionDetailStudent = {
  avatar: string
  latestTs: number
  latestUrl: string
  snapshotCount?: number
  staleSeconds?: number
  studentId: number | string
  studentName: string
}

/** `/school/classMonitor/sessionDetail` 返回的 data 对象 */
export type ClassMonitorSessionDetail = {
  classId: number | string
  className: string
  durationSec: number
  endTime: string
  intervalMs: number
  schoolId: number | string
  sessionId: number | string
  snapshotCount: number
  startTime: string
  status: number
  studentCount: number
  students: ClassMonitorSessionDetailStudent[]
  teacherId: number | string
  teacherName: string
}

/** 分页里的扩展字段 rainbow 暂不消费 */
export type ClassMonitorSessionsPage = PageResponse<ClassMonitorSessionRow> & {
  rainbow?: unknown[]
}

class ClassroomMonitorService {
  /** 教师名下班级列表，与 `/api/main/org/class/listByTeacher` 一致 */
  async getClassList(): Promise<ClassroomItem[]> {
    const res = await get<any>('/org/class/listByTeacher')
    const list = Array.isArray(res) ? res : (res?.rows ?? res?.data ?? [])
    return list as ClassroomItem[]
  }

  async getCurrent(classId: number | string) {
    return await get<ClassMonitorCurrent>('/school/classMonitor/current', {
      classId,
    })
  }

  async getLatestSnapshots(classId: number | string) {
    return await get<ClassMonitorSnapshot[]>(
      '/school/classMonitor/latestSnapshots',
      { classId },
    )
  }

  async getStudentSnapshots(
    sessionId: number | string,
    studentId: number | string,
  ) {
    return await get<ClassMonitorStudentSnapshot[]>(
      '/school/classMonitor/studentSnapshots',
      { sessionId, studentId },
    )
  }

  async getSessions(
    classId: number | string,
    params?: { current?: number; size?: number },
  ) {
    return await post<ClassMonitorSessionsPage>('/school/classMonitor/sessions', {
      classId,
      current: params?.current ?? 1,
      size: params?.size ?? 10,
    })
  }

  async getSessionDetail(
    sessionId: number | string,
    classId?: number | string,
  ) {
    const params: Record<string, number | string> = { sessionId }
    if (classId !== undefined && classId !== '') {
      params.classId = classId
    }
    return await get<ClassMonitorSessionDetail>(
      '/school/classMonitor/sessionDetail',
      params,
    )
  }

  async updateMonitorStatus(classId: number | string, status: 0 | 1) {
    return await post('/school/classMonitor/control', {
      classId,
      status,
    })
  }

  async heartbeat(classId: number | string) {
    return await post(
      `/school/classMonitor/heartbeat?classId=${encodeURIComponent(String(classId))}`,
    )
  }
}

export const classroomMonitorService = new ClassroomMonitorService()
