<?php

namespace domain\Ticket\Ticket;

use domain\_\AbstractNotifier;
use domain\Ticket\TicketMessage\TicketMessage;
use domain\Ticket\TicketMessage\TicketMessageRepository;
use domain\User\User\UserSolver;

class TicketNotifier extends AbstractNotifier
{
    protected UserSolver $userSolver;
    protected TicketMessageRepository $ticketMessageRepository;

    public function loadDependencies(): void
    {
        parent::loadDependencies();
        $this->userSolver = $this->loadService(UserSolver::class);
        $this->ticketMessageRepository = $this->loadService(TicketMessageRepository::class);
    }

    public function sendEmailAdminNewTicket(Ticket $ticket)
    {
        $this->mailer->sendAdmin(
            'Nouveau ticket de '.$ticket->producer->name.' : '.$ticket->subject,
            'newTicketAdmin',
            [
                'ticket' => $ticket,
            ]
        );
    }

    public function sendEmailAdminNewTicketMessage(TicketMessage $ticketMessage)
    {
        $countTicketMessages = $this->ticketMessageRepository->countTicketMessagesByTicketId($ticketMessage->ticket->id);

        if($this->userSolver->isStatusProducer($ticketMessage->user) && $countTicketMessages > 1) {
            $this->mailer->sendAdmin(
                'Réponse au ticket : ' . $ticketMessage->ticket->subject.' ('.$ticketMessage->ticket->producer->name.')',
                'newTicketMessageAdmin',
                [
                    'ticketMessage' => $ticketMessage
                ]
            );
        }
    }
}