<?php

namespace common\logic\Ticket\Ticket\Service;

use common\logic\AbstractSolver;
use common\logic\Ticket\Ticket\Model\Ticket;
use common\logic\Ticket\TicketMessage\Model\TicketMessage;
use common\logic\Ticket\TicketUser\Model\TicketUser;
use common\logic\User\User\Model\User;
use common\logic\User\User\Service\UserSolver;

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

        return $ticket->ticketMessages[0];
    }

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

        return $count;
    }
}