import request from '@/utils/request'

export interface DashboardStatsParams {
  start_date?: string
  end_date?: string
  include_enhanced?: boolean
}

export interface IntentRuleStat {
  ruleId: string
  ruleName: string
  hitCount: number
  hitRate: number
}

export interface PromptTemplateStat {
  templateId: string
  templateName: string
  usageCount: number
}

export interface ScriptFlowStat {
  flowId: string
  flowName: string
  activationCount: number
  completionRate: number
}

export interface GuardrailWordStat {
  word: string
  category: string
  hitCount: number
}

export interface DashboardStats {
  knowledgeBases: number
  totalDocuments: number
  totalMessages: number
  totalSessions: number
  totalTokens: number
  promptTokens: number
  completionTokens: number
  aiRequestsCount: number
  avgLatencyMs: number
  lastLatencyMs: number | null
  lastRequestTime: string | null
  slowRequestsCount: number
  errorRequestsCount: number
  p95LatencyMs: number
  p99LatencyMs: number
  minLatencyMs: number
  maxLatencyMs: number
  latencyThresholdMs: number
  intentRuleHitRate: number
  intentRuleHitCount: number
  topIntentRules: IntentRuleStat[]
  promptTemplateUsageCount: number
  topPromptTemplates: PromptTemplateStat[]
  scriptFlowActivationCount: number
  scriptFlowCompletionRate: number
  topScriptFlows: ScriptFlowStat[]
  guardrailBlockCount: number
  guardrailBlockRate: number
  topGuardrailWords: GuardrailWordStat[]
}

export function getDashboardStats(params?: DashboardStatsParams): Promise<DashboardStats> {
  return request({
    url: '/admin/dashboard/stats',
    method: 'get',
    params
  })
}
