<?php

namespace domain\Ticket\Ticket;

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

class TicketRepository extends AbstractRepository
{
    protected TicketRepositoryQuery $query;
    protected TicketSolver $ticketSolver;

    public function loadDependencies(): void
    {
        $this->loadQuery(TicketRepositoryQuery::class);
        $this->ticketSolver = $this->loadService(TicketSolver::class);
    }
    
    public function getDefaultOptionsSearch(): array
    {
        return [
            self::WITH => ['user', 'producer', 'ticketMessages', 'ticketUsers'],
            self::JOIN_WITH => [],
            self::ORDER_BY => '',
            self::ATTRIBUTE_ID_PRODUCER => 'ticket.id_producer'
        ];
    }

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

    public function countTicketsUnreadByUser(User $user): int
    {
        $ticketsArray = $this->createDefaultQuery()->find();
        return $this->ticketSolver->countTicketsUnreadByUserFromArray($ticketsArray, $user);
    }

    public function countTicketsAdminUnreadByUser(User $user): int
    {
        $ticketsArray = $this->createQuery()->find();
        return $this->ticketSolver->countTicketsUnreadByUserFromArray($ticketsArray, $user);
    }

    public function findTicketsAdminStatusOpen()
    {
        return $this->createQuery()
            ->filterByStatusOpen()
            ->find();
    }

    public function countTicketsAdminStatusOpen()
    {
        return count($this->findTicketsAdminStatusOpen());
    }
}