"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pagesAdminRouter = void 0;
const express_1 = require("express");
const pages_service_1 = require("./pages.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 database_1 = require("../../config/database");
const roles_1 = require("../../utils/roles");
const router = (0, express_1.Router)();
exports.pagesAdminRouter = router;
router.use(auth_middleware_1.authenticate);
router.use((0, rbac_middleware_1.requireRole)(...roles_1.CONTENT_ROLES));
// GET /api/pages/view-counts — landing page view counts (last 30d)
router.get('/view-counts', async (_req, res, next) => {
    try {
        const since = new Date();
        since.setDate(since.getDate() - 30);
        const rows = await database_1.prisma.docsPageView.groupBy({
            by: ['path'],
            where: {
                path: { startsWith: '/p/' },
                createdAt: { gte: since },
            },
            _count: { id: true },
        });
        const counts = {};
        for (const row of rows) {
            // Extract slug from /p/:slug
            const slug = row.path.replace(/^\/p\//, '');
            counts[slug] = row._count.id;
        }
        res.json(counts);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/pages/sync — sync MkDocs overrides (must be before /:id routes)
router.post('/sync', async (_req, res, next) => {
    try {
        const result = await pages_service_1.pagesService.syncOverrides();
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/pages/validate — validate and repair MkDocs exports
router.post('/validate', async (_req, res, next) => {
    try {
        const result = await pages_service_1.pagesService.validateExports();
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/pages — list landing pages with pagination/filters
router.get('/', (0, validate_1.validate)(pages_schemas_1.listLandingPagesSchema, 'query'), async (req, res, next) => {
    try {
        const result = await pages_service_1.pagesService.findAll(req.query);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/pages/:id — get single landing page
router.get('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        const page = await pages_service_1.pagesService.findById(id);
        res.json(page);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/pages — create landing page
router.post('/', (0, validate_1.validate)(pages_schemas_1.createLandingPageSchema), async (req, res, next) => {
    try {
        const page = await pages_service_1.pagesService.create(req.body);
        res.status(201).json(page);
    }
    catch (err) {
        next(err);
    }
});
// PUT /api/pages/:id — update landing page
router.put('/:id', (0, validate_1.validate)(pages_schemas_1.updateLandingPageSchema), async (req, res, next) => {
    try {
        const id = req.params.id;
        const page = await pages_service_1.pagesService.update(id, req.body);
        res.json(page);
    }
    catch (err) {
        next(err);
    }
});
// DELETE /api/pages/:id — delete landing page
router.delete('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        await pages_service_1.pagesService.delete(id);
        res.status(204).send();
    }
    catch (err) {
        next(err);
    }
});
//# sourceMappingURL=pages-admin.routes.js.map