import api from "./api.js";
import { unwrapData, unwrapList } from "./http.js";

export async function listForms(query = {}) {
  const res = await api.get("/ptw_form_details", { params: query });
  return unwrapList(res);
}

export async function createForm(payload) {
  const res = await api.post("/ptw_form_details", payload);
  return unwrapData(res);
}

export async function updateForm(formId, payload) {
  const res = await api.put(`/ptw_form_details/${formId}`, payload);
  return unwrapData(res);
}

export async function deleteForm(formId) {
  const res = await api.delete(`/ptw_form_details/${formId}`);
  return unwrapData(res);
}

export async function getForm(formId) {
  const res = await api.get(`/ptw_form_details/${formId}`);
  return unwrapData(res);
}

export async function listSections(query = {}) {
  const res = await api.get("/ptw_section_details", { params: query });
  return unwrapList(res);
}

export async function createSection(payload) {
  const res = await api.post("/ptw_section_details", payload);
  return unwrapData(res);
}

export async function updateSection(sectionId, payload) {
  const res = await api.put(`/ptw_section_details/${sectionId}`, payload);
  return unwrapData(res);
}

export async function deleteSection(sectionId) {
  const res = await api.delete(`/ptw_section_details/${sectionId}`);
  return unwrapData(res);
}

export async function getSection(sectionId) {
  const res = await api.get(`/ptw_section_details/${sectionId}`);
  return unwrapData(res);
}

export async function listQuestions(query = {}) {
  const res = await api.get("/ptw_questions", { params: query });
  return unwrapList(res);
}

export async function createQuestion(payload) {
  const res = await api.post("/ptw_questions", payload);
  return unwrapData(res);
}

export async function updateQuestion(questionId, payload) {
  const res = await api.put(`/ptw_questions/${questionId}`, payload);
  return unwrapData(res);
}

export async function deleteQuestion(questionId) {
  const res = await api.delete(`/ptw_questions/${questionId}`);
  return unwrapData(res);
}

export async function getQuestion(questionId) {
  const res = await api.get(`/ptw_questions/${questionId}`);
  return unwrapData(res);
}

export async function createQuestionOption(payload) {
  const res = await api.post("/ptw_question_options", payload);
  return unwrapData(res);
}

export async function listQuestionOptions(query = {}) {
  const res = await api.get("/ptw_question_options", { params: query });
  return unwrapList(res);
}

export async function deleteQuestionOption(questionOptionsId) {
  const res = await api.delete(`/ptw_question_options/${questionOptionsId}`);
  return unwrapData(res);
}

export async function listFormSectionMaps(query = {}) {
  const res = await api.get("/ptw_form_section_map", { params: query });
  return unwrapList(res);
}

export async function getFormWithSections(formId) {
  const res = await api.get(`/ptw_form_section_map/form/${formId}`);
  return unwrapData(res);
}

/** Form metadata, ordered sections, and each section's questions with options. */
export async function loadFormForFill(formId) {
  const form = await getFormWithSections(formId);
  const sections = Array.isArray(form?.sections) ? form.sections : [];
  const sectionsWithQuestions = await Promise.all(
    sections.map(async (sec) => {
      const detail = await getSectionWithQuestions(sec.section_id);
      return {
        ...sec,
        questions: Array.isArray(detail?.questions) ? detail.questions : []
      };
    })
  );
  return { ...form, sections: sectionsWithQuestions };
}

export async function mapFormSection(payload) {
  const res = await api.post("/ptw_form_section_map", payload);
  return unwrapData(res);
}

export async function updateFormSectionMap(formId, sectionId, payload) {
  const res = await api.put(
    `/ptw_form_section_map/${formId}/${sectionId}`,
    payload
  );
  return unwrapData(res);
}

export async function reorderFormSections(formId, sections) {
  const res = await api.put(`/ptw_form_section_map/form/${formId}/reorder`, {
    sections
  });
  return unwrapData(res);
}

export async function deleteFormSectionMap(formId, sectionId) {
  const res = await api.delete(`/ptw_form_section_map/${formId}/${sectionId}`);
  return unwrapData(res);
}

export async function listSectionQuestionMaps(query = {}) {
  const res = await api.get("/ptw_section_question_map", { params: query });
  return unwrapList(res);
}

export async function getSectionWithQuestions(sectionId) {
  const res = await api.get(`/ptw_section_question_map/section/${sectionId}`);
  return unwrapData(res);
}

export async function mapSectionQuestion(payload) {
  const res = await api.post("/ptw_section_question_map", payload);
  return unwrapData(res);
}

export async function deleteSectionQuestionMap(sectionId, questionId) {
  const res = await api.delete(
    `/ptw_section_question_map/${sectionId}/${questionId}`
  );
  return unwrapData(res);
}

export async function mapFormLocation(payload) {
  const res = await api.post("/ptw_location_forms_details", payload);
  return unwrapData(res);
}

export async function setSkipLogic(payload) {
  const res = await api.post("/ptw_alternative_sections", payload);
  return unwrapData(res);
}

export async function publishNestedForm(payload) {
  const res = await api.post("/form-builder", payload);
  return unwrapData(res);
}

export async function listFormLogs(query = {}) {
  const res = await api.get("/ptw_form_logs", { params: query });
  return unwrapList(res);
}

export async function listSectionLogs(query = {}) {
  const res = await api.get("/ptw_section_logs", { params: query });
  return unwrapList(res);
}
