<?php
/**
 * 2007-2024 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author    PrestaShop SA <contact@prestashop.com>
 *  @copyright 2007-2023 PrestaShop SA
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

namespace PrestaShop\Module\AgileMultisellerPro\Helper;

if (!defined('_PS_VERSION_')) {
    exit;
}

use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoMembershipType;
use PrestaShop\Module\AgileMultisellerPro\Form\Configuration\Commissions\DataConfiguration as CommissionsDataConfiguration;
use PrestaShop\Module\AgileMultisellerPro\Form\Configuration\Membership\DataConfiguration as MembershipDataConfiguration;
use PrestaShop\Module\AgileMultisellerPro\Sql\SqlInstaller;

class AgileUninstall
{
    private $module;
    private $entityManager;

    public function __construct($module)
    {
        $this->module = $module;
        $this->entityManager = AgileHelper::buildContainer()->get('doctrine.orm.entity_manager');
    }

    public function processUninstall()
    {
        return
            $this->deleteMembershipProducts()
            && $this->deleteMembershipCategory()
            && $this->deleteConfiguration()
            && $this->unregisterHook()
            && $this->uninstallSql()
        ;
    }

    private function deleteConfiguration()
    {
        \Configuration::deleteByName('AGILE_MS_LOCATION_BLOCK_LEVEL');
        \Configuration::deleteByName(CommissionsDataConfiguration::ASC_COMMISSION_CURRENCY);
        \Configuration::deleteByName(CommissionsDataConfiguration::ASC_COMMISSION_MODE);
        \Configuration::deleteByName(CommissionsDataConfiguration::ASC_SHIPPING_INCLUDE);
        \Configuration::deleteByName(CommissionsDataConfiguration::ASC_TAX_INCLUDE);
        \Configuration::deleteByName(CommissionsDataConfiguration::ASC_VOUCHER_COST);
        \Configuration::deleteByName(CommissionsDataConfiguration::ASC_RT_COMMISSION_AT);
        \Configuration::deleteByName(CommissionsDataConfiguration::ASC_CT_COMMISSION_AT);

        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_STATUS);
        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_CID);
        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_PAUSE_GRACEDAYS);
        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_FREE_ONETIME_ONLY);
        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_FREE_TRIAL_ID);
        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_FREE_TRIAL_TIMING);
        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_FREE_TRIAL_STATE);
        \Configuration::deleteByName(MembershipDataConfiguration::AGILE_MEMBERSHIP_REMINDER_DAYS);

        return true;
    }

    private function deleteMembershipCategory()
    {
        $idCategory = (int) \Configuration::get('AGILE_MEMBERSHIP_CID');
        $category = new \Category($idCategory);

        if (\Validate::isLoadedObject($category)) {
            return $category->delete();
        }

        return true;
    }

    private function deleteMembershipProducts()
    {
        $sellerinfoMembershipTypeRepository = $this->entityManager->getRepository(SellerinfoMembershipType::class);

        $membershipTypes = $sellerinfoMembershipTypeRepository->findAll();

        if ($membershipTypes) {
            foreach ($membershipTypes as $membershipType) {
                $idProduct = $membershipType->getIdProduct();
                $product = new \Product($idProduct);

                if (
                    \Validate::isLoadedObject($product)
                    && !$product->delete()
                ) {
                    return false;
                }
            }
        }

        return true;
    }

    private function unregisterHook()
    {
        return
            $this->module->unregisterHook('displayHeader')
            && $this->module->unregisterHook('displayHeader')
            && $this->module->unregisterHook('displayFooter')
            && $this->module->unregisterHook('actionFrontControllerSetMedia')
            && $this->module->unregisterHook('displayCustomerAccount')
            && $this->module->unregisterHook('displayBeforeCarrier')
            && $this->module->unregisterHook('moduleRoutes')
        ;
    }

    private function uninstallSql()
    {
        $sqlInstaller = new SqlInstaller();

        return $sqlInstaller->uninstall();
    }
}
