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

namespace WriteTextAI\WriteTextAI\Ui\DataProvider\Products\Listing;

use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;

class Collection extends SearchResult
{
    /**
     * Override _initSelect to add custom columns
     *
     * @return void
     */
    protected function _initSelect()
    {
        $this->addFilterToMap('entity_id', 'main_table.entity_id');
        $this->addFilterToMap('sku', 'main_table.sku');
        $this->addFilterToMap('type_id', 'main_table.type_id');
        $this->addFilterToMap('category_ids', 'categories.category_ids');
        $this->addFilterToMap('category_names', 'categories.category_names');
        $this->addFilterToMap(
            'visibility',
            new \Zend_Db_Expr('COALESCE(visibility.value, default_visibility.value)')
        );
        $this->addFilterToMap(
            'status',
            new \Zend_Db_Expr('COALESCE(status.value, default_status.value)')
        );
        $this->addFilterToMap(
            'product_name',
            new \Zend_Db_Expr('COALESCE(product_name.value, default_product_name.value)')
        );
        $this->addFilterToMap(
            'mg_page_title',
            new \Zend_Db_Expr('COALESCE(mg_page_title.value, default_mg_page_title.value)')
        );
        $this->addFilterToMap(
            'mg_page_description',
            new \Zend_Db_Expr('COALESCE(mg_page_description.value, default_mg_page_description.value)')
        );
        $this->addFilterToMap(
            'mg_product_description',
            new \Zend_Db_Expr('COALESCE(mg_product_description.value, default_mg_product_description.value)')
        );
        $this->addFilterToMap(
            'mg_short_product_description',
            new \Zend_Db_Expr('COALESCE(
                mg_short_product_description.value,
                default_mg_short_product_description.value
            )')
        );
        $this->addFilterToMap(
            'mg_open_graph',
            new \Zend_Db_Expr('COALESCE(mg_open_graph.value, default_mg_open_graph.value)')
        );
        $this->addFilterToMap('generated_at', 'wtai_products.generated_at');
        $this->addFilterToMap('edited_at', 'wtai_products.edited_at');
        $this->addFilterToMap('transferred_at', 'wtai_products.transferred_at');
        $this->addFilterToMap('store_id', 'store.store_id');
        parent::_initSelect();
    }
}
