const Project_element_defaultsService = require('../services/project_element_defaults');
const Project_element_defaultsDBApi = require('../db/api/project_element_defaults');
const { createEntityRouter } = require('../factories/router.factory');
const wrapAsync = require('../helpers').wrapAsync;

// Create base router with standard CRUD operations
const baseRouter = createEntityRouter(
  'project_element_defaults',
  Project_element_defaultsService,
  Project_element_defaultsDBApi,
  {
    permissionEntity: 'page_elements',
  },
);

/**
 * @swagger
 * /api/project-element-defaults/{id}/reset:
 *   post:
 *     security:
 *       - bearerAuth: []
 *     tags: [Project Element Defaults]
 *     summary: Reset project element default to global
 *     description: Resets a project element default to the current global element type default settings
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: string
 *         required: true
 *         description: Project element default ID
 *     responses:
 *       200:
 *         description: Successfully reset
 *       404:
 *         description: Project element default not found
 */
baseRouter.post(
  '/:id/reset',
  wrapAsync(async (req, res) => {
    const payload = await Project_element_defaultsService.resetToGlobal(
      req.params.id,
      { currentUser: req.currentUser },
    );
    res.status(200).json(payload);
  }),
);

/**
 * @swagger
 * /api/project-element-defaults/{id}/diff:
 *   get:
 *     security:
 *       - bearerAuth: []
 *     tags: [Project Element Defaults]
 *     summary: Get diff from global default
 *     description: Compares project element default with the current global element type default
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: string
 *         required: true
 *         description: Project element default ID
 *     responses:
 *       200:
 *         description: Diff result
 *       404:
 *         description: Project element default not found
 */
baseRouter.get(
  '/:id/diff',
  wrapAsync(async (req, res) => {
    const payload = await Project_element_defaultsService.getDiffFromGlobal(
      req.params.id,
    );
    res.status(200).json(payload);
  }),
);

module.exports = baseRouter;
