<?php

namespace common\logic\Ticket\Ticket\Repository;

use common\logic\AbstractRepository;
use common\logic\Ticket\Ticket\Model\Ticket;
use common\logic\Ticket\Ticket\Service\TicketSolver;
use common\logic\User\User\Model\User;

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);
    }
}