<?php
declare(strict_types=1);

namespace Autogedal\PayPo\Test\Unit\Plugin;

use Autogedal\PayPo\Model\ApprovalConfig;
use Autogedal\PayPo\Model\PayPoContext;
use Autogedal\PayPo\Plugin\OrderStatusHistoryPlugin;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\Order\Status\History;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class OrderStatusHistoryPluginTest extends TestCase
{
    public static function orders(): array
    {
        return [
            'eligible PayPo' => ['asteptare_plata_paypo', 'processing', 'paypo', 1, true, 'asteptare_plata_paypo,new', 'new'],
            'another order' => ['asteptare_plata_paypo', 'processing', 'paypo', 2, true, 'asteptare_plata_paypo,new', 'custom_awb'],
            'card' => ['new', 'processing', 'net_card', 1, true, 'new', 'custom_awb'],
            'already taken' => ['pending', 'processing', 'paypo', 1, true, 'asteptare_plata_paypo,new', 'custom_awb'],
            'empty configuration' => ['new', 'processing', 'paypo', 1, true, '', 'custom_awb'],
            'outside callback' => ['new', 'processing', 'paypo', 1, false, 'new', 'custom_awb'],
            'complete selected' => ['complete', 'complete', 'paypo', 1, true, 'complete', 'custom_awb'],
            'closed selected' => ['closed', 'closed', 'paypo', 1, true, 'closed', 'custom_awb'],
            'held selected' => ['holded', 'holded', 'paypo', 1, true, 'holded', 'custom_awb'],
        ];
    }

    #[DataProvider('orders')]
    public function testBothHistoryMethodsRespectEligibility(
        string $originalStatus,
        string $state,
        string $method,
        int $id,
        bool $approving,
        string $configured,
        string $expected
    ): void {
        $context = new PayPoContext();
        $context->setApprovingOrder($approving);
        $context->setApprovingOrderId(1);
        $scopeConfig = $this->createMock(ScopeConfigInterface::class);
        $scopeConfig->method('getValue')->willReturn($configured);
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()
            ->onlyMethods(['getId', 'getPayment', 'canUnhold'])->getMock();
        $order->method('getId')->willReturn($id);
        $order->method('canUnhold')->willReturn(false);
        $order->setData(['state' => $state, 'status' => 'custom_awb', 'store_id' => 1]);
        $order->setOrigData('status', $originalStatus);
        $payment = $this->createMock(Payment::class);
        $payment->method('getMethod')->willReturn($method);
        $order->method('getPayment')->willReturn($payment);
        $plugin = new OrderStatusHistoryPlugin($context, new ApprovalConfig($scopeConfig));

        foreach (['afterAddStatusHistoryComment', 'afterAddCommentToStatusHistory'] as $hook) {
            $history = $this->getMockBuilder(History::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
            $history->setStatus('custom_awb');
            self::assertSame($history, $plugin->$hook($order, $history));
            self::assertSame($expected, $history->getStatus());
        }
    }
}
