<?php

namespace Alsendo\AlsendoWrapper\Api\Points\Model;

use Alsendo\AlsendoWrapper\Model\Request;

class PointSearchRequest extends Request
{
    protected ?string $searchText = null;
    protected ?int $size = null;
    /** @var string[] Operator constants */
    protected array $operators = [];
    protected ?string $posType = null;
    protected ?bool $cod = null;
    /** @var string[] PointField constants */
    protected array $fields = [];
    /** @var string[] */
    protected ?array $pointTypes = null;
    protected ?float $lat = null;
    protected ?float $lon = null;
    protected ?float $distance = null;
    protected ?string $distanceUnit = null;

    public function toQueryParams(): array
    {
        $params = [];

        if ($this->searchText !== null && $this->searchText !== '') {
            $params['searchText'] = $this->searchText;
        }

        if ($this->size !== null) {
            $params['size'] = $this->size;
        }

        if (!empty($this->operators)) {
            $params['operators'] = implode(',', $this->operators);
        }

        if ($this->posType !== null) {
            $params['posType'] = $this->posType;
        }

        if ($this->cod !== null) {
            $params['cod'] = $this->cod ? 'true' : 'false';
        }

        if (!empty($this->fields)) {
            $params['fields'] = implode(',', $this->fields);
        }

        if ($this->pointTypes !== null && !empty($this->pointTypes)) {
            $params['pointTypes'] = implode(',', $this->pointTypes);
        }

        if ($this->lat !== null) {
            $params['lat'] = $this->lat;
        }

        if ($this->lon !== null) {
            $params['lon'] = $this->lon;
        }

        if ($this->distance !== null) {
            $params['distance'] = $this->distance;
        }

        if ($this->distanceUnit !== null) {
            $params['distanceUnit'] = $this->distanceUnit;
        }

        return $params;
    }

    public function toArray(array $exclude = [], bool $convertToSnakeCase = true): array
    {
        return $this->toQueryParams();
    }
}
