"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const ws_1 = require("../ws"); // Import sessions, SessionState, broadcastToSession, and handleWebSocketMessage from ws/index.ts
const router = express_1.default.Router();
router.post('/sessions/:sessionId/responses', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { sessionId } = req.params;
    const { userId, wants, accepts, afraidToAsk } = req.body;
    if (!ws_1.sessions.has(sessionId)) {
        return res.status(404).json({ message: 'Session not found.' });
    }
    // Create a dummy WebSocket object for the handleWebSocketMessage function.
    // This is a workaround to reuse the WebSocket message handling logic.
    // In a real application, consider a more robust event-driven architecture.
    const dummyWs = {
        send: (message) => console.log('Dummy WS send:', message),
        readyState: 1, // OPEN
    };
    const message = {
        type: 'SUBMIT_RESPONSE',
        clientId: userId,
        payload: {
            response: { wants, accepts, afraidToAsk },
        },
    };
    try {
        yield (0, ws_1.handleWebSocketMessage)(dummyWs, sessionId, message);
        res.status(202).json({ message: 'Response submission acknowledged and processed.' });
    }
    catch (error) {
        console.error('Error processing response via HTTP route:', error);
        res.status(500).json({ message: 'Error processing response.', error: error.message });
    }
}));
router.get('/sessions/:sessionId/results', (req, res) => {
    const { sessionId } = req.params;
    if (!ws_1.sessions.has(sessionId)) {
        return res.status(404).json({ message: 'Session not found.' });
    }
    const sessionData = ws_1.sessions.get(sessionId);
    if (sessionData.state !== ws_1.SessionState.FINAL || !sessionData.finalResult) {
        return res.status(200).json({ message: 'Session results not yet finalized.', harmonizedIdeas: [] });
    }
    // Assuming finalResult directly contains the harmonized ideas as per openapi.yaml
    res.status(200).json({ sessionId, harmonizedIdeas: sessionData.finalResult });
});
router.post('/sessions/:sessionId/terminate', (req, res) => {
    const { sessionId } = req.params;
    if (!ws_1.sessions.has(sessionId)) {
        return res.status(404).json({ message: 'Session not found.' });
    }
    ws_1.sessions.delete(sessionId);
    // Log the purging event
    // logEvent('session_terminated_and_purged', sessionId);
    console.log(`Session ${sessionId} terminated and data purged.`);
    res.status(200).json({ message: 'Session terminated and data purged successfully.' });
});
exports.default = router;
