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

export interface AlertQualityScore {
  rule_id: number
  resource_uid: string
  business_system_id: number
  window_start: string
  window_end: string
  score: number
  repeat_count: number
  notify_count: number
  avg_ack_seconds: number
  avg_resolve_seconds: number
  reopen_rate: number
  factors: string[]
}

export interface NoiseBucket {
  rule_id: number
  resource_uid: string
  business_system_id: number
  window_start: string
  window_end: string
  alert_count: number
  notify_count: number
  noise_score: number
}

export interface RuleSuggestion {
  type: string
  priority: string
  rule_id: number
  resource_uid?: string
  business_system_id?: number
  reason: string
  action: string
}

export interface CapacityRisk {
  resource_uid: string
  metric_code: string
  unit: string
  current_value: number
  threshold: number
  slope_per_hour: number
  risk_level: string
  predicted_at?: string
  sample_count: number
}

export interface SloDashboardBucket {
  total: number
  response_breached: number
  resolve_breached: number
  breached: number
  breach_rate: number
}

export interface SloDashboard {
  total_closed: number
  response_breached: number
  resolve_breached: number
  overall_breached: number
  breach_rate: number
  by_business_system: Record<string, SloDashboardBucket>
  by_resource_category: Record<string, SloDashboardBucket>
}

export const fetchAlertQualityScores = (params?: { hours?: number }) =>
  request.get<AlertQualityScore[]>('/Alert/v1/governance/quality', { params })

export const fetchAlertNoise = (params?: { hours?: number }) =>
  request.get<NoiseBucket[]>('/Alert/v1/governance/noise', { params })

export const fetchRuleSuggestions = (params?: { hours?: number }) =>
  request.get<RuleSuggestion[]>('/Alert/v1/governance/suggestions', { params })

export const fetchCapacityRisk = (params?: { resource_uid?: string; metric_code?: string }) =>
  request.get<CapacityRisk>('/DC-Control/v1/risk/capacity', { params })

export const fetchSloDashboard = () => request.get<SloDashboard>('/Feedback/v1/tickets/slo/dashboard')
