<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Mageplaza\ProductAttachments\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Validator\ValidateException;
use Mageplaza\Core\Helper\AbstractData as CoreHelper;
use Mageplaza\ProductAttachments\Helper\Data;

/**
 * Class InstallEavAttribute
 *
 * @package Mageplaza\ProductAttachments\Setup\Patch\Data
 */
class InstallEavAttribute implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $_eavSetupFactory;

    /**
     * @var CoreHelper
     */
    private $_helperData;

    /**
     * InstallEavAttribute constructor.
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param ModuleDataSetupInterface $eavSetupFactory
     * @param CoreHelper $helperData
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory,
        CoreHelper $helperData
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_helperData = $helperData;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $setup = $this->moduleDataSetup;
        $setup->startSetup();
        $configData = [];
        $value = $this->_helperData->serialize($configData);
        $data = [
            'scope' => 'default',
            'scope_id' => 0,
            'path' => Data::CONFIG_MODULE_PATH . '/general/manage_icon/icons',
            'value' => $value,
        ];
        $setup->getConnection()
            ->insertOnDuplicate($setup->getTable('core_config_data'), $data, ['value']);

        $this->_installEavAttribute($setup);
    }

    /**
     * @param $setup
     *
     * @throws LocalizedException
     * @throws ValidateException
     */
    private function _installEavAttribute($setup)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attachment location attribute to the product eav/attribute
         */
        $eavSetup->removeAttribute(Product::ENTITY, 'mp_attachments_location');
        $eavSetup->addAttribute(
            Product::ENTITY,
            'mp_attachments_location',
            [
                'group' => 'Product Details',
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Attachment Location',
                'input' => 'text',
                'class' => 'mp-attachment-location',
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => false,
                'user_defined' => true,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false
            ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getVersion()
    {
        return '2.0.1';
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}
