"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.phoneRouter = void 0;
const express_1 = require("express");
const call_data_storage_1 = require("../../providers/phone/call-data-storage");
const audio_upload_1 = require("../controller/phone/audio-upload");
const download_recording_1 = require("../controller/phone/download-recording");
const incoming_call_1 = require("../controller/phone/incoming-call");
const initiate_advanced_outbound_call_1 = require("../controller/phone/initiate-advanced-outbound-call");
const outbound_call_answered_1 = require("../controller/phone/outbound-call-answered");
const simple_outbound_answered_1 = require("../controller/phone/simple-outbound-answered");
const simple_outbound_call_1 = require("../controller/phone/simple-outbound-call");
const logger_1 = __importDefault(require("../../logger"));
exports.phoneRouter = (0, express_1.Router)();
// Test endpoint to verify router is working
exports.phoneRouter.get('/test', (req, res) => {
    res.json({ message: 'Phone router is working', timestamp: new Date().toISOString() });
});
// Debug endpoint to check stored call data
exports.phoneRouter.get('/debug/calls', async (_req, res) => {
    try {
        const callSids = await (0, call_data_storage_1.getAllCallSids)();
        const calls = [];
        for (const callSid of callSids) {
            const data = await (0, call_data_storage_1.getCallData)(callSid);
            calls.push({ callSid, data });
        }
        res.json({ totalCalls: calls.length, calls });
    }
    catch (err) {
        logger_1.default.error(`Error in /debug/calls: ${err}`);
        res.status(500).json({ error: 'Failed to retrieve call data' });
    }
});
// Audio upload endpoints
exports.phoneRouter.post('/upload-audio', audio_upload_1.uploadAudioMiddleware, audio_upload_1.uploadAudioController);
exports.phoneRouter.get('/uploads/:filename', audio_upload_1.serveUploadedFileController);
// Public webhook endpoints (no auth required - Twilio webhooks)
// Twilio sends POST for initial webhook, but GET for redirects - use .all() to handle both
exports.phoneRouter.all('/incoming-call', incoming_call_1.incomingCallController);
exports.phoneRouter.all('/outbound-call-answered', simple_outbound_answered_1.simpleOutboundAnsweredController);
exports.phoneRouter.all('/outbound-call-advanced-answered', outbound_call_answered_1.outboundCallAnsweredController);
exports.phoneRouter.all('/outbound-call-advanced-step-complete', outbound_call_answered_1.outboundCallStepCompleteController);
exports.phoneRouter.all('/outbound-call-advanced-realtime', outbound_call_answered_1.outboundCallRealtimeController);
// Protected API endpoints
exports.phoneRouter.post('/outbound-call', simple_outbound_call_1.simpleOutboundCallController);
exports.phoneRouter.post('/outbound-call-advanced', initiate_advanced_outbound_call_1.initiateAdvancedOutboundCallController);
exports.phoneRouter.post('/recording/download', download_recording_1.downloadRecordingController);
// Catch-all route to debug 404s (must be last)
exports.phoneRouter.all('*', (req, res, next) => {
    next(); // Let Express handle 404
});
//# sourceMappingURL=phone.js.map