"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cutsPublicRouter = exports.cutsAdminRouter = void 0;
const express_1 = require("express");
const multer_1 = __importDefault(require("multer"));
const cuts_service_1 = require("./cuts.service");
const cuts_schemas_1 = require("./cuts.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 geojsonUpload = (0, multer_1.default)({
    storage: multer_1.default.memoryStorage(),
    limits: { fileSize: 10 * 1024 * 1024 },
    fileFilter: (_req, file, cb) => {
        if (file.originalname.endsWith('.geojson') || file.originalname.endsWith('.json') || file.mimetype === 'application/json' || file.mimetype === 'application/geo+json') {
            cb(null, true);
        }
        else {
            cb(new Error('Only .geojson or .json files are allowed'));
        }
    },
});
// --- Admin Router ---
const adminRouter = (0, express_1.Router)();
exports.cutsAdminRouter = adminRouter;
adminRouter.use(auth_middleware_1.authenticate);
adminRouter.use((0, rbac_middleware_1.requireRole)(...roles_1.MAP_ROLES));
// GET /api/map/cuts — list paginated
adminRouter.get('/', (0, validate_1.validate)(cuts_schemas_1.listCutsSchema, 'query'), async (req, res, next) => {
    try {
        const result = await cuts_service_1.cutsService.findAll(req.query);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/cuts/export-geojson — all cuts as FeatureCollection
adminRouter.get('/export-geojson', async (_req, res, next) => {
    try {
        const collection = await cuts_service_1.cutsService.exportAllGeoJson();
        res.setHeader('Content-Type', 'application/geo+json');
        res.setHeader('Content-Disposition', 'attachment; filename=cuts.geojson');
        res.json(collection);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/map/cuts/import-geojson — upload + create cuts
adminRouter.post('/import-geojson', geojsonUpload.single('file'), async (req, res, next) => {
    try {
        if (!req.file) {
            res.status(400).json({ error: { message: 'No file uploaded', code: 'NO_FILE' } });
            return;
        }
        const result = await cuts_service_1.cutsService.importGeoJson(req.file.buffer, req.user.id);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/cuts/:id/export-geojson — single cut as Feature
adminRouter.get('/:id/export-geojson', async (req, res, next) => {
    try {
        const id = req.params.id;
        const feature = await cuts_service_1.cutsService.exportSingleGeoJson(id);
        res.setHeader('Content-Type', 'application/geo+json');
        res.setHeader('Content-Disposition', `attachment; filename=cut-${id}.geojson`);
        res.json(feature);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/cuts/:id — single cut
adminRouter.get('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        const cut = await cuts_service_1.cutsService.findById(id);
        res.json(cut);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/map/cuts — create cut
adminRouter.post('/', (0, validate_1.validate)(cuts_schemas_1.createCutSchema), async (req, res, next) => {
    try {
        const cut = await cuts_service_1.cutsService.create(req.body, req.user.id);
        res.status(201).json(cut);
    }
    catch (err) {
        next(err);
    }
});
// PUT /api/map/cuts/:id — update cut
adminRouter.put('/:id', (0, validate_1.validate)(cuts_schemas_1.updateCutSchema), async (req, res, next) => {
    try {
        const id = req.params.id;
        const cut = await cuts_service_1.cutsService.update(id, req.body);
        res.json(cut);
    }
    catch (err) {
        next(err);
    }
});
// DELETE /api/map/cuts/:id — delete cut
adminRouter.delete('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        await cuts_service_1.cutsService.delete(id);
        res.status(204).send();
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/cuts/:id/locations — locations within cut
adminRouter.get('/:id/locations', async (req, res, next) => {
    try {
        const id = req.params.id;
        const locations = await cuts_service_1.cutsService.getLocationsInCut(id);
        res.json(locations);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/cuts/:id/statistics — support level breakdown for cut
adminRouter.get('/:id/statistics', async (req, res, next) => {
    try {
        const id = req.params.id;
        const stats = await cuts_service_1.cutsService.getStatistics(id);
        res.json(stats);
    }
    catch (err) {
        next(err);
    }
});
// --- Public Router ---
const publicRouter = (0, express_1.Router)();
exports.cutsPublicRouter = publicRouter;
// GET /api/map/cuts/public — all public cuts for map display
publicRouter.get('/public', async (_req, res, next) => {
    try {
        const cuts = await cuts_service_1.cutsService.getPublicCuts();
        res.json(cuts);
    }
    catch (err) {
        next(err);
    }
});
//# sourceMappingURL=cuts.routes.js.map