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

export interface AnalysisItem {
  id: string;
  patient_id: string;
  patient_name?: string;
  analysis_type: string;
  source_ref: string;
  model_used: string;
  status: string;
  result_content: string | null;
  result_metadata: Record<string, unknown> | null;
  error_message: string | null;
  created_at: string;
  updated_at: string;
}

export interface HealthSummaryResponse {
  patient_id: string;
  risk_level: 'low' | 'medium' | 'high' | 'critical';
  active_insights_count: number;
  recent_analyses_count: number;
  latest_insight_title: string | null;
  latest_analysis_type: string | null;
  summary_items: Array<{
    category: string;
    title: string;
    severity: string | null;
    created_at: string;
  }>;
}

export const analysisApi = {
  list: async (params?: { patient_id?: string; analysis_type?: string; page?: number; page_size?: number }) => {
    const resp = await client.get('/ai/analysis/history', { params });
    return resp.data.data as PaginatedResponse<AnalysisItem>;
  },
  get: async (id: string) => {
    const resp = await client.get(`/ai/analysis/${id}`);
    return resp.data.data as AnalysisItem;
  },
  getHealthSummary: async (patientId: string) => {
    const resp = await client.get('/ai/health-summary', { params: { patient_id: patientId } });
    return resp.data.data as HealthSummaryResponse;
  },
};
