"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.trackingAdminRouter = exports.trackingVolunteerRouter = void 0;
const express_1 = require("express");
const tracking_service_1 = require("./tracking.service");
const tracking_schemas_1 = require("./tracking.schemas");
const validate_1 = require("../../../middleware/validate");
const auth_middleware_1 = require("../../../middleware/auth.middleware");
const rbac_middleware_1 = require("../../../middleware/rbac.middleware");
const rate_limit_1 = require("../../../middleware/rate-limit");
const roles_1 = require("../../../utils/roles");
// ─── Volunteer Router ────────────────────────────────────────────────
const volunteerRouter = (0, express_1.Router)();
exports.trackingVolunteerRouter = volunteerRouter;
volunteerRouter.use(auth_middleware_1.authenticate);
// POST /api/map/tracking/sessions — start tracking session
volunteerRouter.post('/sessions', (0, validate_1.validate)(tracking_schemas_1.startTrackingSchema), async (req, res, next) => {
    try {
        const session = await tracking_service_1.trackingService.startSession(req.user.id, req.body);
        res.status(201).json(session);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/map/tracking/sessions/:id/end — end tracking session
volunteerRouter.post('/sessions/:id/end', async (req, res, next) => {
    try {
        const id = req.params.id;
        const session = await tracking_service_1.trackingService.endSession(id, req.user.id);
        if (!session) {
            res.status(404).json({ error: { message: 'Active tracking session not found', code: 'NOT_FOUND' } });
            return;
        }
        res.json(session);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/map/tracking/sessions/:id/points — submit GPS point batch
volunteerRouter.post('/sessions/:id/points', rate_limit_1.gpsTrackingRateLimit, (0, validate_1.validate)(tracking_schemas_1.submitPointsSchema), async (req, res, next) => {
    try {
        const id = req.params.id;
        const result = await tracking_service_1.trackingService.submitPoints(id, req.user.id, req.body);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/map/tracking/sessions/:id/link-canvass — link to canvass session
volunteerRouter.post('/sessions/:id/link-canvass', (0, validate_1.validate)(tracking_schemas_1.linkCanvassSchema), async (req, res, next) => {
    try {
        const id = req.params.id;
        const session = await tracking_service_1.trackingService.linkCanvassSession(id, req.body);
        res.json(session);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/tracking/my/session — get active tracking session
volunteerRouter.get('/my/session', async (req, res, next) => {
    try {
        const session = await tracking_service_1.trackingService.getActiveSession(req.user.id);
        res.json(session);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/tracking/my/sessions — paginated list of own tracking sessions
volunteerRouter.get('/my/sessions', (0, validate_1.validate)(tracking_schemas_1.myHistoricalListQuerySchema, 'query'), async (req, res, next) => {
    try {
        const query = req.query;
        const result = await tracking_service_1.trackingService.getHistoricalSessions({
            ...query,
            userId: req.user.id,
        });
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/tracking/my/sessions/:id/route — full route for own session
volunteerRouter.get('/my/sessions/:id/route', async (req, res, next) => {
    try {
        const id = req.params.id;
        const route = await tracking_service_1.trackingService.getMySessionRoute(id, req.user.id);
        if (!route) {
            res.status(404).json({ error: { message: 'Session not found', code: 'NOT_FOUND' } });
            return;
        }
        res.json(route);
    }
    catch (err) {
        next(err);
    }
});
// ─── Admin Router ────────────────────────────────────────────────────
const adminRouter = (0, express_1.Router)();
exports.trackingAdminRouter = adminRouter;
adminRouter.use(auth_middleware_1.authenticate);
adminRouter.use((0, rbac_middleware_1.requireRole)(...roles_1.MAP_ROLES));
// GET /api/map/tracking/live — active volunteers with positions + recent trails
adminRouter.get('/live', (0, validate_1.validate)(tracking_schemas_1.liveVolunteersQuerySchema, 'query'), async (req, res, next) => {
    try {
        const volunteers = await tracking_service_1.trackingService.getLiveVolunteers(req.query);
        res.json(volunteers);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/tracking/sessions — historical session list
adminRouter.get('/sessions', (0, validate_1.validate)(tracking_schemas_1.historicalListQuerySchema, 'query'), async (req, res, next) => {
    try {
        const result = await tracking_service_1.trackingService.getHistoricalSessions(req.query);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/map/tracking/sessions/:id/route — full route for a session
adminRouter.get('/sessions/:id/route', async (req, res, next) => {
    try {
        const id = req.params.id;
        const route = await tracking_service_1.trackingService.getSessionRoute(id);
        res.json(route);
    }
    catch (err) {
        next(err);
    }
});
//# sourceMappingURL=tracking.routes.js.map