<?php

namespace App\Entity;

use App\Repository\BilletOrderRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: BilletOrderRepository::class)]
class BilletOrder
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: BilletBuyer::class)]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ?BilletBuyer $billetBuyer = null;

    #[ORM\ManyToOne(targetEntity: Billet::class)]
    #[ORM\JoinColumn(nullable: false)]
    private ?Billet $billet = null;

    #[ORM\Column(length: 23, unique: true)]
    private string $reference;

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

    #[ORM\Column(length: 16)]
    private string $securityKey = '';

    #[ORM\Column]
    private int $unitPriceHT = 0;

    public const STATE_VALID = 'valid';
    public const STATE_INVALID = 'invalid';
    public const STATE_EXPIRED = 'expired';

    #[ORM\Column(length: 20)]
    private string $state = self::STATE_VALID;

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

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

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

    public function __construct()
    {
        $this->reference = BilletBuyer::generateReference();
        $this->createdAt = new \DateTimeImmutable();
    }

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

    public function getBilletBuyer(): ?BilletBuyer
    {
        return $this->billetBuyer;
    }

    public function setBilletBuyer(?BilletBuyer $billetBuyer): static
    {
        $this->billetBuyer = $billetBuyer;

        return $this;
    }

    public function getBillet(): ?Billet
    {
        return $this->billet;
    }

    public function setBillet(?Billet $billet): static
    {
        $this->billet = $billet;

        return $this;
    }

    public function getReference(): string
    {
        return $this->reference;
    }

    public function getBilletName(): ?string
    {
        return $this->billetName;
    }

    public function setBilletName(string $billetName): static
    {
        $this->billetName = $billetName;

        return $this;
    }

    public function getSecurityKey(): string
    {
        return $this->securityKey;
    }

    public function setSecurityKey(string $securityKey): static
    {
        $this->securityKey = $securityKey;

        return $this;
    }

    public static function generateSecurityKey(string $reference, string $appSecret): string
    {
        return strtoupper(substr(hash_hmac('sha256', $reference, $appSecret), 0, 16));
    }

    public function getUnitPriceHT(): int
    {
        return $this->unitPriceHT;
    }

    public function setUnitPriceHT(int $unitPriceHT): static
    {
        $this->unitPriceHT = $unitPriceHT;

        return $this;
    }

    public function getUnitPriceHTDecimal(): float
    {
        return $this->unitPriceHT / 100;
    }

    public function getState(): string
    {
        return $this->state;
    }

    public function setState(string $state): static
    {
        $this->state = $state;

        return $this;
    }

    public function isValid(): bool
    {
        return self::STATE_VALID === $this->state;
    }

    public function isInvitation(): ?bool
    {
        return $this->isInvitation;
    }

    public function setIsInvitation(?bool $isInvitation): static
    {
        $this->isInvitation = $isInvitation;

        return $this;
    }

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

    public function setFirstScannedAt(?\DateTimeImmutable $firstScannedAt): static
    {
        $this->firstScannedAt = $firstScannedAt;

        return $this;
    }

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