<?php

namespace App\Entity;

use App\Repository\MessengerLogRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: MessengerLogRepository::class)]
#[ORM\Index(columns: ['status'], name: 'idx_messenger_log_status')]
#[ORM\Index(columns: ['created_at'], name: 'idx_messenger_log_created_at')]
class MessengerLog
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $messageClass = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $messageBody = null;

    #[ORM\Column(length: 20)]
    private ?string $status = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $errorMessage = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $stackTrace = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $transportName = null;

    #[ORM\Column]
    private ?int $retryCount = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $failedAt = null;

    public function __construct(
        string $messageClass,
        ?string $messageBody,
        string $errorMessage,
        ?string $stackTrace = null,
        ?string $transportName = null,
        int $retryCount = 0,
    ) {
        $this->messageClass = $messageClass;
        $this->messageBody = $messageBody;
        $this->status = 'failed';
        $this->errorMessage = $errorMessage;
        $this->stackTrace = $stackTrace;
        $this->transportName = $transportName;
        $this->retryCount = $retryCount;
        $this->createdAt = new \DateTimeImmutable();
        $this->failedAt = new \DateTimeImmutable();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getMessageClass(): ?string
    {
        return $this->messageClass;
    }

    public function getMessageBody(): ?string
    {
        return $this->messageBody;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(string $status): static
    {
        $this->status = $status;

        return $this;
    }

    public function getErrorMessage(): ?string
    {
        return $this->errorMessage;
    }

    public function getStackTrace(): ?string
    {
        return $this->stackTrace;
    }

    public function getTransportName(): ?string
    {
        return $this->transportName;
    }

    public function getRetryCount(): ?int
    {
        return $this->retryCount;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function getFailedAt(): ?\DateTimeImmutable
    {
        return $this->failedAt;
    }

    public function markAsResolved(): void
    {
        $this->status = 'resolved';
    }
}
