import api from './index'

// ========== AI General Services ==========
export const aiApi = {
  chat: (data: { messages: Array<{ role: string; content: string }>; system_prompt?: string; max_tokens?: number }) =>
    api.post('/ai/chat', data),
  health: () => api.get('/ai/health'),
  logs: (params?: { limit?: number }) => api.get('/ai/logs', { params }),
  usage: () => api.get('/ai/usage'),
}

// ========== L0 Pre-screening + Feasibility Search ==========
export const searchApi = {
  l0Check: (params: { params: Record<string, any> }) =>
    api.post('/search/l0/check', params),
  l0Filter: (paramSets: Array<Record<string, any>>) =>
    api.post('/search/l0/filter', paramSets),
  create: (data: {
    parameters: Array<{ name: string; min_value: number; max_value: number; step?: number; unit?: string }>
    total_budget?: number
    batch_size?: number
    initial_samples?: number
    objective_metric?: string
    objective_direction?: string
    seed?: number
  }) => api.post('/search/create', data),
  getState: (runId: string) => api.get(`/search/${runId}/state`),
  nextBatch: (runId: string) => api.post(`/search/${runId}/next-batch`),
  reportResult: (runId: string, data: { point_id: number; metrics: Record<string, number>; status?: string }) =>
    api.post(`/search/${runId}/report`, data),
  getPoints: (runId: string, status?: string) =>
    api.get(`/search/${runId}/points`, { params: status ? { status } : {} }),
  exportState: (runId: string) => api.get(`/search/${runId}/export`),
  listRuns: () => api.get('/search/runs'),
}

// ========== AI Plan Generation ==========
export const planGenApi = {
  generate: (data: { requirement: string; project_context?: Record<string, any>; existing_experience?: Array<any> }) =>
    api.post('/ai-plan/generate', data),
  refine: (data: { original_plan: Record<string, any>; feedback: string }) =>
    api.post('/ai-plan/refine', data),
  listTemplates: () => api.get('/ai-plan/templates'),
}

// ========== AI Result Analysis ==========
export const analysisApi = {
  analyze: (data: {
    results: Array<Record<string, any>>
    targets?: Record<string, number>
    fidelity?: string
    scan_parameters?: string[]
  }) => api.post('/analysis/analyze', data),
  calibrate: (data: { metric: string; value: number; fidelity: string }) =>
    api.post('/analysis/calibrate', data),
  updateCalibration: (data: {
    low_fidelity_results: Record<string, number>
    high_fidelity_results: Record<string, number>
    low_fidelity: string
    high_fidelity?: string
  }) => api.post('/analysis/calibration/update', data),
  getFidelityLevels: () => api.get('/analysis/fidelity-levels'),
  getConfidenceScale: () => api.get('/analysis/confidence/scale'),
  getConvergenceCriteria: () => api.get('/analysis/convergence/criteria'),
}

// ========== Adaptive Closed Loop ==========
export const adaptiveApi = {
  createLoop: (data: { user_requirement: string; total_budget?: number; batch_size?: number }) =>
    api.post('/adaptive/loops', data),
  listLoops: () => api.get('/adaptive/loops'),
  getLoop: (loopId: string) => api.get(`/adaptive/loops/${loopId}`),
  generatePlan: (loopId: string) => api.post(`/adaptive/loops/${loopId}/generate-plan`),
  initSearch: (loopId: string) => api.post(`/adaptive/loops/${loopId}/init-search`),
  nextBatch: (loopId: string) => api.post(`/adaptive/loops/${loopId}/next-batch`),
  reportResults: (loopId: string, data: { point_results: Array<{ point_id: number; metrics: Record<string, number>; status?: string }> }) =>
    api.post(`/adaptive/loops/${loopId}/report-results`, data),
  updateExperience: (loopId: string) => api.post(`/adaptive/loops/${loopId}/update-experience`),
  checkCompletion: (loopId: string) => api.get(`/adaptive/loops/${loopId}/check-completion`),
  getPhases: () => api.get('/adaptive/phases'),
}
