/** Skills API client */

import { BaseApiClient } from './base'

const API_BASE = '/api/v1/skill-management'

export type SkillCategory = 'agent_template' | 'business_skill'

export interface ISkillInfo {
  name: string
  version: string
  description: string
  capabilities: string[]
  dependencies: string[]
  status: string
  /** "agent_template" = 通用执行引擎 (react/direct/rewoo/...); "business_skill" = 业务领域技能. Optional for backward compat during rollout. */
  category?: SkillCategory
  agent_type?: string
  execution_mode?: string
  task_mode?: string
}

export interface ISkillDetail {
  name: string
  version: string
  description: string
  capabilities: string[]
  dependencies: string[]
  config: Record<string, unknown>
  health_status: string
  category?: SkillCategory
  agent_type?: string
  execution_mode?: string
  task_mode?: string
}

export interface ICapabilityInfo {
  name: string
  display_name: string
  skill_count: number
}

export interface ISkillListResponse {
  skills: ISkillInfo[]
  total: number
  page: number
  size: number
}

class SkillsApiClient extends BaseApiClient {
  constructor(baseUrl: string = API_BASE) {
    super(baseUrl)
  }

  /** List all skills with optional filtering */
  async listSkills(
    capability?: string,
    page: number = 1,
    size: number = 20
  ): Promise<ISkillListResponse> {
    const params = new URLSearchParams()
    if (capability) params.set('capability', capability)
    params.set('page', String(page))
    params.set('size', String(size))
    return this.request<ISkillListResponse>(`/skills?${params.toString()}`)
  }

  /** Get skill detail */
  async getSkillDetail(skillName: string): Promise<ISkillDetail> {
    return this.request<ISkillDetail>(`/skills/${encodeURIComponent(skillName)}`)
  }

  /** Check skill health */
  async checkSkillHealth(
    skillName: string
  ): Promise<{ skill_name: string; status: string; message: string }> {
    return this.request(`/skills/${encodeURIComponent(skillName)}/health`)
  }

  /** List all capability tags */
  async listCapabilities(): Promise<{ capabilities: ICapabilityInfo[] }> {
    return this.request<{ capabilities: ICapabilityInfo[] }>('/capabilities')
  }

  /** Install a skill by name (searches GitHub) or from a source URL */
  async installSkill(
    name: string,
    source?: string
  ): Promise<{ status: string; name: string; path: string }> {
    // The install endpoint is on /api/v1/skills/install, not under skill-management
    return this.request<{ status: string; name: string; path: string }>(
      '/api/v1/skills/install',
      {
        method: 'POST',
        body: JSON.stringify({ name, source }),
      }
    )
  }

  /** Uninstall a skill */
  async uninstallSkill(skillName: string): Promise<{ status: string; name: string }> {
    return this.request<{ status: string; name: string }>(
      `/api/v1/skills/${encodeURIComponent(skillName)}`,
      { method: 'DELETE' }
    )
  }

  /** Reload a skill */
  async reloadSkill(
    skillName: string
  ): Promise<{ skill_name: string; status: string; message: string }> {
    return this.request(`/skills/${encodeURIComponent(skillName)}/reload`, {
      method: 'POST',
    })
  }
}

export const skillsApi = new SkillsApiClient()

// Backward-compatible standalone function exports
export const listSkills = skillsApi.listSkills.bind(skillsApi)
export const getSkillDetail = skillsApi.getSkillDetail.bind(skillsApi)
export const checkSkillHealth = skillsApi.checkSkillHealth.bind(skillsApi)
export const listCapabilities = skillsApi.listCapabilities.bind(skillsApi)
export const reloadSkill = skillsApi.reloadSkill.bind(skillsApi)
