<?php
namespace Tests\Unit;

use Api\Core\HttpClient;
use ElixForms\Auth\ElixFormsApiClient;
use ElixForms\Auth\ElixFormsRequestStatus;
use PHPUnit\Framework\TestCase;

final class ElixFormsApiClientTest extends TestCase
{
    public function testLookupByStatusOmitsStatusWhenItIsNull(): void
    {
        $httpClient = new class extends HttpClient {
            public string $url = '';
            public array $headers = [];

            public function get(string $url, array $headers = [])
            {
                $this->url = $url;
                $this->headers = $headers;

                return [
                    'status' => 200,
                    'json' => [
                        'value' => [
                            'globalStatus' => 'OK',
                            'requests' => [['requestId' => 123]],
                        ],
                    ],
                ];
            }
        };
        $client = new ElixFormsApiClient('https://example.test', $httpClient);

        $requests = $client->lookupByStatus('MODULO TEST', 'token', 'user');

        self::assertSame([['requestId' => 123]], $requests);
        self::assertStringNotContainsString('requestStatus=', $httpClient->url);
        self::assertStringContainsString('moduleTag=MODULO%20TEST', $httpClient->url);
        self::assertSame('Bearer token', $httpClient->headers['Authorization']);
        self::assertSame('user', $httpClient->headers['x-api-username']);
    }

    public function testLookupByStatusExpandsCombinedFlags(): void
    {
        $httpClient = new class extends HttpClient {
            public string $url = '';

            public function get(string $url, array $headers = [])
            {
                $this->url = $url;

                return [
                    'status' => 200,
                    'json' => [
                        'value' => [
                            'globalStatus' => 'OK',
                            'requests' => [],
                        ],
                    ],
                ];
            }
        };
        $client = new ElixFormsApiClient('https://example.test', $httpClient);

        $client->lookupByStatus(
            'MODULO',
            'token',
            'user',
            ElixFormsRequestStatus::IN_PROGRESS | ElixFormsRequestStatus::PROCESSED
        );

        self::assertStringContainsString('requestStatus=IN_PROGRESS', $httpClient->url);
        self::assertStringNotContainsString('requestStatus=SUBMITTED', $httpClient->url);
        self::assertStringContainsString('requestStatus=PROCESSED', $httpClient->url);
    }

    public function testRequestStatusRejectsUnknownFlags(): void
    {
        $this->expectException(\InvalidArgumentException::class);

        ElixFormsRequestStatus::toApiValues(8);
    }

    public function testGetExportTagsSupportsVendorJsonContentTypeResponses(): void
    {
        $httpClient = new class extends HttpClient {
            public string $url = '';

            public function get(string $url, array $headers = [])
            {
                $this->url = $url;

                return [
                    'status' => 200,
                    'body' => json_encode([
                        'value' => [
                            'globalStatus' => 'OK',
                            'exportTags' => [
                                ['name' => 'contratto.id', 'value' => 'ABC-123'],
                            ],
                        ],
                    ]),
                    'json' => null,
                ];
            }
        };
        $client = new ElixFormsApiClient('https://example.test/', $httpClient);

        $tags = $client->getExportTags(42, 'MODULO', 'token', 'user');

        self::assertSame(
            [['name' => 'contratto.id', 'value' => 'ABC-123']],
            $tags
        );
        self::assertSame(
            'https://example.test/eF/services/api/request/42/view/_DEFAULT/exportTags/get/v1?moduleTag=MODULO&exportGroup=API',
            $httpClient->url
        );
    }

    public function testGetExportTagsUsesTheProvidedExportGroup(): void
    {
        $httpClient = new class extends HttpClient {
            public string $url = '';

            public function get(string $url, array $headers = [])
            {
                $this->url = $url;

                return [
                    'status' => 200,
                    'json' => [
                        'value' => [
                            'globalStatus' => 'OK',
                            'exportTags' => [],
                        ],
                    ],
                ];
            }
        };
        $client = new ElixFormsApiClient('https://example.test', $httpClient);

        $client->getExportTags(42, 'MODULO', 'token', 'user', 'REPORT ORE');

        self::assertStringContainsString('exportGroup=REPORT%20ORE', $httpClient->url);
    }

    public function testGetExportTagsRejectsAnEmptyExportGroup(): void
    {
        $this->expectException(\InvalidArgumentException::class);

        $httpClient = new HttpClient();
        $client = new ElixFormsApiClient('https://example.test', $httpClient);
        $client->getExportTags(42, 'MODULO', 'token', 'user', '   ');
    }
}
