import request from '@/utils/http'

export interface PromptVariable { name: string; label?: string; default?: string; required: boolean }
export interface PromptVersion { id: string; template_id: string; version: number; content: string; variables: PromptVariable[]; change_note: string; created_at: string }
export interface PromptTemplate { id: string; name: string; description: string; category_id?: string; category_name: string; tags: string[]; department_ids: string[]; enabled: boolean; current_version?: number; current?: PromptVersion; revision: number }
export interface PromptInput { name: string; description: string; category_id?: string | null; tags: string[]; department_ids: string[]; enabled: boolean; content: string; variables: PromptVariable[]; change_note: string }
export interface PromptCategory { id: string; name: string; description: string; template_count: number }
export const fetchPromptCategories=()=>request.get<PromptCategory[]>({url:'/api/v1/admin/prompt-categories'})
export const createPromptCategory=(params:{name:string;description:string})=>request.post<PromptCategory>({url:'/api/v1/admin/prompt-categories',params})
export const deletePromptCategory=(id:string)=>request.del({url:`/api/v1/admin/prompt-categories/${id}`})
export const fetchPrompts=()=>request.get<PromptTemplate[]>({url:'/api/v1/admin/prompts'})
export const createPrompt=(params:PromptInput)=>request.post<PromptTemplate>({url:'/api/v1/admin/prompts',params})
export const updatePrompt=(id:string,params:PromptInput)=>request.put<PromptTemplate>({url:`/api/v1/admin/prompts/${id}`,params})
export const deletePrompt=(id:string)=>request.del({url:`/api/v1/admin/prompts/${id}`})
export const fetchPromptVersions=(id:string)=>request.get<PromptVersion[]>({url:`/api/v1/admin/prompts/${id}/versions`})
export const createPromptVersion=(id:string,params:{content:string;variables:PromptVariable[];change_note:string;activate:boolean})=>request.post<PromptVersion>({url:`/api/v1/admin/prompts/${id}/versions`,params})
export const activatePromptVersion=(id:string,version:number)=>request.post({url:`/api/v1/admin/prompts/${id}/versions/${version}/activate`})

export interface KnowledgeBase { id:string;name:string;description:string;retrieval_mode:string;chunk_size:number;chunk_overlap:number;department_ids:string[];enabled:boolean;revision:number;document_count:number;chunk_count:number }
export interface KnowledgeDocument {id:string;knowledge_base_id:string;title:string;source_type:string;source_uri:string;content_sha256:string;status:string;status_message:string;char_count:number;chunk_count:number;created_at:string}
export interface SearchHit {chunk_id:string;document_id:string;document_title:string;chunk_index:number;content:string;score:number}
export type KnowledgeBaseInput=Pick<KnowledgeBase,'name'|'description'|'retrieval_mode'|'chunk_size'|'chunk_overlap'|'department_ids'|'enabled'>
export const fetchKnowledgeBases=()=>request.get<KnowledgeBase[]>({url:'/api/v1/admin/knowledge-bases'})
export const createKnowledgeBase=(params:KnowledgeBaseInput)=>request.post<KnowledgeBase>({url:'/api/v1/admin/knowledge-bases',params})
export const updateKnowledgeBase=(id:string,params:KnowledgeBaseInput)=>request.put<KnowledgeBase>({url:`/api/v1/admin/knowledge-bases/${id}`,params})
export const deleteKnowledgeBase=(id:string)=>request.del({url:`/api/v1/admin/knowledge-bases/${id}`})
export const fetchKnowledgeDocuments=(id:string)=>request.get<KnowledgeDocument[]>({url:`/api/v1/admin/knowledge-bases/${id}/documents`})
export const addKnowledgeDocument=(id:string,params:{title:string;source_type:string;source_uri:string;content:string})=>request.post<KnowledgeDocument>({url:`/api/v1/admin/knowledge-bases/${id}/documents`,params})
export const deleteKnowledgeDocument=(kb:string,id:string)=>request.del({url:`/api/v1/admin/knowledge-bases/${kb}/documents/${id}`})
export const searchKnowledge=(id:string,params:{query:string;top_k:number})=>request.post<SearchHit[]>({url:`/api/v1/admin/knowledge-bases/${id}/search`,params})

export interface ToolDefinition {id:string;code:string;name:string;description:string;endpoint_url:string;http_method:string;input_schema:Record<string,unknown>;timeout_seconds:number;department_ids:string[];enabled:boolean;has_secret_headers:boolean;revision:number}
export interface ToolInput {code:string;name:string;description:string;endpoint_url:string;http_method:string;headers?:Record<string,string>;input_schema:Record<string,unknown>;timeout_seconds:number;department_ids:string[];enabled:boolean}
export const fetchTools=()=>request.get<ToolDefinition[]>({url:'/api/v1/admin/tools'})
export const createTool=(params:ToolInput)=>request.post<ToolDefinition>({url:'/api/v1/admin/tools',params})
export const updateTool=(id:string,params:ToolInput)=>request.put<ToolDefinition>({url:`/api/v1/admin/tools/${id}`,params})
export const deleteTool=(id:string)=>request.del({url:`/api/v1/admin/tools/${id}`})
export const testTool=(id:string,input:Record<string,unknown>)=>request.post<{status_code:number;body:unknown}>({url:`/api/v1/admin/tools/${id}/test`,params:{input}})

export interface ApplicationConfig {model:string;prompt_template_id?:string;knowledge_base_ids:string[];tool_ids:string[];retrieval_top_k:number;temperature:number;max_tool_rounds:number}
export interface Application {id:string;code:string;name:string;description:string;status:'draft'|'active'|'suspended'|'retired';department_ids:string[];draft_config:ApplicationConfig;published_version?:number;published_config?:ApplicationConfig;revision:number}
export type ApplicationInput=Pick<Application,'code'|'name'|'description'|'status'|'department_ids'|'draft_config'>
export const fetchApplications=()=>request.get<Application[]>({url:'/api/v1/admin/applications'})
export const createApplication=(params:ApplicationInput)=>request.post<Application>({url:'/api/v1/admin/applications',params})
export const updateApplication=(id:string,params:ApplicationInput)=>request.put<Application>({url:`/api/v1/admin/applications/${id}`,params})
export const deleteApplication=(id:string)=>request.del({url:`/api/v1/admin/applications/${id}`})
export const publishApplication=(id:string,change_note:string)=>request.post<Application>({url:`/api/v1/admin/applications/${id}/publish`,params:{change_note}})
export const fetchApplicationVersions=(id:string)=>request.get<Record<string,unknown>[]>({url:`/api/v1/admin/applications/${id}/versions`})
export const fetchApplicationRuns=(id:string)=>request.get<Record<string,unknown>[]>({url:`/api/v1/admin/applications/${id}/runs`})

export interface NotificationChannel {id:string;name:string;webhook_url:string;event_patterns:string[];enabled:boolean;has_signing_secret:boolean;revision:number}
export interface NotificationDelivery {id:string;channel_name:string;event_id:string;event_type:string;status:string;attempts:number;response_status?:number;last_error:string;created_at:string}
export interface NotificationInput {name:string;webhook_url:string;signing_secret?:string;event_patterns:string[];enabled:boolean}
export const fetchNotificationChannels=()=>request.get<NotificationChannel[]>({url:'/api/v1/admin/notification-channels'})
export const createNotificationChannel=(params:NotificationInput)=>request.post<NotificationChannel>({url:'/api/v1/admin/notification-channels',params})
export const updateNotificationChannel=(id:string,params:NotificationInput)=>request.put<NotificationChannel>({url:`/api/v1/admin/notification-channels/${id}`,params})
export const deleteNotificationChannel=(id:string)=>request.del({url:`/api/v1/admin/notification-channels/${id}`})
export const fetchNotificationDeliveries=()=>request.get<NotificationDelivery[]>({url:'/api/v1/admin/notification-deliveries'})
export const retryNotificationDelivery=(id:string)=>request.post({url:`/api/v1/admin/notification-deliveries/${id}/retry`})
