<?php

namespace MintyPHP\Tests\Module\Security\Service;

use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Module\Security\Service\SecurityBcGateway;
use MintyPHP\Module\Security\Service\SecurityBcSettingsGateway;
use MintyPHP\Module\Security\Service\SecurityOAuthTokenService;
use PHPUnit\Framework\TestCase;

class SecurityBcGatewayTest extends TestCase
{
    private function makeGateway(bool $configured = false): SecurityBcGateway
    {
        $settings = $this->createMock(SecurityBcSettingsGateway::class);
        $settings->method('isConfigured')->willReturn($configured);
        $settings->method('getAuthMode')->willReturn(SecurityBcSettingsGateway::AUTH_MODE_BASIC);
        $oauth = $this->createMock(SecurityOAuthTokenService::class);
        $session = $this->createMock(SessionStoreInterface::class);
        $session->method('all')->willReturn([]);

        return new SecurityBcGateway($settings, $oauth, $session);
    }

    public function testSearchCustomersReturnsEmptyForBlankQuery(): void
    {
        $this->assertSame([], $this->makeGateway()->searchCustomers('  '));
    }

    public function testListDomainsForCustomerReturnsEmptyForBlankNumber(): void
    {
        $this->assertSame([], $this->makeGateway()->listDomainsForCustomer(''));
    }

    public function testListDomainsForCustomerReturnsEmptyWhenNotConfigured(): void
    {
        // request() short-circuits to null before any network call when unconfigured.
        $this->assertSame([], $this->makeGateway(false)->listDomainsForCustomer('D10001'));
    }

    public function testSearchCustomersRejectsInvalidCharacters(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->makeGateway(true)->searchCustomers("bad'); DROP TABLE--");
    }

    public function testTestConnectionReportsIncompleteConfiguration(): void
    {
        $result = $this->makeGateway(false)->testConnection();

        $this->assertFalse($result['ok']);
        $this->assertArrayHasKey('error', $result);
    }
}
