<?php

namespace routes;

require_once WD . '/classes/bird.php';
require_once WD . '/traits/route_t.php';
require_once WD . '/traits/bird_route_helpers_t.php';
require_once WD . '/traits/bird_route_validation_t.php';
require_once WD . '/modules/bird/helpers/bird_request_schemas.php';
require_once WD . '/modules/bird/helpers/bird_payloads.php';

use bird\helpers\bird_flash_create_payload;
use bird\helpers\bird_flash_end_payload;
use bird\helpers\bird_flash_hangup_payload;
use bird\helpers\bird_flash_list_query_payload;
use bird\helpers\bird_request_schemas;
use classes\bird;
use traits\bird_route_helpers_t;
use traits\bird_route_validation_t;
use traits\route_t;

class birdVoiceFlashCallsRoute
{
    use route_t, bird_route_helpers_t, bird_route_validation_t;

    public function run(): void
    {
        // Create a flash call
        $this->post('/bird/voice/flash-calls', function () {
            global $response;
            self::requirePermission('modules_bird_voice_flash_calls_create');

            $client = new bird();
            [$workspaceId, $channelId] = $this->birdResolveWorkspaceAndChannelIds($client);

            $payload = $this->birdPayloadWithout(['workspaceId', 'channelId']);
            $payload = $this->birdValidateSchema($payload, bird_request_schemas::flashCreateBody());
            $payload = bird_flash_create_payload::fromArray($payload)->toArray();

            $res = $client->createFlashCall($workspaceId, $channelId, $payload);
            $response->success($res ?? ['status' => 'ok']);
        }, [
            'modules_bird_voice_flash_calls_create' => 'Create/place a flash call via Bird',
        ]);

        // List flash calls
        $this->get('/bird/voice/flash-calls', function () {
            global $response;
            self::requirePermission('modules_bird_voice_flash_calls_list');

            $client = new bird();
            [$workspaceId, $channelId] = $this->birdResolveWorkspaceAndChannelIds($client);

            $query = $this->birdPayloadWithout(['workspaceId', 'channelId']);
            $query = $this->birdValidateSchema($query, bird_request_schemas::flashListQuery());
            $query = bird_flash_list_query_payload::fromArray($query)->toArray();

            $res = $client->listFlashCalls($workspaceId, $channelId, $query);
            $response->success($res ?? []);
        }, [
            'modules_bird_voice_flash_calls_list' => 'List flash calls via Bird',
        ]);

        // Get a specific flash call by ID
        $this->get('/bird/voice/flash-calls/{id}', function () {
            global $response;
            self::requirePermission('modules_bird_voice_flash_calls_get');

            $client = new bird();
            [$workspaceId, $channelId] = $this->birdResolveWorkspaceAndChannelIds($client);
            $callId = $this->birdResolveRouteCallId();

            $res = $client->getFlashCall($workspaceId, $channelId, $callId);
            $response->success($res ?? []);
        }, [
            'modules_bird_voice_flash_calls_get' => 'Get a flash call by ID via Bird',
        ]);

        // Complete/end a flash call by ID
        $this->post('/bird/voice/flash-calls/{id}', function () {
            global $response;
            self::requirePermission('modules_bird_voice_flash_calls_end');

            $client = new bird();
            [$workspaceId, $channelId] = $this->birdResolveWorkspaceAndChannelIds($client);
            $callId = $this->birdResolveRouteCallId();

            $payload = $this->birdPayloadWithout(['workspaceId', 'channelId']);
            $payload = $this->birdValidateSchema($payload, bird_request_schemas::flashEndBody());
            $payload = bird_flash_end_payload::fromArray($payload)->toArray();

            $res = $client->endFlashCall($workspaceId, $channelId, $callId, $payload);
            $response->success($res ?? ['status' => 'ok']);
        }, [
            'modules_bird_voice_flash_calls_end' => 'Complete/end a flash call by ID via Bird',
        ]);

        // Hang up flash calls by payload
        $this->post('/bird/voice/flash-calls/hangup', function () {
            global $response;
            self::requirePermission('modules_bird_voice_flash_calls_hangup');

            $client = new bird();
            [$workspaceId, $channelId] = $this->birdResolveWorkspaceAndChannelIds($client);

            $payload = $this->birdPayloadWithout(['workspaceId', 'channelId']);
            $payload = $this->birdValidateSchema($payload, bird_request_schemas::flashHangupBody());
            $payload = bird_flash_hangup_payload::fromArray($payload)->toArray();

            $res = $client->hangupFlashCall($workspaceId, $channelId, $payload);
            $response->success($res ?? ['status' => 'ok']);
        }, [
            'modules_bird_voice_flash_calls_hangup' => 'Hang up flash calls via Bird',
        ]);

        // Compatibility alias for flash hangup endpoint
        $this->post('/bird/voice/flash-calls/end', function () {
            global $response;
            self::requirePermission('modules_bird_voice_flash_calls_end_by_numbers');

            $client = new bird();
            [$workspaceId, $channelId] = $this->birdResolveWorkspaceAndChannelIds($client);

            $payload = $this->birdPayloadWithout(['workspaceId', 'channelId']);
            $payload = $this->birdValidateSchema($payload, bird_request_schemas::flashHangupBody());
            $payload = bird_flash_hangup_payload::fromArray($payload)->toArray();

            $res = $client->hangupFlashCall($workspaceId, $channelId, $payload);
            $response->success($res ?? ['status' => 'ok']);
        }, [
            'modules_bird_voice_flash_calls_end_by_numbers' => 'Compatibility alias for hanging up flash calls via Bird',
        ]);
    }
}
