<?php


namespace AutoAdmin\Bundle\Content\Entity\Traits;

use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

trait PublishTimePeriodTrait
{
    #[ORM\Column(name: 'publish_start_date', type: Types::DATETIME_MUTABLE, nullable: true)]
    #[Groups(['publishable'])]
    protected ?DateTimeInterface $publishStartDate = null;

    #[ORM\Column(name: 'publish_end_date', type: Types::DATETIME_MUTABLE, nullable: true)]
    #[Groups(['publishable'])]
    protected ?DateTimeInterface $publishEndDate = null;

    public function getPublishStartDate(): ?DateTimeInterface
    {
        return $this->publishStartDate;
    }

    public function setPublishStartDate(?DateTimeInterface $startDate = null): void
    {
        $this->publishStartDate = $startDate;
    }

    public function getPublishEndDate(): ?DateTimeInterface
    {
        return $this->publishEndDate;
    }

    public function setPublishEndDate(?DateTimeInterface $endDate = null): void
    {
        $this->publishEndDate = $endDate;
    }

    public function isInPublishTimePeriod(): bool
    {
        $now = new DateTime();
        $startDate = $this->getPublishStartDate();
        $endDate = $this->getPublishEndDate();

        if ($startDate !== null && $startDate > $now) {
            return false;
        }

        if ($endDate !== null && $endDate < $now) {
            return false;
        }

        return true;
    }
}
