import type {
  ParentHistoryDetail,
  ParentHistoryList,
  ParentLanguagePair,
  ParentLesson,
  IdiomCatalog,
  ParentScene,
  ParentDeviceStatus,
  ParentTask,
  ParentTaskList,
  ParentTaskSave,
} from './types'
import { http } from '@/http/request/alova'

const noCache = {
  ignoreAuth: false,
  toast: false,
}

export function getParentScenes() {
  return http.Get<ParentScene[]>('/user/scenes', {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

export function getParentIdiomCatalog() {
  return http.Get<IdiomCatalog>('/user/idiom-catalog', {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

export function getParentLessons(scene: string) {
  return http.Get<ParentLesson[]>('/user/lessons', {
    params: { scene },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

export function getParentLanguagePairs() {
  return http.Get<ParentLanguagePair[]>('/user/language-pairs', {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

export function getParentDeviceStatus(macAddress: string, agentId: string) {
  return http.Get<ParentDeviceStatus>('/device/presence', {
    params: { macAddress, agentId },
    meta: noCache,
    cacheFor: { expire: 0 },
    timeout: 10000,
  })
}

export function getParentTasks(macAddress: string, agentId: string, runMode?: string) {
  return http.Get<ParentTaskList>('/user/parent-task', {
    params: {
      macAddress,
      agentId,
      ...(runMode ? { runMode } : {}),
    },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

export function getParentTask(id: string) {
  return http.Get<ParentTask>(`/user/parent-task/${id}`, {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

const mqttMeta = {
  ignoreAuth: false,
  toast: false,
  isExposeError: true,
}

export function createParentTask(data: ParentTaskSave) {
  return http.Post<ParentTask>('/user/parent-task', data, {
    timeout: 20000,
    meta: mqttMeta,
  })
}

export function updateParentTask(id: string, data: ParentTaskSave) {
  return http.Put<ParentTask>(`/user/parent-task/${id}`, data, {
    timeout: 20000,
    meta: mqttMeta,
    cacheFor: { expire: 0 },
  })
}

export function deleteParentTask(id: string) {
  return http.Delete<boolean>(`/user/parent-task/${id}`, {
    timeout: 20000,
    meta: mqttMeta,
  })
}

export function runParentTaskNow(id: string) {
  return http.Post<ParentTask>(`/user/parent-task/${id}/run`, {}, {
    timeout: 20000,
    meta: mqttMeta,
  })
}

export function stopParentTask(id: string) {
  return http.Post<ParentTask>(`/user/parent-task/${id}/stop`, {}, {
    timeout: 20000,
    meta: mqttMeta,
  })
}

export function getParentHistory(macAddress: string, agentId: string, limit = 20) {
  return http.Get<ParentHistoryList>('/user/parent-history', {
    params: { macAddress, agentId, limit },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

export function getParentHistoryDetail(id: string, macAddress: string, agentId: string) {
  return http.Get<ParentHistoryDetail>(`/user/parent-history/${id}`, {
    params: { macAddress, agentId },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}

export function parentTaskError(error: unknown, fallback: string) {
  if (typeof error === 'string' && error.trim())
    return error.trim()
  const message = error instanceof Error ? error.message : ''
  const cut = message.includes('：') ? message.split('：').slice(1).join('：').trim() : message.trim()
  return cut || fallback
}
