"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.representativesRouter = void 0;
const express_1 = require("express");
const representatives_service_1 = require("./representatives.service");
const representatives_schemas_1 = require("./representatives.schemas");
const postal_codes_schemas_1 = require("../postal-codes/postal-codes.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.representativesRouter = router;
// =============================================
// PUBLIC ROUTES (no auth required)
// =============================================
// GET /api/representatives/by-postal/:postalCode — cache-first lookup
router.get('/by-postal/:postalCode', (0, validate_1.validate)(postal_codes_schemas_1.postalCodeParamSchema, 'params'), (0, validate_1.validate)(postal_codes_schemas_1.postalCodeQuerySchema, 'query'), async (req, res, next) => {
    try {
        const code = req.params.postalCode;
        const refresh = req.query.refresh === 'true';
        const result = await representatives_service_1.representativesService.lookupByPostalCode(code, refresh);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/representatives/test-connection — Represent API health check
router.get('/test-connection', async (_req, res, next) => {
    try {
        const result = await representatives_service_1.representativesService.testApiConnection();
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// =============================================
// ADMIN ROUTES (auth + role required)
// =============================================
router.use(auth_middleware_1.authenticate);
router.use((0, rbac_middleware_1.requireRole)(...roles_1.INFLUENCE_ROLES));
// GET /api/representatives/cache-stats — cache statistics
router.get('/cache-stats', async (_req, res, next) => {
    try {
        const stats = await representatives_service_1.representativesService.getCacheStats();
        res.json(stats);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/representatives — list all cached reps (paginated)
router.get('/', (0, validate_1.validate)(representatives_schemas_1.listRepresentativesSchema, 'query'), async (req, res, next) => {
    try {
        const result = await representatives_service_1.representativesService.findAll(req.query);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/representatives/:id — single cached rep
router.get('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        const rep = await representatives_service_1.representativesService.findById(id);
        res.json(rep);
    }
    catch (err) {
        next(err);
    }
});
// DELETE /api/representatives/by-postal/:postalCode — clear cache for postal code
router.delete('/by-postal/:postalCode', (0, validate_1.validate)(postal_codes_schemas_1.postalCodeParamSchema, 'params'), async (req, res, next) => {
    try {
        const code = req.params.postalCode;
        const result = await representatives_service_1.representativesService.clearByPostalCode(code);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// DELETE /api/representatives/:id — delete single cached rep
router.delete('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        await representatives_service_1.representativesService.deleteById(id);
        res.status(204).send();
    }
    catch (err) {
        next(err);
    }
});
//# sourceMappingURL=representatives.routes.js.map