const { createEntityRouter, isUuidV4 } = require('../factories/router.factory');
const ProjectsService = require('../services/projects');
const ProjectsDBApi = require('../db/api/projects');
const { wrapAsync } = require('../helpers');

// Create base router with factory (includes all standard CRUD endpoints)
const router = createEntityRouter('projects', ProjectsService, ProjectsDBApi, {
  permissionEntity: 'projects',
  csvFields: [
    'id',
    'name',
    'slug',
    'description',
    'logo_url',
    'favicon_url',
    'og_image_url',
  ],
});

// Custom endpoint: Clone project
router.post(
  '/:id/clone',
  wrapAsync(async (req, res) => {
    if (!isUuidV4(req.params.id)) {
      return res.status(400).send('Invalid project id');
    }

    const payload = await ProjectsService.cloneFromProject(
      req.params.id,
      req.currentUser,
    );
    res.status(200).send(payload);
  }),
);

module.exports = router;
