<?php

declare(strict_types=1);

namespace KupShop\CatalogElasticBundle\Elastica;

use KupShop\CatalogElasticBundle\Util\ElasticQueryClient;

class Query extends \Elastica\Query
{
    use QueryRuntimeFields;

    public static function aggregationsOnly($query = null): self
    {
        $me = new self($query);
        $me->setSize(0);
        $me->setTrackTotalHits(false);
        $me->setTrackScores(false);
        $me->setSource(false);

        return $me;
    }

    public static function forIds($query = null): self
    {
        $me = new Query($query);
        $me->setSource(false);
        $me->setFields(['id']);
        $me->setSize(ElasticQueryClient::$maxResults);

        return $me;
    }

    public function setFields(array $fields): self
    {
        $this->setParam('fields', $fields);

        return $this;
    }

    public function getFields(): ?array
    {
        if (!$this->hasParam('fields')) {
            return null;
        }

        return $this->getParam('fields');
    }

    public function getAggregations(): ?array
    {
        if (!$this->hasParam('aggs')) {
            return null;
        }

        return $this->getParam('aggs');
    }
}
