"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createApp = createApp;
require("dotenv/config");
const cors_1 = __importDefault(require("cors"));
const express_1 = __importDefault(require("express"));
const config_1 = require("./config");
const accountingAgent_1 = require("./routes/accountingAgent");
const assistant_1 = require("./routes/assistant");
const eval_1 = require("./routes/eval");
const history_1 = require("./routes/history");
const normalize_1 = require("./routes/normalize");
const presets_1 = require("./routes/presets");
const testConnection_1 = require("./routes/testConnection");
const inMemoryRuntimeAdapter_1 = require("./runtime/inMemoryRuntimeAdapter");
const assistantService_1 = require("./services/assistantService");
const assistantSessionStore_1 = require("./services/assistantSessionStore");
const evalService_1 = require("./services/evalService");
const normalizerService_1 = require("./services/normalizerService");
const openaiResponsesClient_1 = require("./services/openaiResponsesClient");
const files_1 = require("./utils/files");
const http_1 = require("./utils/http");
const log_1 = require("./utils/log");
function createApp() {
    (0, files_1.ensureDir)(config_1.TRACES_DIR);
    (0, files_1.ensureDir)(config_1.PRESETS_DIR);
    (0, files_1.ensureDir)(config_1.EVAL_CASES_DIR);
    (0, files_1.ensureDir)(config_1.REPORTS_DIR);
    (0, files_1.ensureDir)(config_1.ASSISTANT_SESSIONS_DIR);
    const app = (0, express_1.default)();
    app.use((0, cors_1.default)());
    app.use(express_1.default.json({ type: ["application/json", "application/*+json"], limit: "2mb" }));
    const openaiClient = new openaiResponsesClient_1.OpenAIResponsesClient();
    const normalizerService = new normalizerService_1.NormalizerService(openaiClient);
    const evalService = new evalService_1.EvalService(normalizerService);
    const assistantSessionStore = new assistantSessionStore_1.AssistantSessionStore();
    const assistantService = new assistantService_1.AssistantService(normalizerService, assistantSessionStore);
    const runtimeAdapter = new inMemoryRuntimeAdapter_1.InMemoryRuntimeAdapter();
    const services = {
        normalizerService,
        evalService,
        assistantService,
        runtimeAdapter
    };
    app.get("/api/health", (_req, res) => {
        (0, http_1.ok)(res, {
            ok: true,
            service: "llm-normalizer-backend",
            status: "RUNNING",
            timezone: config_1.TIMEZONE,
            now: new Date().toISOString()
        });
    });
    app.use((0, testConnection_1.buildTestConnectionRouter)(openaiClient));
    app.use((0, normalize_1.buildNormalizeRouter)(services));
    app.use((0, eval_1.buildEvalRouter)(services));
    app.use((0, assistant_1.buildAssistantRouter)(services));
    app.use((0, history_1.buildHistoryRouter)());
    app.use((0, presets_1.buildPresetsRouter)());
    app.use((0, accountingAgent_1.buildAccountingAgentRouter)(services));
    app.use(http_1.errorMiddleware);
    return app;
}
if (require.main === module) {
    const app = createApp();
    app.listen(config_1.PORT, () => {
        (0, log_1.logJson)({
            timestamp: new Date().toISOString(),
            level: "info",
            service: "llm_normalizer_backend",
            message: `Backend started on http://localhost:${config_1.PORT}`
        });
    });
}
