import express from "express";
import { createCrudRouter } from "./crudRouter.js";
import { createFormWithChildren } from "../controllers/ptwFormBuilderController.js";
import {
  createForm,
  updateForm,
  deleteForm
} from "../controllers/ptwFormController.js";
import {
  createSection,
  updateSection,
  deleteSection
} from "../controllers/ptwSectionController.js";
import {
  listRecords,
  getRecordByPk
} from "../controllers/genericCrudController.js";
import {
  createSectionQuestionMap,
  getSectionWithQuestions
} from "../controllers/ptwSectionQuestionMapController.js";
import {
  createFormSectionMap,
  getFormWithSections,
  reorderFormSections
} from "../controllers/ptwFormSectionMapController.js";

const router = express.Router();

// Nested transactional builder: Form -> Sections -> Questions -> Options -> maps.
router.post("/form-builder", createFormWithChildren);

// ptw_form_details: dedicated controller (location maps, versioning, form logs).
router.get("/ptw_form_details", listRecords("ptw_form_details"));
router.get("/ptw_form_details/:form_id", getRecordByPk("ptw_form_details", ["form_id"]));
router.post("/ptw_form_details", createForm);
router.put("/ptw_form_details/:form_id", updateForm);
router.delete("/ptw_form_details/:form_id", deleteForm);

// ptw_section_details: dedicated controller (versioning, section logs).
router.get("/ptw_section_details", listRecords("ptw_section_details"));
router.get(
  "/ptw_section_details/:section_id",
  getRecordByPk("ptw_section_details", ["section_id"])
);
router.post("/ptw_section_details", createSection);
router.put("/ptw_section_details/:section_id", updateSection);
router.delete("/ptw_section_details/:section_id", deleteSection);

// Questions + options.
router.use("/ptw_questions", createCrudRouter("ptw_questions", ["question_id"]));
router.use(
  "/ptw_question_options",
  createCrudRouter("ptw_question_options", ["question_options_id"])
);

// Composite-key mapping tables.
router.use(
  "/ptw_location_forms_details",
  createCrudRouter("ptw_location_forms_details", ["form_id", "location_id"])
);
router.get(
  "/ptw_section_question_map/section/:section_id",
  getSectionWithQuestions
);
router.post("/ptw_section_question_map", createSectionQuestionMap);
router.use(
  "/ptw_section_question_map",
  createCrudRouter("ptw_section_question_map", ["section_id", "question_id"])
);
router.get("/ptw_form_section_map/form/:form_id", getFormWithSections);
router.put("/ptw_form_section_map/form/:form_id/reorder", reorderFormSections);
router.post("/ptw_form_section_map", createFormSectionMap);
router.use(
  "/ptw_form_section_map",
  createCrudRouter("ptw_form_section_map", ["form_id", "section_id"])
);
router.use(
  "/ptw_alternative_sections",
  createCrudRouter("ptw_alternative_sections", [
    "skipable_section_id",
    "other_section_id"
  ])
);

// Archive + log read tables.
router.use("/ptw_archive_form", createCrudRouter("ptw_archive_form", ["form_id"]));
router.use("/ptw_archive_section", createCrudRouter("ptw_archive_section", ["section_id"]));
router.use("/ptw_form_logs", createCrudRouter("ptw_form_logs", ["log_id"]));
router.use("/ptw_section_logs", createCrudRouter("ptw_section_logs", ["log_id"]));

export default router;
