<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Index(columns: ['advert_id'], name: 'idx_advert_event_advert')]
#[ORM\Index(columns: ['type'], name: 'idx_advert_event_type')]
class AdvertEvent
{
    public const TYPE_VIEW = 'view';
    public const TYPE_PAY = 'pay';
    public const TYPE_MAIL_OPEN = 'mail_open';
    public const TYPE_MAIL_SEND = 'mail_send';
    public const TYPE_REMINDER = 'reminder';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Advert::class)]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private Advert $advert;

    #[ORM\Column(length: 30)]
    private string $type;

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $details = null;

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

    #[ORM\Column]
    private \DateTimeImmutable $createdAt;

    public function __construct(Advert $advert, string $type, ?string $details = null, ?string $ip = null)
    {
        $this->advert = $advert;
        $this->type = $type;
        $this->details = $details;
        $this->ip = $ip;
        $this->createdAt = new \DateTimeImmutable();
    }

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

    public function getAdvert(): Advert
    {
        return $this->advert;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function getDetails(): ?string
    {
        return $this->details;
    }

    public function getIp(): ?string
    {
        return $this->ip;
    }

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

    public function getTypeLabel(): string
    {
        return match ($this->type) {
            self::TYPE_VIEW => 'Page consultee',
            self::TYPE_PAY => 'Paiement effectue',
            self::TYPE_MAIL_OPEN => 'Email ouvert',
            self::TYPE_MAIL_SEND => 'Email envoye',
            self::TYPE_REMINDER => 'Relance envoyee',
            default => $this->type,
        };
    }
}
