import request from '@/utils/http'
export interface ModelRequest{id:string;user_login:string;provider_code:string;model:string;reason:string;requested_rpm:number;requested_monthly_tokens:number;status:string;decision_note:string;created_at:string}
export const fetchModelRequests=(status='')=>request.get<ModelRequest[]>({url:'/api/v1/admin/model-requests',params:{status}})
export const decideModelRequest=(id:string,status:'approve'|'reject',note:string)=>request.post<ModelRequest>({url:`/api/v1/admin/model-requests/${id}/${status}`,params:{note}})
export interface FactSettings{provider_id?:string;provider_code:string;model:string;timeout_seconds:number;updated_at?:string}
export interface FactPolicy{id?:string;scope:string;enabled:boolean;mode:'async'|'sync';action:'observe'|'annotate'|'block';knowledge_base_ids:string[];support_threshold:number;evidence_threshold:number;top_k:number;max_claims:number;revision?:number}
export interface FactEvent{id:string;request_id:string;model:string;mode:string;action:string;verdict:string;support_score?:number;latency_ms:number;error:string;created_at:string}
export const fetchFactSettings=()=>request.get<FactSettings>({url:'/api/v1/admin/fact-check/settings'})
export const saveFactSettings=(params:FactSettings)=>request.put<FactSettings>({url:'/api/v1/admin/fact-check/settings',params})
export const fetchFactPolicies=()=>request.get<{items:FactPolicy[]}>({url:'/api/v1/admin/fact-check/policies'})
export const createFactPolicy=(params:FactPolicy)=>request.post<FactPolicy>({url:'/api/v1/admin/fact-check/policies',params})
export const updateFactPolicy=(id:string,params:FactPolicy)=>request.put<FactPolicy>({url:`/api/v1/admin/fact-check/policies/${id}`,params})
export const deleteFactPolicy=(id:string)=>request.del({url:`/api/v1/admin/fact-check/policies/${id}`})
export const fetchFactEvents=()=>request.get<{items:FactEvent[]}>({url:'/api/v1/admin/fact-check/events'})


// --- 0.11.3 治理:资源/渠道申请、工具审批、报表多维、平台环境变量 ---
export interface ResourceRequest {
  id: string; portal_user_id: string; user_login: string; resource_type: string
  resource_code: string; reason: string; status: string; decision_note: string
  decided_at?: string; created_at: string; updated_at: string
}
export interface ToolApproval {
  id: string; tool_code: string; tool_name: string; status: string
  reason: string; decision_note: string; created_at?: string; decided_at?: string; decided_by?: string
}
export const fetchResourceRequests=(status='')=>request.get<ResourceRequest[]>({url:'/api/v1/admin/resource-requests',params:{status}})
export const decideResourceRequest=(id:string,status:'approve'|'reject',note:string)=>request.post<ResourceRequest>({url:`/api/v1/admin/resource-requests/${id}/${status}`,params:{note}})
export const fetchToolApprovals=(status='')=>request.get<ToolApproval[]>({url:'/api/v1/admin/tool-approvals',params:{status}})
export const decideToolApproval=(id:string,status:'approved'|'rejected',note:string)=>request.post<ToolApproval>({url:`/api/v1/admin/tool-approvals/${id}/decide`,params:{status,note}})

export interface ToolUsageRow { code:string;name:string;requests:number;success:number;failed:number;avg_latency_ms:number }
export interface ApprovalStatRow { kind:string;status:string;count:number }
export interface SecurityReport { login_stats:Array<{success:boolean;count:number;distinct_ips:number}>;top_ips:Array<{ip:string;count:number}> }
export const fetchToolUsageReport=(params:{from:string;to:string})=>request.get<ToolUsageRow[]>({url:'/api/v1/admin/reports/tools',params})
export const fetchApprovalReport=(params:{from:string;to:string})=>request.get<ApprovalStatRow[]>({url:'/api/v1/admin/reports/approvals',params})
export const fetchSecurityReport=(params:{from:string;to:string})=>request.get<SecurityReport>({url:'/api/v1/admin/reports/security',params})

export interface PlatformEnvVar { key:string;configured:boolean;description:string;updated_at:string }
export const fetchPlatformEnvVars=()=>request.get<PlatformEnvVar[]>({url:'/api/v1/admin/env-vars'})
export const upsertPlatformEnvVar=(key:string,value:string,description:string)=>request.put<{saved:boolean}>({url:`/api/v1/admin/env-vars/${key}`,params:{value,description}})
export const deletePlatformEnvVar=(key:string)=>request.del({url:`/api/v1/admin/env-vars/${key}`})
