<?php

declare(strict_types=1);

namespace FiloBlu\Refilo\Setup\Patch\Data;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
 * customer_grid_flat's FULLTEXT index is capped by MySQL/InnoDB at 16 columns.
 * Attributes flagged is_searchable_in_grid on customer/customer_address pushed
 * it to 17, breaking setup:upgrade with "Too many key parts specified".
 *
 * care_of also needs is_filterable_in_grid disabled: it's a TEXT-backed
 * attribute (backend_type text) with frontend_input "text" rather than
 * "textarea", so Magento doesn't skip it when building the grid's filter
 * indexes and tries to create a plain INDEX on a TEXT column, which MySQL
 * rejects without an explicit key length ("used in key specification
 * without a key length").
 */
class DisableCustomerGridSearchableAttributes implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
    }

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

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

    /**
     * @return void
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavConfig = $customerSetup->getEavConfig();

        $attributesToDisable = [
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS => [
                'fax' => ['is_searchable_in_grid'],
                'care_of' => ['is_searchable_in_grid', 'is_filterable_in_grid'],
                'note' => ['is_searchable_in_grid']
            ],
            Customer::ENTITY => [
                'social_login' => ['is_searchable_in_grid'],
                'add_to_newsletter' => ['is_searchable_in_grid'],
                'importanza' => ['is_searchable_in_grid'],
                'importato' => ['is_searchable_in_grid'],
                'crm_date_of_update' => ['is_searchable_in_grid'],
                'crm_local_update' => ['is_searchable_in_grid']
            ],
        ];

        foreach ($attributesToDisable as $entityType => $attributes) {
            foreach ($attributes as $attributeCode => $fields) {
                if (!$eavConfig->getAttribute($entityType, $attributeCode)->getId()) {
                    continue;
                }

                foreach ($fields as $field) {
                    $customerSetup->updateAttribute($entityType, $attributeCode, $field, 0);
                }
            }
        }

        $this->moduleDataSetup->getConnection()->endSetup();
    }
}
