<?php

namespace Lc\SovBundle\Repository\Reminder;

use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;

class ReminderStore extends AbstractStore implements ReminderStoreInterface
{
    protected ReminderRepositoryQueryInterface $query;

    public function __construct(ReminderRepositoryQueryInterface $query)
    {
        $this->query = $query;
    }

    public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
    {
        $query->orderBy('id');
        return $query;
    }

    public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
    {
        return $query;
    }

    public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
    {
        return $query;
    }

    public function get($params = [], $query = null)
    {
        $query = $this->createDefaultQuery($query);

        $query->filterByDone();

        if (array_key_exists('user', $params)) {
            $query
                    ->filterByUser($params['user'])
                    ->groupBy('.id');
        }

        if (array_key_exists('crudAction', $params)) {
            $query->filterByCrudAction($params['crudAction']);
        }

        if (array_key_exists('crudControllerFqcn', $params)) {
            $query->filterByCrudControllerFqcn($params['crudControllerFqcn']);
        }

        if (array_key_exists('entityId', $params)) {
            $query->filterByEntityId($params['entityId']);
        }

        $query->orderBy('.dateReminder');

        return $query->find();
    }

//    findByUser
    public function getByUser(UserInterface $user, $query = null): array
    {
        $query = $this->createDefaultQuery($query);

        $query
                ->filterByUser($user)
                ->filterIsNotDone()
                ->orderBy('.dateReminder')
                ->groupBy('.id');

        return $query->find();
    }

//    findByEasyAdminConfigAndUser
    public function getByEasyAdminConfigAndUser(
            string $crudAction,
            string $crudControllerFqcn,
            UserInterface $user,
            int $entityId = null,
            $query = null
    ): array {
        $query = $this->createDefaultQuery($query);

        $query
                ->filterByUser($user)
                ->filterLikeCrudAction($crudAction)
                ->filterLikeCrudControllerFqcn($crudControllerFqcn)
                ->filterIsNotDone();

        if ($entityId) {
            $query
                    ->filterLikeEntityId($entityId);
        }
        $query
                ->orderBy('.dateReminder')
                ->groupBy('.id');

        return $query->find();
    }
}