<?php
declare(strict_types=1);

namespace Autogedal\PayPo\Test\Unit\Model;

use Autogedal\PayPo\Model\ApprovalConfig;
use Autogedal\PayPo\Model\Config\Source\ApprovalStatuses;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sales\Model\Order\Config;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class ApprovalConfigTest extends TestCase
{
    public static function statuses(): array
    {
        return [
            [' custom_waiting, new, ', 'custom_waiting', true],
            ['custom_waiting,new', 'pending', false],
            ['', 'new', false],
            [null, 'new', false],
            ['new', null, false],
            ['new', '', false],
        ];
    }

    #[DataProvider('statuses')]
    public function testStoreScopedEligibility(?string $configured, ?string $status, bool $expected): void
    {
        $scopeConfig = $this->createMock(ScopeConfigInterface::class);
        $scopeConfig->expects(self::once())->method('getValue')
            ->with(ApprovalConfig::XML_PATH_STATUSES, ScopeInterface::SCOPE_STORE, 2)
            ->willReturn($configured);

        self::assertSame($expected, (new ApprovalConfig($scopeConfig))->isAllowed($status, 2));
    }

    public function testSourceIncludesStatusesFromCustomStates(): void
    {
        $config = $this->createMock(Config::class);
        $config->expects(self::once())->method('getStatuses')->willReturn([
            'new' => 'NOUA', 'asteptare_plata_paypo' => 'Asteptare plata PayPo',
        ]);
        $config->expects(self::never())->method('getStateStatuses');
        $options = (new ApprovalStatuses($config))->toOptionArray();

        self::assertSame(['', 'new', 'asteptare_plata_paypo'], array_column($options, 'value'));
    }
}
