<?php

namespace domain\Ticket\Ticket;

use domain\Ticket\TicketMessage\TicketMessage;
use domain\Ticket\TicketUser\TicketUser;
use domain\User\User\User;
use domain\User\User\UserSolver;
use domain\_\AbstractSolver;

class TicketSolver extends AbstractSolver
{
    protected UserSolver $userSolver;

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

    public function hasTicketAccess(Ticket $ticket, User $user): string
    {
        return $user->id_producer == $ticket->id_producer
            || $this->userSolver->isAdmin($user);
    }

    public function isTicketOpen(Ticket $ticket)
    {
        return $ticket->status == Ticket::STATUS_OPEN;
    }

    public function isTicketClosed(Ticket $ticket)
    {
        return $ticket->status == Ticket::STATUS_CLOSED;
    }

    public function getTicketDateCreatedAtFormat(Ticket $ticket)
    {
        return date('d/m/Y', strtotime($ticket->created_at));
    }

    public function getTicketStatusLabelAsHtml(Ticket $ticket)
    {
        $classLabel = 'label-success';
        $statusLabel = 'Ouvert';
        if ($this->isTicketClosed($ticket)) {
            $classLabel = 'label-danger';
            $statusLabel = 'Fermé';
        }

        return '<span class="label ' . $classLabel . '">' . $statusLabel . '</span>';
    }

    public function getTicketUser(Ticket $ticket, User $user)
    {
        foreach($ticket->ticketUsers as $ticketUser) {
            if($ticketUser->id_user == $user->id) {
                return $ticketUser;
            }
        }

        return null;
    }

    public function isTicketMessageUnread(TicketMessage $ticketMessage, TicketUser $ticketUser)
    {
        if($ticketMessage->created_at > $ticketUser->read_at) {
            return true;
        }

        return false;
    }

    public function isTicketUnread(Ticket $ticket, User $user): bool
    {
        $ticketUser = $this->getTicketUser($ticket, $user);

        if($ticketUser) {
            foreach($ticket->ticketMessages as $ticketMessage) {
                if($this->isTicketMessageUnread($ticketMessage, $ticketUser)) {
                    return true;
                }
            }
            return false;
        }

        return true;
    }

    public function getFirstTicketMessageUnread(Ticket $ticket, User $user): ?TicketMessage
    {
        $ticketUser = $this->getTicketUser($ticket, $user);

        if($ticketUser) {
            foreach($ticket->ticketMessages as $ticketMessage) {
                if($this->isTicketMessageUnread($ticketMessage, $ticketUser)) {
                    return $ticketMessage;
                }
            }
        }

        if(isset($ticket->ticketMessages[0])) {
            return $ticket->ticketMessages[0];
        }

        return null;
    }

    public function countTicketsUnreadByUserFromArray(array $ticketsArray, User $user): int
    {
        $count = 0;
        foreach($ticketsArray as $ticket) {
            if($this->isTicketUnread($ticket, $user)) {
                $count++;
            }
        }

        return $count;
    }
}