import { api } from "@/lib/api-client";

export interface AiSearchResponse {
  answer: string;
  suggestions: string[];
  related_actions?: { label: string; action: string }[];
}

export interface AiInsightItem {
  title: string;
  description: string;
  severity: "info" | "warning" | "critical";
  recommendation: string;
}

export interface AiAlertItem {
  title: string;
  description: string;
  severity: string;
  recommendation?: string;
}

export interface BatchOptimizeResponse {
  optimized: unknown[];
  summary: string;
  impact: string;
}

export interface AiGradingResult {
  scores: Record<string, number>;
  overall_band: number;
  feedback: string;
  suggestions: string[];
}

export const analyticsService = {
  async getStudentAnalytics(params?: Record<string, string | number | boolean | undefined>): Promise<unknown> {
    return api.get("/analytics/student", params);
  },

  async getClassAnalytics(params?: Record<string, string | number | boolean | undefined>): Promise<unknown> {
    return api.get("/analytics/class", params);
  },

  async getSubjectAnalytics(params?: Record<string, string | number | boolean | undefined>): Promise<unknown> {
    return api.get("/analytics/subject", params);
  },

  async getContentGaps(params?: { subject_id?: number }): Promise<{ gaps: { topic_id: number; topic_name: string; resource_count: number }[] }> {
    return api.get("/analytics/content-gaps", params as Record<string, string | number | boolean | undefined>);
  },

  async search(query: string): Promise<AiSearchResponse> {
    return api.post<AiSearchResponse>("/ai/search", { query });
  },

  async getInsights(data: Record<string, unknown>): Promise<{ insights: AiInsightItem[] }> {
    return api.post<{ insights: AiInsightItem[] }>("/ai/insights", { data, type: "general" });
  },

  async getAlerts(): Promise<{ alerts: AiAlertItem[] }> {
    return api.get<{ alerts: AiAlertItem[] }>("/ai/alerts");
  },

  async getReportNarrative(data: { report_type: string; data: Record<string, unknown> }): Promise<{ narrative: string }> {
    return api.post("/ai/report-narrative", data);
  },

  async getBatchOptimization(batchId: number, items: unknown[] = [], type = "schedule"): Promise<BatchOptimizeResponse> {
    return api.post<BatchOptimizeResponse>("/ai/batch-optimize", { items, type });
  },

  async getGradingSuggestion(data: {
    submission_text: string;
    skill?: string;
    rubric?: string;
    task?: string;
  }): Promise<AiGradingResult> {
    return api.post<AiGradingResult>("/ai/grade-suggest", data);
  },

  async applyBatchOptimization(batchId: number, optimized: unknown[]): Promise<{ applied: number }> {
    return api.post("/ai/batch-optimize/apply", { batch_id: batchId, optimized });
  },

  async vectorSearch(query: string, options?: { content_type?: string; limit?: number }): Promise<{
    results: { content_type: string; content_id: number; text: string; metadata: Record<string, unknown>; similarity: number }[];
    query: string;
    count: number;
  }> {
    return api.get("/ai/vector-search", { q: query, ...options } as Record<string, string | number | boolean | undefined>);
  },
};
