<?php
    require_once('call_flow_functions.php');
    header('Content-Type: application/xml; charset=UTF-8');
    echo '<?xml version="1.0" encoding="UTF-8"?>';

    $message_url = "/shared/serve_audios.php?path=";

    $call_id = isset($_REQUEST['call_id']) ? (int)$_REQUEST['call_id'] : 0;
    $call_data = fetch_table_data('call_histories', 'id', $call_id);
    $from_number = format_number($call_data['from_number']);
    $to_number = format_number($call_data['to_number']);
    $call_flow = get_call_flow($to_number);
    $base_url = htmlspecialchars(base_url(), ENT_QUOTES, 'UTF-8');

    $message = defalt_voice_message("Welcome-to-Our-Home.wav");
    $is_set_call_flow = 0;

    $property_id = htmlspecialchars($call_data['property_id'] ?? '', ENT_QUOTES, 'UTF-8');

    if ($call_flow['is_first_default_message'] == 0) {
        $check_welcome_message = $call_flow['is_first_textto_speech'];

        if ($check_welcome_message == 0) {
            $welcome_message = $message_url . rawurlencode($call_flow['first_audio_file']);
            $message = "<Play>$welcome_message</Play>";
        } else {
            $welcome_text = htmlspecialchars($call_flow['first_text'], ENT_QUOTES, 'UTF-8');
            $voice = htmlspecialchars($call_flow['first_message_vocie_select'], ENT_QUOTES, 'UTF-8');
            $message = "<Say voice='$voice'>$welcome_text</Say>";
        }
    }

    echo "<Response>
            <Gather input='dtmf' action='inbound_handle.php' method='POST' timeout='10' numDigits='1'>
                $message
            </Gather>
            <Redirect>no_response.php</Redirect>
          </Response>";    
    
?>
