<?php

require_once 'controllers/TwilioCallController.php';
require_once 'helpers/auth_helper.php';
require_once 'helpers/audit_trail.php';
require_once 'helpers/response_helper.php';

$twilioCallController = new TwilioCallController();


// Call Process Routes
if ($method === 'POST' && $uri === '/calls-initiate') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $twilioCallController->initiateCall($data);
    logAndRespond($user_id, $user_type, '/calls-initiate', $response, 'POST', $data);
}

if ($method === 'POST' && $uri === '/calls-end') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $twilioCallController->endCall($data);
    logAndRespond($user_id, $user_type, '/calls-end', $response, 'POST', $data);
}

if ($method === 'GET' && $uri === '/calls-token') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $identity = 'user_' . $user_id; // use stable unique identity per user
    $response = $twilioCallController->callToken($identity);
    logAndRespond($user_id, $user_type, '/calls-token', $response, 'GET', $_GET);
}

if ($method === 'POST' && $uri === '/calls-test') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $response = $twilioCallController->testCall();
    echo $response;
    exit;
}

?>