<?php

namespace routes;

use classes\bird;
use traits\bird_route_helpers_t;
use traits\route_t;

class birdVoiceFlashCallsRoute
{
    use route_t, bird_route_helpers_t;

    public function run(): void
    {
        // Create a flash call
        $this->post('/bird/voice/flash-calls', function () {
            global $response;
            // Permission: create/place a flash call via Bird
            self::requirePermission('modules_bird_voice_flash_calls_create');
            $client = new bird();
            // Require workspace/channel per Bird API docs
            $ws = $this->normalizeOptionalString($this->fromRequest('workspaceId') ?? $this->fromQuery('workspaceId'));
            $ch = $this->normalizeOptionalString($this->fromRequest('channelId') ?? $this->fromQuery('channelId'));
            if ($ws === '') {
                $ws = $this->getConfiguredWorkspaceId($client);
            }
            if ($ch === '') {
                $ch = $this->getConfiguredChannelId($client);
            }
            if ($ws === '' || $ch === '') {
                $response->error('Missing required parameters: workspaceId, channelId', 400);
            }
            $base = '/workspaces/' . rawurlencode($ws) . '/channels/' . rawurlencode($ch) . '/flashcalls';
            $payload = $this->getParametersAsArray();
            unset($payload['workspaceId'], $payload['channelId']);
            $res = $client->sendPostRequest($base, $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;
            // Permission: list flash calls via Bird
            self::requirePermission('modules_bird_voice_flash_calls_list');
            $client = new bird();
            $ws = $this->normalizeOptionalString($this->fromRequest('workspaceId') ?? $this->fromQuery('workspaceId'));
            $ch = $this->normalizeOptionalString($this->fromRequest('channelId') ?? $this->fromQuery('channelId'));
            if ($ws === '') {
                $ws = $this->getConfiguredWorkspaceId($client);
            }
            if ($ch === '') {
                $ch = $this->getConfiguredChannelId($client);
            }
            if ($ws === '' || $ch === '') {
                $response->error('Missing required parameters: workspaceId, channelId', 400);
            }
            $base = '/workspaces/' . rawurlencode($ws) . '/channels/' . rawurlencode($ch) . '/flashcalls';
            $query = $this->getParametersAsArray();
            unset($query['workspaceId'], $query['channelId']);
            $res = $client->sendGetRequest($base, $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;
            // Permission: get a specific flash call via Bird
            self::requirePermission('modules_bird_voice_flash_calls_get');
            $client = new bird();
            $id = (string)$this->fromRoute('id');
            if ($id === null || $id === '') {
                $response->error('Missing id', 400);
            }
            $ws = $this->normalizeOptionalString($this->fromRequest('workspaceId') ?? $this->fromQuery('workspaceId'));
            $ch = $this->normalizeOptionalString($this->fromRequest('channelId') ?? $this->fromQuery('channelId'));
            if ($ws === '') {
                $ws = $this->getConfiguredWorkspaceId($client);
            }
            if ($ch === '') {
                $ch = $this->getConfiguredChannelId($client);
            }
            if ($ws === '' || $ch === '') {
                $response->error('Missing required parameters: workspaceId, channelId', 400);
            }
            $base = '/workspaces/' . rawurlencode($ws) . '/channels/' . rawurlencode($ch) . '/flashcalls';
            $res = $client->sendGetRequest($base . '/' . rawurlencode($id));
            $response->success($res ?? []);
        }, [
            'modules_bird_voice_flash_calls_get' => 'Get a flash call by ID via Bird',
        ]);

        // Complete/end a flash call by ID (POST to the resource)
        $this->post('/bird/voice/flash-calls/{id}', function () {
            global $response;
            // Permission: complete/end a flash call via Bird
            self::requirePermission('modules_bird_voice_flash_calls_end');
            $client = new bird();
            $id = (string)$this->fromRoute('id');
            if ($id === null || $id === '') {
                $response->error('Missing id', 400);
            }
            // Forward any body fields (e.g., result/status) transparently
            $ws = $this->normalizeOptionalString($this->fromRequest('workspaceId') ?? $this->fromQuery('workspaceId'));
            $ch = $this->normalizeOptionalString($this->fromRequest('channelId') ?? $this->fromQuery('channelId'));
            if ($ws === '') {
                $ws = $this->getConfiguredWorkspaceId($client);
            }
            if ($ch === '') {
                $ch = $this->getConfiguredChannelId($client);
            }
            if ($ws === '' || $ch === '') {
                $response->error('Missing required parameters: workspaceId, channelId', 400);
            }
            $base = '/workspaces/' . rawurlencode($ws) . '/channels/' . rawurlencode($ch) . '/flashcalls';
            $payload = $this->getParametersAsArray();
            unset($payload['workspaceId'], $payload['channelId']);
            $res = $client->sendPostRequest($base . '/' . rawurlencode($id), $payload);
            $response->success($res ?? ['status' => 'ok']);
        }, [
            'modules_bird_voice_flash_calls_end' => 'Complete/end a flash call by ID via Bird',
        ]);

        // Complete/end a flash call by using from/to numbers
        $this->post('/bird/voice/flash-calls/end', function () {
            global $response;
            // Permission: complete/end a flash call by numbers via Bird
            self::requirePermission('modules_bird_voice_flash_calls_end_by_numbers');
            $client = new bird();
            $ws = $this->normalizeOptionalString($this->fromRequest('workspaceId') ?? $this->fromQuery('workspaceId'));
            $ch = $this->normalizeOptionalString($this->fromRequest('channelId') ?? $this->fromQuery('channelId'));
            if ($ws === '') {
                $ws = $this->getConfiguredWorkspaceId($client);
            }
            if ($ch === '') {
                $ch = $this->getConfiguredChannelId($client);
            }
            if ($ws === '' || $ch === '') {
                $response->error('Missing required parameters: workspaceId, channelId', 400);
            }
            $base = '/workspaces/' . rawurlencode($ws) . '/channels/' . rawurlencode($ch) . '/flashcalls';
            $payload = $this->getParametersAsArray();
            unset($payload['workspaceId'], $payload['channelId']);
            // Basic validation hints: expect 'from' and 'to' but let Bird validate strictly
            $res = $client->sendPostRequest($base . '/end', $payload);
            $response->success($res ?? ['status' => 'ok']);
        }, [
            'modules_bird_voice_flash_calls_end_by_numbers' => 'Complete/end a flash call using from/to numbers via Bird',
        ]);
    }
}
