<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;
use Simplyin\Payment\Model\Auth\RsaRevoker;
use Simplyin\Payment\Model\Auth\RsaSigner;

/**
 * Module uninstall hook, run by `bin/magento module:uninstall --remove-data Simplyin_Payment`.
 *
 * Best-effort revokes the merchant's registered RSA public key on the backend (see
 * {@see RsaRevoker}) before wiping the local key material — otherwise a shop that removes the
 * plugin leaves an orphaned, still-active signing credential registered upstream forever. Local
 * cleanup ({@see RsaSigner::clear()}) always runs, regardless of whether the revoke call
 * succeeded, so uninstall never hangs on an unreachable backend.
 */
class Uninstall implements UninstallInterface
{
    /**
     * @var RsaSigner
     */
    private RsaSigner $signer;

    /**
     * @var RsaRevoker
     */
    private RsaRevoker $revoker;

    /**
     * @param RsaSigner $signer
     * @param RsaRevoker $revoker
     */
    public function __construct(RsaSigner $signer, RsaRevoker $revoker)
    {
        $this->signer = $signer;
        $this->revoker = $revoker;
    }

    /**
     * @inheritdoc
     */
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $this->revoker->revoke();
        $this->signer->clear();

        $setup->endSetup();
    }
}
