<?php

namespace App\Http\Controllers\Twilio;

use App\Models\Communication\Call;
use Illuminate\Http\Request;
use Twilio\TwiML\VoiceResponse;

class OutboundTwimlController {
    public function __invoke($callId): string {

        /** @var Call $call */
        $call = Call::query()
            ->with([
                'communication',
                'phoneNumber',
            ])
            ->findOrFail($callId);

        abort_unless(
            $call->provider === 'twilio',
            404
        );

        $voice = new VoiceResponse();

        $dial = $voice->dial('', [
            'callerId' => $call->from_number,
            'record' => $call->is_recorded ? 'record-from-answer-dual' : 'do-not-record',
        ]);

        $dial->number($call->to_number);

        return (string) $voice;
    }
}