const express = require("express");
const router = express.Router();
const formController = require("../controllers/formController");
const authenticate = require("../middlewares/authenticate");
const authorize = require("../middlewares/authorize");

/**
 * @swagger
 * tags:
 *   name: Forms
 *   description: Form management APIs
 */

// CREATE form (admin only)
router.post("/create", authenticate, authorize("admin"), (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Create a new form'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.createForm(req, res);
});

// GET all forms (admin + manager allowed)
router.get("/get/all", authenticate, authorize(["admin", "manager"]), (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Get a list of all forms'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.getForms(req, res);
});

// GET form by ID (all logged in users)
router.get("/get/:id", authenticate, (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Get a form by ID'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.getFormById(req, res);
});

// UPDATE form (admin only)
router.put("/update/:id", authenticate, authorize("admin"), (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Update a form by ID'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.updateForm(req, res);
});

// UPDATE form status (admin only)
router.put("/update/status/:id", authenticate, authorize("admin"), (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Update a form Status by ID'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.updateFormStatus(req, res);
});

// SUBMIT a form (all logged in users)
router.post("/post/submit/:formId", authenticate, (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Submit a form'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.submitForm(req, res);
});

// GET form submissions by ID (admin + manager allowed)
router.get("/get/submissions/:formId", authenticate, authorize(["admin", "manager"]), (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Get form submissions by ID'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.getFormSubmissionsById(req, res);
});

// DELETE form (admin only)
router.delete("/delete/:id", authenticate, authorize("admin"), (req, res) => {
  // #swagger.tags = ['Forms']
  // #swagger.summary = 'Delete a form by ID'
  // #swagger.security = [{ "bearerAuth": [] }]

  formController.deleteForm(req, res);
});

module.exports = router;
