<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2023 PrestaShop SA
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

use PrestaShop\Module\AgileMultisellerPro\Provider\SellerProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;

if (!defined('_PS_VERSION_')) {
    exit;
}

class AgileMultiSellerProProductsModuleFrontController extends ProductListingFrontController
{
    public function __construct()
    {
        parent::__construct();
        $this->id_seller = 6;
    }

    public function init()
    {
        parent::init();
    }

    public function initContent()
    {
        parent::initContent();

        $this->doProductSearch('catalog/listing/product-list');
    }

    // Implement getListingLabel method
    public function getListingLabel()
    {
        return $this->getTranslator()->trans('Seller products', [], 'Shop.Theme.Catalog');
    }

    protected function getProductSearchQuery()
    {
        $query = new ProductSearchQuery();
        $query
            ->setSortOrder(new SortOrder('product', Tools::getProductsOrder('by'), Tools::getProductsOrder('way')));

        return $query;
    }

    // Implement getDefaultProductSearchProvider method
    protected function getDefaultProductSearchProvider()
    {
        $id_selectedcategory = (int) Tools::getValue('id_selectedcategory');
        $extra_conditions = '';
        if ($id_selectedcategory > 0) {
            $extra_conditions = ' AND p.id_product IN (SELECT DISTINCT id_product 	FROM ' . _DB_PREFIX_ . "category_product WHERE id_category = $id_selectedcategory )";
        }

        return new SellerProductSearchProvider(
            $this->getTranslator(),
            $this->id_seller,
            $extra_conditions
        );
    }

    public function getBreadcrumbLinks()
    {
        $breadcrumb = parent::getBreadcrumbLinks();

        $breadcrumb['links'][] = [
            'title' => $this->trans('Sellers list', [], 'Shop.Theme.Catalog'),
            'url' => $this->context->link->getPageLink('best-sales', true),
        ];

        $breadcrumb['links'][] = [
            'title' => $this->trans('Current', [], 'Shop.Theme.Catalog'),
            'url' => $this->context->link->getPageLink('best-sales', true),
        ];

        return $breadcrumb;
    }
}
