<?php
/**
 * _             _       _
 * | |____      _| | __ _| |__
 * | '_ \ \ /\ / / |/ _` | '_ \
 * | |_) \ V  V /| | (_| | |_) |
 * |_.__/ \_/\_/ |_|\__,_|_.__/.
 *
 * @author    Bwlab di Luigi Massa <info@bwlab.it>
 * @copyright since 2014 Bwlab
 * @license   proprietary
 */

declare(strict_types=1);

namespace Bwlab\Withdrawal\Entity;

if (!defined('_PS_VERSION_')) {
    exit;
}

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Bwlab\Withdrawal\Repository\WithdrawalRequestRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class WithdrawalRequest
{
    public const STATUS_PENDING = 'pending';
    public const STATUS_PROCESSING = 'processing';
    public const STATUS_COMPLETED = 'completed';
    public const STATUS_REJECTED = 'rejected';

    public const STATUSES = [
        self::STATUS_PENDING,
        self::STATUS_PROCESSING,
        self::STATUS_COMPLETED,
        self::STATUS_REJECTED,
    ];

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(name="id_withdrawal_request", type="integer", nullable=false)
     */
    private ?int $id = null;

    /**
     * @ORM\Column(name="id_order", type="integer", nullable=false)
     */
    private int $idOrder;

    /**
     * @ORM\Column(name="id_customer", type="integer", nullable=false)
     */
    private int $idCustomer;

    /**
     * @ORM\Column(name="id_shop", type="integer", nullable=false)
     */
    private int $idShop;

    /**
     * @ORM\Column(name="customer_name", type="string", length=255, nullable=false)
     */
    private string $customerName;

    /**
     * @ORM\Column(name="customer_email", type="string", length=255, nullable=false)
     */
    private string $customerEmail;

    /**
     * @ORM\Column(name="order_reference", type="string", length=9, nullable=false)
     */
    private string $orderReference;

    /**
     * @ORM\Column(name="status", type="string", length=30, nullable=false, options={"default": "pending"})
     */
    private string $status = self::STATUS_PENDING;

    /**
     * @ORM\Column(name="notes", type="text", nullable=true)
     */
    private ?string $notes = null;

    /**
     * @ORM\Column(name="user_agent", type="string", length=512, nullable=false)
     */
    private string $userAgent;

    /**
     * @ORM\Column(name="withdrawal_deadline", type="datetime", nullable=false)
     */
    private \DateTimeInterface $withdrawalDeadline;

    /**
     * @ORM\Column(name="date_add", type="datetime", nullable=false)
     */
    private \DateTimeInterface $dateAdd;

    /**
     * @ORM\Column(name="date_upd", type="datetime", nullable=false)
     */
    private \DateTimeInterface $dateUpd;

    /**
     * @ORM\Column(name="id_employee", type="integer", nullable=true)
     */
    private ?int $idEmployee = null;

    /**
     * @ORM\PrePersist()
     */
    public function prePersist(): void
    {
        $now = new \DateTime();
        $this->dateAdd = $now;
        $this->dateUpd = $now;
    }

    /**
     * @ORM\PreUpdate()
     */
    public function preUpdate(): void
    {
        $this->dateUpd = new \DateTime();
    }

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

    public function getIdOrder(): int
    {
        return $this->idOrder;
    }

    public function setIdOrder(int $idOrder): self
    {
        $this->idOrder = $idOrder;

        return $this;
    }

    public function getIdCustomer(): int
    {
        return $this->idCustomer;
    }

    public function setIdCustomer(int $idCustomer): self
    {
        $this->idCustomer = $idCustomer;

        return $this;
    }

    public function getIdShop(): int
    {
        return $this->idShop;
    }

    public function setIdShop(int $idShop): self
    {
        $this->idShop = $idShop;

        return $this;
    }

    public function getCustomerName(): string
    {
        return $this->customerName;
    }

    public function setCustomerName(string $customerName): self
    {
        $this->customerName = $customerName;

        return $this;
    }

    public function getCustomerEmail(): string
    {
        return $this->customerEmail;
    }

    public function setCustomerEmail(string $customerEmail): self
    {
        $this->customerEmail = $customerEmail;

        return $this;
    }

    public function getOrderReference(): string
    {
        return $this->orderReference;
    }

    public function setOrderReference(string $orderReference): self
    {
        $this->orderReference = $orderReference;

        return $this;
    }

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

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

        return $this;
    }

    public function getNotes(): ?string
    {
        return $this->notes;
    }

    public function setNotes(?string $notes): self
    {
        $this->notes = $notes;

        return $this;
    }

    public function appendNote(string $note): self
    {
        $timestamp = (new \DateTime())->format('Y-m-d H:i:s');
        $this->notes = ($this->notes ? $this->notes . "\n" : '') . "[{$timestamp}] {$note}";

        return $this;
    }

    public function getUserAgent(): string
    {
        return $this->userAgent;
    }

    public function setUserAgent(string $userAgent): self
    {
        $this->userAgent = $userAgent;

        return $this;
    }

    public function getWithdrawalDeadline(): \DateTimeInterface
    {
        return $this->withdrawalDeadline;
    }

    public function setWithdrawalDeadline(\DateTimeInterface $withdrawalDeadline): self
    {
        $this->withdrawalDeadline = $withdrawalDeadline;

        return $this;
    }

    public function getDateAdd(): \DateTimeInterface
    {
        return $this->dateAdd;
    }

    public function getDateUpd(): \DateTimeInterface
    {
        return $this->dateUpd;
    }

    public function getIdEmployee(): ?int
    {
        return $this->idEmployee;
    }

    public function setIdEmployee(?int $idEmployee): self
    {
        $this->idEmployee = $idEmployee;

        return $this;
    }

    public function isWithinDeadline(): bool
    {
        return new \DateTime() <= $this->withdrawalDeadline;
    }
}
