<?php

declare(strict_types=1);

namespace KupShop\FeedGeneratorBundle\Feed;

use KupShop\FeedsBundle\FeedProductList;
use Query\QueryBuilder;

class ProductConfigurableFeed extends ConfigurableFeed
{
    public static $type = 'productConfigurable';
    public static $alias = 'Produkty';

    public function getProductList(int $feedID): FeedProductList
    {
        $productList = parent::getProductList($feedID);
        $productList->setVariationsAsResult(false);
        $productList->andSpec(function (QueryBuilder $qb) {
            $qb->addSelect('MIN(COALESCE(pv.price, p.price)) AS price');
            $qb->addSelect('SUM(COALESCE(pv.in_store, p.in_store, 0)) AS in_store');
            // $qb->groupBy('p.id'); // Don't group by here, it would break ProductList::getProductsCount()

            return '';
        });

        return $productList;
    }
}
