import { computed, ref } from 'vue'
import { getOtaSettings, getOtaTarget, listOtaJobs } from '@/api/ota'
import type { OtaJob, OtaSettings, OtaTarget } from '@/types/ota'
import { OTA_COPY } from '@/views/shared/ota/otaCopy'

const settings = ref<OtaSettings | null>(null)
const target = ref<OtaTarget | null>(null)
const targetSummary = ref<Record<string, string>>({})
const activeJobCount = ref(0)
const loadingMeta = ref(false)
let pollTimer: ReturnType<typeof setInterval> | null = null
let started = false

export async function refreshOtaMeta() {
  loadingMeta.value = true
  try {
    const [s, t, jobs] = await Promise.all([
      getOtaSettings(),
      getOtaTarget(),
      listOtaJobs(30)
    ])
    settings.value = s
    target.value = t.target
    targetSummary.value = t.summary ?? {}
    activeJobCount.value = jobs.filter((j) =>
      ['pending', 'probing', 'running'].includes(j.status)
    ).length
  } finally {
    loadingMeta.value = false
  }
}

export function startOtaMetaPolling() {
  if (started) return
  started = true
  void refreshOtaMeta()
  pollTimer = setInterval(() => {
    void refreshOtaMeta()
  }, 8000)
}

export function stopOtaMetaPolling() {
  started = false
  if (pollTimer) {
    clearInterval(pollTimer)
    pollTimer = null
  }
}

export function useOtaWorkbench() {
  const targetLabel = computed(() => {
    if (!target.value) return OTA_COPY.unsetTarget
    return target.value.name || target.value.packageId
  })

  return {
    settings,
    target,
    targetSummary,
    targetLabel,
    activeJobCount,
    loadingMeta,
    refreshMeta: refreshOtaMeta,
    startPolling: startOtaMetaPolling,
    stopPolling: stopOtaMetaPolling
  }
}

export type { OtaJob, OtaSettings, OtaTarget }
