import { http } from '@/services/api/http'
import { buildUrl, endpoints } from '@/services/api/endpoints'

export const apiGetAdminExams = (params) => http.get(endpoints.getExamsList, { params })

export const apiShowAdminExam = (id) => http.get(buildUrl(endpoints.showExam, { id }))

export const apiAddAdminExam = (payload) => http.post(endpoints.addNewExam, payload)

// Backend uses PATCH /exams/:id (not PUT).
export const apiUpdateAdminExam = (id, payload) =>
  http.patch(buildUrl(endpoints.updateExam, { id }), payload)

export const apiDeleteAdminExam = (id) => http.delete(buildUrl(endpoints.deleteExam, { id }))

// POST /exams/:examId/questions — payload: { questionText, position, options: [{ optionText, isCorrect }] }
// Must include ≥2 options; question + options created atomically.
export const apiAddAdminExamQuestion = (examId, payload) =>
  http.post(buildUrl(endpoints.addExamQuestion, { examId }), payload)

// POST /questions/:questionId/options — payload: { optionText, isCorrect }
export const apiAddAdminQuestionOption = (questionId, payload) =>
  http.post(buildUrl(endpoints.addQuestionOption, { questionId }), payload)

// GET /exams/:examId/attempts — paginated list, each row is one attempt with
// the attempting user embedded (multiple rows per user possible).
export const apiGetAdminExamAttempts = (examId, params) =>
  http.get(buildUrl(endpoints.getExamAttempts, { examId }), { params })

export const apiShowAdminExamParticipant = (examId, participantId) =>
  http.get(buildUrl(endpoints.showExamParticipant, { examId, participantId }))

// GET /exam-attempts/:id — full per-attempt review: scoring, the user, the
// term/course/session context, and every question with all options carrying
// isCorrect (answer key) and isSelected (what this user picked).
export const apiShowAdminExamAttempt = (id) => http.get(buildUrl(endpoints.showExamAttempt, { id }))
