import { api } from './http';

export type KnowledgeStatus = 'pending' | 'indexing' | 'ready' | 'failed';

export interface KnowledgeFile {
  id: string;
  originalName: string;
  filename: string;
  size: number;
  mime: string;
  status: KnowledgeStatus;
  error?: string;
  chunkCount: number;
  createdAt: number;
}

export type SkillType = 'prompt' | 'http' | 'js';

export interface SkillBrief {
  id: string;
  name: string;
  filename: string;
  description?: string;
  type: SkillType;
  enabled: number;
  createdAt: number;
}

export interface SkillDetail extends SkillBrief {
  content: string;
  parameters: string;
  handler: string;
  config: string;
}

export interface Agent {
  id: string;
  name: string;
  description: string;
  avatar: string;
  prompt: string;
  model: string;
  temperature: number;
  owner_id?: string | null;
  team_id?: string | null;
  visibility?: 'private' | 'team' | 'public';
  fork_count?: number;
  forked_from?: string | null;
  created_at: number;
  updated_at: number;
  knowledge?: KnowledgeFile[];
  skills?: SkillBrief[];
  _access?: 'owner' | 'team' | 'view' | 'none';
}

export const AgentAPI = {
  list: () => api.get<Agent[]>('/agents').then((r) => r.data),
  detail: (id: string) => api.get<Agent>(`/agents/${id}`).then((r) => r.data),
  create: (payload: Partial<Agent>) => api.post<Agent>('/agents', payload).then((r) => r.data),
  update: (id: string, payload: Partial<Agent>) => api.put<Agent>(`/agents/${id}`, payload).then((r) => r.data),
  remove: (id: string) => api.delete(`/agents/${id}`).then((r) => r.data),

  updateAvatar: (id: string, avatar: string) => api.patch<Agent>(`/agents/${id}/avatar`, { avatar }).then((r) => r.data),

  uploadKnowledge: (id: string, files: File[]) => {
    const fd = new FormData();
    files.forEach((f) => fd.append('files', f));
    return api.post(`/agents/${id}/knowledge`, fd).then((r) => r.data);
  },
  reindexKnowledge: (agentId: string, fileId: string) =>
    api.post(`/agents/${agentId}/knowledge/${fileId}/reindex`).then((r) => r.data),
  deleteKnowledge: (agentId: string, fileId: string) => api.delete(`/agents/${agentId}/knowledge/${fileId}`).then((r) => r.data),

  getKnowledgeStatus: (agentId: string) => api.get<{ files: KnowledgeFile[] }>(`/agents/${agentId}/knowledge/status`).then((r) => r.data),

  uploadSkills: (id: string, files: File[]) => {
    const fd = new FormData();
    files.forEach((f) => fd.append('files', f));
    return api.post(`/agents/${id}/skills`, fd).then((r) => r.data);
  },
  createSkill: (id: string, payload: { name: string; content: string }) => api.post(`/agents/${id}/skills/manual`, payload).then((r) => r.data),
  getSkill: (agentId: string, skillId: string) => api.get<SkillDetail>(`/agents/${agentId}/skills/${skillId}`).then((r) => r.data),
  updateSkill: (agentId: string, skillId: string, payload: { content?: string; enabled?: boolean }) =>
    api.put(`/agents/${agentId}/skills/${skillId}`, payload).then((r) => r.data),
  deleteSkill: (agentId: string, skillId: string) => api.delete(`/agents/${agentId}/skills/${skillId}`).then((r) => r.data)
};

