<?php

namespace domain\User\UserMessage;

use domain\_\AbstractRepository;
use domain\User\User\User;

class UserMessageRepository extends AbstractRepository
{
    protected UserMessageRepositoryQuery $query;

    public function loadDependencies(): void
    {
        $this->loadQuery(UserMessageRepositoryQuery::class);
    }

    public function getDefaultOptionsSearch(): array
    {
        return [
            self::WITH => [],
            self::JOIN_WITH => [],
            self::ORDER_BY => 'created_at DESC',
            self::ATTRIBUTE_ID_PRODUCER => ''
        ];
    }

    public function findOneUserMessageById(int $id): ?UserMessage
    {
        return $this->createQuery()
            ->filterById($id)
            ->findOne();
    }

    public function queryUserMessagesByUser(User $user)
    {
        return $this->createDefaultQuery()
            ->filterByUser($user);
    }

    public function findUserMessagesByUser(User $user): array
    {
        return $this->queryUserMessagesByUser($user)->find();
    }

    public function countUserMessagesByUser(User $user): int
    {
        return count($this->findUserMessagesByUser($user));
    }

    public function findUserMessagesUnreadByUser(User $user)
    {
        return $this->queryUserMessagesByUser($user)
            ->filterIsUnread()
            ->find();
    }

    public function countUserMessagesUnreadByUser(User $user): int
    {
        return count($this->findUserMessagesUnreadByUser($user));
    }
}