"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.incomingCallController = void 0;
const twilio_1 = __importDefault(require("twilio"));
const logger_1 = __importDefault(require("../../../logger"));
/**
 * Controller for handling incoming calls
 */
const incomingCallController = async (req, res) => {
    try {
        const twiml = new twilio_1.default.twiml.VoiceResponse();
        const host = req.headers.host;
        twiml.say({
            voice: 'Google.en-US-Chirp3-HD-Aoede',
        }, 'Please wait while we connect your call to the A. I. voice assistant, powered by Twilio and the Open A I Realtime API');
        twiml.pause({ length: 1 });
        twiml.say({
            voice: 'Google.en-US-Chirp3-HD-Aoede',
        }, 'O.K. you can start talking!');
        const protocol = req.secure || req.headers['x-forwarded-proto'] === 'https' ? 'wss' : 'ws';
        const streamUrl = `${protocol}://${host}/api/phone/media-stream`;
        const connect = twiml.connect();
        connect.stream({ url: streamUrl });
        res.type('text/xml');
        res.send(twiml.toString());
    }
    catch (error) {
        logger_1.default.error(`Error in incoming call webhook: ${error.message}`);
        const twiml = new twilio_1.default.twiml.VoiceResponse();
        twiml.say('An application error has occurred. Goodbye.');
        twiml.hangup();
        res.type('text/xml');
        res.send(twiml.toString());
    }
};
exports.incomingCallController = incomingCallController;
//# sourceMappingURL=incoming-call.js.map