<?php

namespace Kai\Banner\Setup;

use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    protected $_eavSetupFactory;
    protected $_attributeRepositoryInterface;
    protected $_attributeSetupFactory;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepositoryInterface,
        \Magento\Catalog\Setup\CategorySetupFactory $attributeSetupFactory
    ) {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_attributeRepositoryInterface = $attributeRepositoryInterface;
        $this->_attributeSetupFactory = $attributeSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        try {
            $this->_attributeRepositoryInterface->get(Product::ENTITY, 'kai_banner');
        } catch (\Exception $ex) {

            $setup->startSetup();
            $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->addAttribute(
                Product::ENTITY,
                'kai_banner',
                [
                    'input' => 'select',
                    'type' => 'varchar',
                    'label' => 'Banner',
                    'required' => false,
                    'visible' => true,
                    'user_defined' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'used_in_product_listing' => true,
                    'apply_to' => '',
                    'source' => \Kai\Banner\Model\KaiBannerValues::class,
                    'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                    'group' => 'General',
                ]
            );
            $setup->endSetup();
        }
    }
}
