<?php

namespace Ecoyn\Setup;

use Ecoyn\Setup\Steps\Finish;
use QUI;
use QUI\ERP\Api\NumberRangeInterface;
use QUI\InstallationWizard\AbstractInstallationWizard;
use QUI\InstallationWizard\InstallationWizardStepInterface;
use QUI\Locale;

use function class_exists;

/**
 * Class SetupProvider
 */
class SetupFinish extends AbstractInstallationWizard
{
    /**
     * set it as main provider
     * @return int
     */
    public function getPriority(): int
    {
        return 2;
    }

    /**
     * @return string
     */
    public function getTitle(?Locale $Locale = null): string
    {
        if ($Locale === null) {
            $Locale = QUI::getLocale();
        }

        return $Locale->get('ecoyn/core', 'set.up.finish.title');
    }

    /**
     * @return string
     */
    public function getDescription(?Locale $Locale = null): string
    {
        if ($Locale === null) {
            $Locale = QUI::getLocale();
        }

        return $Locale->get('ecoyn/core', 'set.up.finish.description');
    }

    /**
     * @return string
     */
    public function getFinishButtonText($Locale = null): string
    {
        if (!$Locale instanceof Locale) {
            $Locale = QUI::getLocale();
        }

        return $Locale->get('ecoyn/core', 'set.up.finish.button');
    }

    /**
     * @return list<InstallationWizardStepInterface>
     */
    public function getSteps(): array
    {
        return [
            new Finish()
        ];
    }

    /**
     * @param array<string, mixed> $data
     * @return void
     */
    public function execute(array $data = []): void
    {
        // Bestimmte Seiten im Shop automatisch nicht indexieren
        $types = [
            'quiqqer/sitetypes:types/legalnotes',
            'quiqqer/sitetypes:types/generalTermsAndConditions',
            'quiqqer/sitetypes:types/privacypolicy',
            'quiqqer/verification:types/verifier',
            'quiqqer/frontend-users:types/profile',
            'quiqqer/frontend-users:types/registrationSignUp',
            'quiqqer/order:types/shoppingCart',
        ];

        $projects = QUI::getProjectManager()->getProjectList();

        foreach ($projects as $Project) {
            try {
                foreach ($types as $type) {
                    $sites = $Project->getSites([
                        'where' => [
                            'type' => $type
                        ]
                    ]);

                    if (!is_array($sites)) {
                        continue;
                    }

                    foreach ($sites as $Site) {
                        $Edit = $Site->getEdit();
                        $Edit->setAttribute('quiqqer.meta.site.robots', 'noindex');
                        $Edit->save();
                    }
                }
            } catch (QUI\Exception) {
            }
        }


        // Nummernkreise

        /** @var array<class-string<NumberRangeInterface>, int> $numberRanges */
        $numberRanges = [
            'QUI\ERP\Products\NumberRange' => 1010,
            'QUI\ERP\Order\NumberRanges\Order' => 140140,
            'QUI\ERP\Accounting\Invoice\NumberRanges\Invoice' => 240240,
            'QUI\ERP\Accounting\Invoice\NumberRanges\TemporaryInvoice' => 1000,
            'QUI\ERP\Customer\NumberRange' => 10110,
            'QUI\ERP\Purchasing\Processes\NumberRanges\Process' => 640640,
            'QUI\ERP\Accounting\Contracts\NumberRanges\Contract' => 808080,
            'QUI\ERP\SalesOrders\NumberRanges\SalesOrder' => 440440,
        ];

        foreach ($numberRanges as $rangeClass => $index) {
            if (!class_exists($rangeClass)) {
                continue;
            }

            $Range = new $rangeClass();
            $this->setNumberRange($Range, $index);
        }
    }

    /**
     * @param NumberRangeInterface $Range
     * @param int $index
     * @return void
     */
    protected function setNumberRange(NumberRangeInterface $Range, int $index): void
    {
        $currentRange = $Range->getRange();

        if ($currentRange === 1) {
            $Range->setRange($index);
        }
    }
}
