<?php

declare(strict_types=1);

/*
 * This file is part of the VitiCore 2.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Product\Model;

use CpCreation\VitiCore\Cart\Model\CartItem;
use CpCreation\VitiCore\Product\Repository\ProductGiftRepository;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

#[ORM\Entity(repositoryClass: ProductGiftRepository::class)]
#[ORM\Table(name: 'product_gift')]
#[ORM\HasLifecycleCallbacks]
class ProductGift
{
    #[ORM\Id]
    #[ORM\Column(type: 'uuid')]
    #[ORM\GeneratedValue(strategy: 'NONE')]
    private UuidInterface $id;

    #[ORM\Column(type: 'string', length: 8)]
    private string $token;

    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTimeInterface $usedAt = null;

    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTimeInterface $validUntilDate = null;

    #[ORM\ManyToOne(targetEntity: CartItem::class, inversedBy: 'gifts')]
    private ?CartItem $cartItem = null;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->token = mb_strtoupper(bin2hex(random_bytes(4)));
    }

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

    public function getToken(): string
    {
        return $this->token;
    }

    public function setToken(string $token): void
    {
        $this->token = mb_strtoupper($token);
    }

    public function getUsedAt(): ?\DateTimeInterface
    {
        return $this->usedAt;
    }

    public function setUsedAt(?\DateTimeInterface $usedAt): void
    {
        $this->usedAt = $usedAt;
    }

    public function getCartItem(): ?CartItem
    {
        return $this->cartItem;
    }

    public function setCartItem(?CartItem $cartItem): void
    {
        $this->cartItem = $cartItem;
    }

    public function getValidUntilDate(): ?\DateTimeInterface
    {
        return $this->validUntilDate;
    }

    public function setValidUntilDate(?\DateTimeInterface $validUntilDate): void
    {
        $this->validUntilDate = $validUntilDate;
    }
}
