import { api } from "@/lib/api-client";
import type {
  PlacementSubject,
  CATStartResponse,
  CATAnswer,
  CATAnswerResponse,
  CATAutoSave,
  PlacementResult,
  LearningPathPreview,
} from "@/types";
import type { ApiSuccessResponse } from "@/types";

export const placementService = {
  start: (subject: PlacementSubject) =>
    api.post<CATStartResponse>("/placement/start", { subject }),

  submitAnswer: (data: CATAnswer) =>
    api.post<CATAnswerResponse>("/placement/answer", data),

  autoSave: (data: CATAutoSave) =>
    api.post<ApiSuccessResponse>("/placement/autosave", data),

  uploadSpeaking: (sessionId: string, promptId: number, audioFile: File) => {
    const formData = new FormData();
    formData.append("session_id", sessionId);
    formData.append("prompt_id", String(promptId));
    formData.append("audio_file", audioFile);
    return api.upload<ApiSuccessResponse>("/placement/speaking-upload", formData);
  },

  getResults: (sessionId: string) =>
    api.get<PlacementResult>("/placement/results", { session_id: sessionId }),

  getSpeakingStatus: (sessionId: string) =>
    api.get<{ status: "pending" | "scored" }>("/placement/speaking-status", { session_id: sessionId }),

  getLearningPath: (sessionId: string) =>
    api.get<LearningPathPreview>("/placement/learning-path", { session_id: sessionId }),

  startTrial: () =>
    api.post<ApiSuccessResponse>("/subscription/trial"),
};
