import { request } from '@/api/request'

export interface UrlDeviceItem {
  id: number
  created_at: string
  updated_at: string
  deleted_at: string | null
  service_identity: string
  name: string
  description: string
  target_url: string
  method: string
  enabled: boolean
  interval: number
  extra: string
  tags: string
  collect_on: boolean
  collect_interval: number
  collect_last_result: string
  status: string
  status_code: number
  response_time: number
  last_check_time: string
  last_online_time: string | null
  last_offline_time: string | null
  continuous_errors: number
}

export interface UrlDeviceListResponse {
  total: number
  page: number
  page_size: number
  data: UrlDeviceItem[]
}

export interface UrlDeviceListParams {
  page?: number
  size?: number
  keyword?: string
  enabled?: boolean
}

export interface UrlDeviceCreateData {
  service_identity?: string
  name: string
  description?: string
  target_url: string
  method?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  collect_on?: boolean
  collect_interval?: number
  collect_last_result?: string
}

export interface UrlDeviceUpdateData {
  name?: string
  description?: string
  target_url?: string
  method?: string
  enabled?: boolean
  interval?: number
  extra?: string
  tags?: string
  collect_on?: boolean
  collect_interval?: number
  collect_last_result?: string
  status?: string
  status_code?: number
  response_time?: number
  last_check_time?: string
  last_online_time?: string | null
  last_offline_time?: string | null
  continuous_errors?: number
}

export interface UrlDeviceCollectData {
  collect_on?: boolean
  collect_interval?: number
  collect_last_result?: string
}

export const fetchUrlDeviceList = (params?: UrlDeviceListParams) => {
  return request.get<UrlDeviceListResponse>('/DC-Control/v1/url-devices', { params })
}

export const fetchUrlDeviceDetail = (id: number) => {
  return request.get<UrlDeviceItem>(`/DC-Control/v1/url-devices/${id}`)
}

export const createUrlDevice = (data: UrlDeviceCreateData) => {
  return request.post<{ message: string; id: number }>('/DC-Control/v1/url-devices', data)
}

export const updateUrlDevice = (id: number, data: UrlDeviceUpdateData) => {
  return request.put<{ message: string }>(`/DC-Control/v1/url-devices/${id}`, data)
}

export const patchUrlDeviceCollect = (id: number, data: UrlDeviceCollectData) => {
  return request.patch<{ message: string }>(`/DC-Control/v1/url-devices/${id}/collect`, data)
}

export const deleteUrlDevice = (id: number) => {
  return request.delete<{ message: string }>(`/DC-Control/v1/url-devices/${id}`)
}

export interface UrlDeviceStatsOverview {
  total: number
  online: number
  today_alert_count: number
  avg_response_time_ms: number
}

export interface ResponseTimePoint {
  hour: string
  avg_response_time_ms: number
}

export interface UrlDeviceResponseTimeTrend {
  points: ResponseTimePoint[]
}

export interface StatusDistributionItem {
  status: string
  count: number
}

export interface UrlDeviceStatusDistribution {
  total: number
  by_status: StatusDistributionItem[]
}

export const fetchUrlDeviceStatsOverview = () => {
  return request.get<UrlDeviceStatsOverview>('/DC-Control/v1/url-devices/stats/overview')
}

export const fetchUrlDeviceResponseTimeTrend = () => {
  return request.get<UrlDeviceResponseTimeTrend>('/DC-Control/v1/url-devices/stats/response-time-trend')
}

export const fetchUrlDeviceStatusDistribution = () => {
  return request.get<UrlDeviceStatusDistribution>('/DC-Control/v1/url-devices/stats/status-distribution')
}
