<?php

/*
 * This file is part of the Serenity package.
 *
 * (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\Cart\Model;

use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Cart\Repository\InvoiceRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

#[ORM\Entity(repositoryClass: InvoiceRepository::class)]
#[ORM\Table(name: 'cart_invoice')]
#[ORM\HasLifecycleCallbacks]
class Invoice implements HasTimestamp
{
    use Timestamp;

    #[ORM\Id]
    #[ORM\Column(type: 'uuid')]
    #[ORM\GeneratedValue(strategy: 'NONE')]
    private UuidInterface $id;

    #[ORM\Column(type: Types::INTEGER, nullable: true)]
    private ?int $numberDebit = null;

    #[ORM\Column(type: Types::INTEGER, nullable: true)]
    private ?int $numberCredit = null;

    #[ORM\Column(type: Types::INTEGER, nullable: true)]
    private ?int $year;

    #[ORM\Column(type: Types::BOOLEAN)]
    private bool $credit = false;

    #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
    private ?\DateTime $creditAt = null;

    #[ORM\OneToOne(mappedBy: 'invoice', targetEntity: Cart::class)]
    private ?Cart $cart;

    #[ORM\Column(type: Types::STRING)]
    private string $type;

    public function __construct(string $type)
    {
        $this->id = Uuid::uuid4();
        $this->year = $this->createYear();
        $this->type = $type;
    }

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

    public function getNumberDebit(): ?int
    {
        return $this->numberDebit;
    }

    public function setNumberDebit(?int $numberDebit): void
    {
        $this->numberDebit = $numberDebit;
    }

    public function getNumberCredit(): ?int
    {
        return $this->numberCredit;
    }

    public function setNumberCredit(?int $numberCredit): void
    {
        $this->numberCredit = $numberCredit;
    }

    public function getYear(): ?int
    {
        return $this->year;
    }

    public function isCredit(): bool
    {
        return $this->credit;
    }

    public function setCredit(bool $credit): void
    {
        $this->credit = $credit;
    }

    public function getCreditAt(): ?\DateTime
    {
        return $this->creditAt;
    }

    public function setCreditAt(?\DateTime $creditAt): void
    {
        $this->creditAt = $creditAt;
    }

    private function createYear(): int
    {
        $date = new \DateTime();

        return (int) $date->format('Y');
    }

    public function getCart(): ?Cart
    {
        return $this->cart;
    }

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

    public function setType(string $type): void
    {
        $this->type = $type;
    }
}
