<?php

namespace KupShop\SystemInspectionBundle\Inspections\Compile;

use Composer\InstalledVersions;
use KupShop\SystemInspectionBundle\Inspections\Inspection;
use KupShop\SystemInspectionBundle\InspectionWriters\MessageTypes\ComposerMissingMessage;
use KupShop\SystemInspectionBundle\InspectionWriters\MessageTypes\InspectionMessage;

class ComposerInspection extends Inspection implements CompileInspectionInterface
{
    /**
     * @return InspectionMessage[]|null
     */
    public function runInspection(): ?array
    {
        $errors = [];

        foreach ($this->getRequiredComposerPackages() as $package) {
            try {
                InstalledVersions::getVersion($package);
            } catch (\Exception $e) {
                $errors[] = new ComposerMissingMessage(
                    sprintf('Missing required composer dependency "%s"! Run composer update to fix it.', $package)
                );
            }
        }

        return $errors;
    }

    protected function getRequiredComposerPackages(): array
    {
        $packages = [];

        if (findModule(\Modules::RETURNS)) {
            $packages[] = 'globalcitizen/php-iban';
        }

        if (findModule(\Modules::PAYMENTS, \Modules::SUB_GOPAY)) {
            $packages[] = 'gopay/payments-sdk-php';
            $packages[] = 'league/iso3166';
        }

        if (findModule(\Modules::PAYMENTS, \Modules::SUB_WSPAY)) {
            $packages[] = 'dodo-it/omnipay-wspay';
            $packages[] = 'league/omnipay';
        }

        if (findModule(\Modules::PAYMENTS, \Modules::SUB_PAYPAL)) {
            $packages[] = 'paypal/rest-api-sdk-php';
        }

        if (findModule(\Modules::PAYMENTS, \Modules::SUB_CSOB)) {
            $packages[] = 'ondrakoupil/csob-eapi-paygate';
        }

        if (findModule(\Modules::SYNCHRONIZATION)) {
            $packages[] = 'symfony/scheduler';
        }

        if (findModule(\Modules::TWO_FACTOR)) {
            $packages[] = 'scheb/2fa-bundle';
            $packages[] = 'scheb/2fa-email';
        }

        if (findModule(\Modules::AGE_VERIFY)) {
            $packages[] = 'veriface/veriface';
        }

        if (findModule(\Modules::USER_OAUTH, \Modules::SUB_APPLE_LOGIN)) {
            $packages[] = 'firebase/php-jwt';
        }

        //        if (findModule(\Modules::KAFKA)) {
        //            $packages[] = 'ext-rdkafka';
        //        }

        return $packages;
    }
}
