<?php

namespace MageArray\LowerPrice\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

/**
 * Class UpgradeData
 * @package MageArray\LowerPrice\Setup
 */
class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * UpgradeData constructor.
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
	 // @codingStandardsIgnoreStart
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
		// @codingStandardsIgnoreEnd
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.5') < 0) {

            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $groupName = 'Lower Price Options';
            $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
            $attributeSetId = $eavSetup->getDefaultAttributeSetId($entityTypeId);
            $eavSetup->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 1000);
            $attributeGroupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);

            // Add existing attribute to group
            $attributeId = $eavSetup->getAttributeId($entityTypeId, 'found_lower_price');
            $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, $attributeId, null);

            // Auto approved inquiries
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'auto_approved_inquiries',
                [
                    'group' => 'Lower Price Options',
                    /* Group name in which you want to display your custom attribute */
                    'type' => 'int',
                    /* Data type in which formate your value save in database*/
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Auto approved inquiries',
                    /* lablel of your attribute*/
                    'input' => 'select',
                    'class' => '',
                    'source' => \MageArray\LowerPrice\Model\Config\Source\Boolean::Class,
                    /* Source of your select type custom attribute options*/
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    /*Scope of your attribute */
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '0',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'sort_order' => 2,
                ]
            );

            // Price Type
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'threshold_price_type',
                [
                    'group' => 'Lower Price Options',
                    /* Group name in which you want to display your custom attribute */
                    'type' => 'varchar',
                    /* Data type in which formate your value save in database*/
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Price Type',
                    /* lablel of your attribute*/
                    'input' => 'select',
                    'class' => '',
                    'source' => \MageArray\LowerPrice\Model\Config\Source\PriceType::Class,
                    /* Source of your select type custom attribute options*/
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    /*Scope of your attribute */
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'sort_order' => 3,
                ]
            );

            // Threshold Limit Price
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'threshold_limit',
                [
                    'group' => 'Lower Price Options',
                    /* Group name in which you want to display your custom attribute */
                    'type' => 'varchar',
                    /* Data type in which formate your value save in database*/
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Threshold Limit Price',
                    /* lablel of your attribute*/
                    'input' => 'text',
                    'class' => '',
                    'source' => '',
                    /* Source of your select type custom attribute options*/
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    /*Scope of your attribute */
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'sort_order' => 4,
                ]
            );
        }

        $setup->endSetup();
    }
}
