<?php

namespace domain\PointSale\PointSale;

use domain\User\User\User;
use domain\_\AbstractRepositoryQuery;
use domain\_\StatusInterface;

class PointSaleRepositoryQuery extends AbstractRepositoryQuery
{
    protected PointSaleDefinition $definition;

    public function loadDependencies(): void
    {
        $this->loadDefinition(PointSaleDefinition::class);
    }

    public function filterByIdUser(int $idUser): self
    {
        $this->andWhere(['id_user' => $idUser]);

        return $this;
    }

    public function filterByName(string $name): self
    {
        $this->andWhere(['name' => $name]);

        return $this;
    }

    public function filterIsOnline(): self
    {
        $this->andWhere(['status' => StatusInterface::STATUS_ONLINE]);

        return $this;
    }

    public function filterByRestrictedAccess(int $restrictedAccess): self
    {
        $this->andWhere(['restricted_access' => $restrictedAccess]);

        return $this;
    }

    public function filterByUserAccess(User $user): self
    {
        $this->andWhere('status = 1 AND (restricted_access = 0 OR (restricted_access = 1 AND (SELECT COUNT(*) FROM user_point_sale WHERE point_sale.id = user_point_sale.id_point_sale AND user_point_sale.id_user = :id_user) > 0))');
        $this->addParams([':id_user' => $user->id]);

        return $this;
    }
}