<?php
/**
 * Webkul Software.
 * @category   Webkul
 * @package    Webkul_MpPayPal
 * @author     Webkul Software Private Limited
 * @copyright  Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license    https://store.webkul.com/license.html
 */
namespace Webkul\MpPayPal\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class CreateAttributes implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory,
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->_customerSetupFactory = $customerSetupFactory;
        $this->_attributeSetFactory = $attributeSetFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $attributesArray = [];
        $attrCode = 'mppaypal_submerchant_id';
        $attributesArray[] = $attrCode;
        $attrLabel = 'Sub Merchant Id';
        $attrNote = 'PayPal sub merchant id for seller';
        /** @var EavSetup $eavSetup */
        
        $eavSetup = $this->_customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $attrCodeExist = $eavSetup->getAttributeId(Customer::ENTITY, $attrCode);
        $customerEntity = $eavSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->_attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        if ($attrCodeExist === false) {

            $eavSetup->addAttribute(
                Customer::ENTITY,
                $attrCode,
                [
                    'type'              => 'varchar',
                    'label'             => $attrLabel,
                    'input'             => 'text',
                    'frontend_class'    => '',
                    'system'            => false,
                    'global'            => true,
                    'visible'           => false,
                    'required'          => false,
                    'user_defined'      => true,
                    'default'           => '',
                    'note'              => $attrNote
                ]
            );
        }

        //attributte for sub merchant approval status
        $attrCode = 'mppaypal_merchant_status';
        $attributesArray[] = $attrCode;
        $attrLabel = 'PayPal sub-merchant approval status';
        $attrNote = 'Confirmation of merchant approval';

        $attrCodeExist = $eavSetup->getAttributeId(Customer::ENTITY, $attrCode);
        if ($attrCodeExist === false) {

            $eavSetup->addAttribute(
                Customer::ENTITY,
                $attrCode,
                [
                    'type'              => 'int',
                    'label'             => $attrLabel,
                    'input'             => 'select',
                    'source'            => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
                    'frontend_class'    => '',
                    'system'            => false,
                    'global'            => true,
                    'visible'           => false,
                    'required'          => false,
                    'user_defined'      => true,
                    'default'           => '',
                    'note'              => $attrNote
                ]
            );
        }
        //attributte for sub merchant tracking id
        $attrCode = 'mppaypal_merchant_tracking_id';
        $attributesArray[] = $attrCode;
        $attrLabel = 'PayPal sub-merchant tracking id';
        $attrNote = 'PayPal sub-merchant tracking id';
        $attrCodeExist = $eavSetup->getAttributeId(Customer::ENTITY, $attrCode);

        if ($attrCodeExist === false) {
            
            $eavSetup->addAttribute(
                Customer::ENTITY,
                $attrCode,
                [
                    'type'              => 'varchar',
                    'label'             => $attrLabel,
                    'input'             => 'text',
                    'frontend_class'    => '',
                    'system'            => false,
                    'global'            => true,
                    'visible'           => false,
                    'required'          => false,
                    'user_defined'      => true,
                    'default'           => '',
                    'note'              => $attrNote
                ]
            );
        }
        foreach ($attributesArray as $ar) {
            $attribute = $eavSetup->getEavConfig()
            ->getAttribute(
                Customer::ENTITY,
                $ar
            )
            ->addData(
                [
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => [
                        'adminhtml_customer'
                    ]
                ]
            );
            $attribute->save();
        }
    }

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

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