import { api } from "@/lib/api-client";
import type {
  Proficiency, ProficiencySummary, LearningPlan,
  DiagnosticSession, DiagnosticQuestion, DiagnosticAnswer, DiagnosticResult,
  TopicContent, ApiSuccessResponse,
} from "@/types";

export const adaptiveService = {
  async startDiagnostic(subjectId: number): Promise<{ session: DiagnosticSession; first_question: DiagnosticQuestion }> {
    return api.post("/diagnostic/start", { subject_id: subjectId });
  },

  async answerDiagnostic(data: DiagnosticAnswer): Promise<{ next_question?: DiagnosticQuestion; completed: boolean }> {
    return api.post("/diagnostic/answer", data);
  },

  async getDiagnosticResult(sessionId: number): Promise<DiagnosticResult> {
    return api.get(`/diagnostic/${sessionId}/result`);
  },

  async getProficiency(params: { subject_id?: number }): Promise<Proficiency[]> {
    const raw = await api.get<{ data: Proficiency[] } | Proficiency[]>("/proficiency", params as Record<string, string | number | boolean | undefined>);
    return Array.isArray(raw) ? raw : (raw as { data: Proficiency[] }).data ?? [];
  },

  async getProficiencySummary(): Promise<ProficiencySummary[]> {
    const raw = await api.get<{ data: ProficiencySummary[] } | ProficiencySummary[]>("/proficiency/summary");
    return Array.isArray(raw) ? raw : (raw as { data: ProficiencySummary[] }).data ?? [];
  },

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

  async getLearningPlan(params: { subject_id?: number }): Promise<LearningPlan> {
    return api.get<LearningPlan>("/learning-plan", params as Record<string, string | number | boolean | undefined>);
  },

  async generateLearningPlan(data: { subject_id: number; target_completion?: string }): Promise<LearningPlan> {
    return api.post<LearningPlan>("/learning-plan/generate", data);
  },

  async updateLearningPlan(id: number, data: Partial<LearningPlan>): Promise<LearningPlan> {
    return api.patch<LearningPlan>(`/learning-plan/${id}`, data);
  },

  async pauseLearningPlan(id: number): Promise<LearningPlan> {
    return api.post<LearningPlan>(`/learning-plan/${id}/pause`);
  },

  async resumeLearningPlan(id: number): Promise<LearningPlan> {
    return api.post<LearningPlan>(`/learning-plan/${id}/resume`);
  },

  async getTopicContent(topicId: number): Promise<TopicContent> {
    return api.get<TopicContent>(`/topics/${topicId}/content`);
  },

  async generateTopicContent(topicId: number): Promise<TopicContent> {
    return api.post<TopicContent>(`/topics/${topicId}/generate-content`);
  },

  async getPracticeQuestions(topicId: number): Promise<{ questions: unknown[] }> {
    return api.post(`/topics/${topicId}/practice`);
  },

  async gradePractice(topicId: number, answers: unknown[]): Promise<{ results: unknown[]; mastery_update: number }> {
    return api.post(`/topics/${topicId}/practice/grade`, { answers });
  },

  async startMasteryQuiz(topicId: number): Promise<{ questions: unknown[] }> {
    return api.post(`/topics/${topicId}/mastery-quiz`);
  },

  async submitMasteryQuiz(topicId: number, answers: unknown[]): Promise<{ passed: boolean; score: number; mastery_update: number }> {
    return api.post(`/topics/${topicId}/mastery-quiz/submit`, { answers });
  },
};
