import request from '@/utils/http'
import type { PromptVariable } from './workbench'

export interface MarketplaceCategory { id: string; name: string; description: string; resource_type: string; sort_order: number; created_at: string }
export const fetchMarketplaceCategories = (params?: { type?: string }) => request.get<MarketplaceCategory[]>({ url: '/api/v1/admin/marketplace-categories', params })
export const createMarketplaceCategory = (params: { name: string; description: string; resource_type?: string; sort_order?: number }) => request.post<MarketplaceCategory>({ url: '/api/v1/admin/marketplace-categories', params })
export const deleteMarketplaceCategory = (id: string) => request.del({ url: `/api/v1/admin/marketplace-categories/${id}` })

export interface MCPTool { name: string; description: string; inputSchema: Record<string, unknown> }

export interface MCPServer {
  id: string; code: string; name: string; description: string; transport: string; endpoint_url: string
  status: 'draft' | 'published' | 'archived'; category_id?: string; category_name: string
  tags: string[]; department_ids: string[]; enabled: boolean; has_secret_headers: boolean; revision: number
}
export interface MCPServerInput {
  code: string; name: string; description: string; transport: string; endpoint_url: string
  headers?: Record<string, string>; status: string; category_id?: string | null
  tags: string[]; department_ids: string[]; enabled: boolean
}
export const fetchMCPServers = () => request.get<MCPServer[]>({ url: '/api/v1/admin/mcp-servers' })
export const createMCPServer = (params: MCPServerInput) => request.post<MCPServer>({ url: '/api/v1/admin/mcp-servers', params })
export const updateMCPServer = (id: string, params: MCPServerInput) => request.put<MCPServer>({ url: `/api/v1/admin/mcp-servers/${id}`, params })
export const deleteMCPServer = (id: string) => request.del({ url: `/api/v1/admin/mcp-servers/${id}` })
export const testMCPServer = (id: string) => request.post<MCPTool[]>({ url: `/api/v1/admin/mcp-servers/${id}/test` })

export interface Skill {
  id: string; code: string; name: string; description: string; content: string; variables: PromptVariable[]
  tool_ids: string[]; mcp_server_ids: string[]; knowledge_base_ids: string[]
  status: string; category_id?: string; category_name: string; tags: string[]; department_ids: string[]; enabled: boolean; revision: number
}
export interface SkillInput {
  code: string; name: string; description: string; content: string; variables: PromptVariable[]
  tool_ids: string[]; mcp_server_ids: string[]; knowledge_base_ids: string[]
  status: string; category_id?: string | null; tags: string[]; department_ids: string[]; enabled: boolean
}
export const fetchSkills = () => request.get<Skill[]>({ url: '/api/v1/admin/skills' })
export const createSkill = (params: SkillInput) => request.post<Skill>({ url: '/api/v1/admin/skills', params })
export const updateSkill = (id: string, params: SkillInput) => request.put<Skill>({ url: `/api/v1/admin/skills/${id}`, params })
export const deleteSkill = (id: string) => request.del({ url: `/api/v1/admin/skills/${id}` })
export const renderSkill = (id: string, variables: Record<string, unknown>) => request.post<{ code: string; name: string; rendered: string }>({ url: `/api/v1/admin/skills/${id}/render`, params: { variables } })

export interface DigitalEmployee {
  id: string; code: string; name: string; description: string; persona: string; model: string
  skill_ids: string[]; tool_ids: string[]; mcp_server_ids: string[]; knowledge_base_ids: string[]
  temperature: number; retrieval_top_k: number; max_tool_rounds: number
  status: string; category_id?: string; category_name: string; tags: string[]; department_ids: string[]; enabled: boolean; revision: number
}
export interface DigitalEmployeeInput {
  code: string; name: string; description: string; persona: string; model: string
  skill_ids: string[]; tool_ids: string[]; mcp_server_ids: string[]; knowledge_base_ids: string[]
  temperature: number; retrieval_top_k: number; max_tool_rounds: number
  status: string; category_id?: string | null; tags: string[]; department_ids: string[]; enabled: boolean
}
export interface DigitalEmployeeRun {
  id: string; digital_employee: string; request_id: string; status: string; error: string
  latency_ms: number; retrieval_count: number; tool_count: number; created_at: string
}
export const fetchDigitalEmployees = () => request.get<DigitalEmployee[]>({ url: '/api/v1/admin/digital-employees' })
export const createDigitalEmployee = (params: DigitalEmployeeInput) => request.post<DigitalEmployee>({ url: '/api/v1/admin/digital-employees', params })
export const updateDigitalEmployee = (id: string, params: DigitalEmployeeInput) => request.put<DigitalEmployee>({ url: `/api/v1/admin/digital-employees/${id}`, params })
export const deleteDigitalEmployee = (id: string) => request.del({ url: `/api/v1/admin/digital-employees/${id}` })
export const publishDigitalEmployee = (id: string) => request.post<DigitalEmployee>({ url: `/api/v1/admin/digital-employees/${id}/publish` })
export const archiveDigitalEmployee = (id: string) => request.post<DigitalEmployee>({ url: `/api/v1/admin/digital-employees/${id}/archive` })
export const fetchDigitalEmployeeRuns = (id: string) => request.get<DigitalEmployeeRun[]>({ url: `/api/v1/admin/digital-employees/${id}/runs` })

export interface MarketItem {
  type: string; code: string; name: string; description: string; category_id?: string; category_name: string
  tags: string[]; department_ids: string[]; updated_at: string
}
export const fetchMarketplaceCatalog = (params?: { type?: string; category_id?: string; tag?: string; q?: string; limit?: number }) => request.get<MarketItem[]>({ url: '/api/v1/admin/marketplace/catalog', params })
