"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConfigRouter = createConfigRouter;
const express_1 = __importDefault(require("express"));
const zod_1 = require("zod");
const configRepository_js_1 = require("../../repositories/configRepository.js");
const getConfig_js_1 = require("../../use-cases/getConfig.js");
const getAllConfig_js_1 = require("../../use-cases/getAllConfig.js");
const setConfig_js_1 = require("../../use-cases/setConfig.js");
// Validation middleware
const validateBody = (schema) => {
    return (req, res, next) => {
        try {
            req.body = schema.parse(req.body);
            next();
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({ error: 'Validation error', details: error.errors });
                return;
            }
            next(error);
        }
    };
};
const validateParams = (schema) => {
    return (req, res, next) => {
        try {
            req.params = schema.parse(req.params);
            next();
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({ error: 'Validation error', details: error.errors });
                return;
            }
            next(error);
        }
    };
};
// Error handling middleware
const asyncHandler = (fn) => {
    return (req, res, next) => {
        Promise.resolve(fn(req, res, next)).catch(next);
    };
};
function createConfigRouter() {
    const router = express_1.default.Router();
    // Initialize repository and use cases
    const configRepo = new configRepository_js_1.ConfigRepository();
    const getConfig = new getConfig_js_1.GetConfig(configRepo);
    const getAllConfig = new getAllConfig_js_1.GetAllConfig(configRepo);
    const setConfig = new setConfig_js_1.SetConfig(configRepo);
    // GET /api/config
    router.get('/', asyncHandler(async (req, res) => {
        const config = await getAllConfig.execute();
        res.json(config);
    }));
    // GET /api/config/:key
    router.get('/:key', validateParams(zod_1.z.object({ key: zod_1.z.string() })), asyncHandler(async (req, res) => {
        const { key } = req.params;
        const value = await getConfig.execute(key);
        if (value === undefined) {
            res.status(404).json({ error: 'Configuration key not found' });
            return;
        }
        res.json({ value });
    }));
    // POST /api/config/:key
    router.post('/:key', validateParams(zod_1.z.object({ key: zod_1.z.string() })), validateBody(zod_1.z.object({ value: zod_1.z.any() })), asyncHandler(async (req, res) => {
        const { key } = req.params;
        const { value } = req.body;
        await setConfig.execute(key, value);
        res.json({ success: true });
    }));
    return router;
}
//# sourceMappingURL=configRoutes.js.map