"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const auth_middleware_1 = require("../middleware/auth-middleware");
const codex_dashboard_service_1 = require("../services/codex-dashboard-service");
const codex_auth_dashboard_service_1 = require("../../codex-auth/codex-auth-dashboard-service");
const router = (0, express_1.Router)();
const CODEX_CONFIG_ACCESS_ERROR = 'Codex configuration endpoints require localhost access when dashboard auth is disabled.';
router.use('/config', (req, res, next) => {
    if ((0, auth_middleware_1.requireLocalAccessWhenAuthDisabled)(req, res, CODEX_CONFIG_ACCESS_ERROR)) {
        next();
    }
});
router.get('/diagnostics', async (_req, res) => {
    try {
        res.json(await (0, codex_dashboard_service_1.getCodexDashboardDiagnostics)());
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
// H6: email is PII — require localhost access when dashboard auth is disabled.
const CODEX_PROFILES_ACCESS_ERROR = 'Codex auth profiles endpoint requires localhost access when dashboard auth is disabled.';
router.get('/profiles', async (req, res) => {
    if (!(0, auth_middleware_1.requireLocalAccessWhenAuthDisabled)(req, res, CODEX_PROFILES_ACCESS_ERROR)) {
        return;
    }
    try {
        res.json(await (0, codex_auth_dashboard_service_1.getCodexAuthProfilesSummary)());
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.get('/config/raw', async (_req, res) => {
    try {
        res.json(await (0, codex_dashboard_service_1.getCodexRawConfig)());
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.put('/config/raw', async (req, res) => {
    try {
        const { rawText, expectedMtime } = req.body ?? {};
        if (typeof rawText !== 'string') {
            res.status(400).json({ error: 'rawText must be a string.' });
            return;
        }
        if (expectedMtime !== undefined &&
            (typeof expectedMtime !== 'number' || !Number.isFinite(expectedMtime))) {
            res.status(400).json({ error: 'expectedMtime must be a finite number when provided.' });
            return;
        }
        res.json(await (0, codex_dashboard_service_1.saveCodexRawConfig)({ rawText, expectedMtime }));
    }
    catch (error) {
        if (error instanceof codex_dashboard_service_1.CodexRawConfigValidationError) {
            res.status(400).json({ error: error.message });
            return;
        }
        if (error instanceof codex_dashboard_service_1.CodexRawConfigConflictError) {
            res.status(409).json({ error: error.message, mtime: error.mtime });
            return;
        }
        res.status(500).json({ error: error.message });
    }
});
router.patch('/config/patch', async (req, res) => {
    try {
        const body = req.body ?? {};
        if (typeof body.kind !== 'string' || body.kind.trim().length === 0) {
            res.status(400).json({ error: 'kind is required.' });
            return;
        }
        if (body.expectedMtime !== undefined &&
            (typeof body.expectedMtime !== 'number' || !Number.isFinite(body.expectedMtime))) {
            res.status(400).json({ error: 'expectedMtime must be a finite number when provided.' });
            return;
        }
        res.json(await (0, codex_dashboard_service_1.patchCodexConfig)(body));
    }
    catch (error) {
        if (error instanceof codex_dashboard_service_1.CodexRawConfigValidationError) {
            res.status(400).json({ error: error.message });
            return;
        }
        if (error instanceof codex_dashboard_service_1.CodexRawConfigConflictError) {
            res.status(409).json({ error: error.message, mtime: error.mtime });
            return;
        }
        res.status(500).json({ error: error.message });
    }
});
exports.default = router;
//# sourceMappingURL=codex-routes.js.map