"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.blocksRouter = void 0;
const express_1 = require("express");
const blocks_service_1 = require("./blocks.service");
const pages_schemas_1 = require("./pages.schemas");
const validate_1 = require("../../middleware/validate");
const auth_middleware_1 = require("../../middleware/auth.middleware");
const rbac_middleware_1 = require("../../middleware/rbac.middleware");
const roles_1 = require("../../utils/roles");
const router = (0, express_1.Router)();
exports.blocksRouter = router;
router.use(auth_middleware_1.authenticate);
router.use((0, rbac_middleware_1.requireRole)(...roles_1.CONTENT_ROLES));
// GET /api/page-blocks — list all blocks
router.get('/', (0, validate_1.validate)(pages_schemas_1.listPageBlocksSchema, 'query'), async (req, res, next) => {
    try {
        const blocks = await blocks_service_1.blocksService.findAll(req.query);
        res.json(blocks);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/page-blocks/:id — get single block
router.get('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        const block = await blocks_service_1.blocksService.findById(id);
        res.json(block);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/page-blocks — create block
router.post('/', (0, validate_1.validate)(pages_schemas_1.createPageBlockSchema), async (req, res, next) => {
    try {
        const block = await blocks_service_1.blocksService.create(req.body);
        res.status(201).json(block);
    }
    catch (err) {
        next(err);
    }
});
// PUT /api/page-blocks/:id — update block
router.put('/:id', (0, validate_1.validate)(pages_schemas_1.updatePageBlockSchema), async (req, res, next) => {
    try {
        const id = req.params.id;
        const block = await blocks_service_1.blocksService.update(id, req.body);
        res.json(block);
    }
    catch (err) {
        next(err);
    }
});
// DELETE /api/page-blocks/:id — delete block
router.delete('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        await blocks_service_1.blocksService.delete(id);
        res.status(204).send();
    }
    catch (err) {
        next(err);
    }
});
//# sourceMappingURL=blocks.routes.js.map