<?php
declare(strict_types=1);

namespace Autogedal\PayPo\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Sales\Model\Order\Status;
use Magento\Sales\Model\Order\StatusFactory;

class AssignWaitingStatusToPendingState implements DataPatchInterface
{
    private const STATUS_CODE = 'asteptare_plata_paypo';

    public function __construct(
        private readonly ModuleDataSetupInterface $setup,
        private readonly StatusFactory $statusFactory
    ) {
    }

    public function apply(): self
    {
        $this->setup->startSetup();

        /** @var Status $status */
        $status = $this->statusFactory->create();
        $status->load(self::STATUS_CODE, 'status');

        if ($status->getData('status')) {
            // Assign to 'pending' state — the state PayPo orders actually land in
            $status->assignState('pending', false, true);
        }

        $this->setup->endSetup();

        return $this;
    }

    public static function getDependencies(): array
    {
        return [AddWaitingPayPoStatus::class];
    }

    public function getAliases(): array
    {
        return [];
    }
}
