import client from '../client';
import type { PaginatedResponse } from '../types';

export interface PromptItem {
  id: string;
  name: string;
  description: string;
  system_prompt: string;
  user_prompt_template: string;
  model_config: Record<string, unknown>;
  version: number;
  is_active: boolean;
  category: string;
  analysis_type: string;
  tags: Record<string, unknown> | null;
  created_at: string;
  updated_at: string;
}

export interface CreatePromptReq {
  name: string;
  description?: string;
  system_prompt: string;
  user_prompt_template: string;
  model_config: Record<string, unknown>;
  category: string;
  analysis_type: string;
}

export const promptApi = {
  list: async (params?: { category?: string; analysis_type?: string; page?: number; page_size?: number }) => {
    const resp = await client.get('/ai/prompts', { params });
    return resp.data.data as PaginatedResponse<PromptItem>;
  },
  create: async (data: CreatePromptReq) => {
    const resp = await client.post('/ai/prompts', data);
    return resp.data.data as PromptItem;
  },
  activate: async (id: string) => {
    const resp = await client.post(`/ai/prompts/${id}/activate`);
    return resp.data.data as PromptItem;
  },
  deactivate: async (id: string) => {
    const resp = await client.post(`/ai/prompts/${id}/deactivate`);
    return resp.data.data as PromptItem;
  },
  rollback: async (id: string) => {
    const resp = await client.post(`/ai/prompts/${id}/rollback`);
    return resp.data.data as PromptItem;
  },
  delete: async (id: string) => {
    await client.delete(`/ai/prompts/${id}`);
  },
};
