import { apiRequest } from "./client";
import { arr, normalizeQuestion, normalizeSurvey } from "../utils/normalizers";

export function getSurveys() {
  return apiRequest("/api/surveys").then((data) => arr(data).map(normalizeSurvey));
}

export function getSurveyDetail(id) {
  return apiRequest(`/api/surveys/${id}`).then(normalizeSurvey);
}

export function getSurveyQuestions(id) {
  return apiRequest(`/api/surveys/${id}/questions`).then((data) => arr(data).map(normalizeQuestion));
}

export function getMySurveyResponses(id) {
  return apiRequest(`/api/surveys/${id}/my-responses`);
}

export function submitSurveyAnswers(id, answers) {
  return apiRequest(`/api/surveys/${id}/responses`, {
    method: "POST",
    data: { answers }
  });
}
