<?php
/**
 * @author 1902 Software
 * @copyright Copyright © 2023 1902 Software (https://1902software.com/magento/)
 * @package WriteTextAI_WriteTextAI
 */

namespace WriteTextAI\WriteTextAI\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class ClearGridBookmarksDataPatch implements DataPatchInterface
{
    public const CATEGORY_GRID_LISTING = 'wtai_categories_grid_listing';
    public const PRODUCT_GRID_LISTING = 'wtai_products_grid_listing';

    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

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

    /**
     * Apply data patch
     *
     * @return void
     */
    public function apply()
    {
        $this->resetListingFilters();
    }

    /**
     * Reset listing filters
     *
     * @return void
     */
    private function resetListingFilters()
    {
        $connection = $this->moduleDataSetup->getConnection();

        $connection->delete(
            $this->moduleDataSetup->getTable('ui_bookmark'),
            ['namespace IN (?)' => [self::CATEGORY_GRID_LISTING, self::PRODUCT_GRID_LISTING]]
        );
    }

    /**
     * Get dependencies
     *
     * @return array
     */
    public static function getDependencies()
    {
        return [
            ApplyWriteTextAiDataPatch::class
        ];
    }

    /**
     * Get aliases
     *
     * @return array
     */
    public function getAliases()
    {
        return [];
    }
}
