import http from '@/api/http'
import type { LaunchMode, RunMode } from '@/types/auth'

export interface HealthInfo {
  status: string
  startTime: string
  uptimeSec: number
}

export interface Simple3Diagnostics {
  enabled: boolean
  isRunning: boolean
  lastLaunchMode?: string | null
  projectionPort: number
  projectionPortReachable: boolean
  gotoSiteApiAvailable?: boolean | null
  executableExists: boolean
  deployHint?: string | null
}

export interface Simple3LaunchResult {
  started: boolean
  status: string
  detail: string
  displayMode?: string | null
  warning?: string | null
}

export function getHealth() {
  return http.get<HealthInfo>('/health')
}

export function getSimple3Diagnostics() {
  return http.get<Simple3Diagnostics>('/health/simple3')
}

export function stopSimple3() {
  return http.post<{ killed: number; diagnostics: Simple3Diagnostics }>('/health/simple3/stop')
}

export function restartSimple3(launchMode: LaunchMode) {
  return http.post<{ restart: Simple3LaunchResult; diagnostics: Simple3Diagnostics }>(
    '/health/simple3/restart',
    null,
    { params: { launchMode }, timeout: 60_000 }
  )
}

/** Simple3 仅 Web；重启一律 WebOnly（保留函数签名供旧调用方）。 */
export function resolveRestartLaunchMode(
  _lastLaunchMode?: string | null,
  _runMode?: RunMode | null
): LaunchMode {
  return 'WebOnly'
}
