<?php

namespace App\Entity\Campaign;

use App\Entity\BroadcastableInterface;
use App\Entity\Campaign;
use App\Entity\Traits\TimestampableTrait;
use App\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\Table(name="campaign_schedule")
 */
class Schedule implements BroadcastableInterface
{
    use TimestampableTrait;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="\App\Entity\Campaign", inversedBy="schedules")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     * @Assert\NotBlank()
     */
    private $campaign;

    /**
     * @ORM\Column(type="EnumCampaignScheduleType", nullable=false)
     */
    private $type;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @Assert\NotBlank()
     */
    private $options;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Assert\NotBlank()
     */
    private $executeAt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $executedAt;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $output;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $origin;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(onDelete="SET NULL")
     * @Gedmo\Blameable(on="create")
     */
    private $createdBy;

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

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

    public function setExecuteAt(?\DateTimeInterface $executeAt): self
    {
        $this->executeAt = $executeAt;

        return $this;
    }

    public function getOptions()
    {
        return json_decode($this->options, true);
    }

    public function setOptions(?string $options): self
    {
        $this->options = $options;

        return $this;
    }

    public function getOutput(): ?string
    {
        return $this->output;
    }

    public function setOutput(?string $output): self
    {
        $this->output = $output;

        return $this;
    }

    public function getCampaign()
    {
        if ($this->campaign) {
            return $this->campaign->getId();
        }

        return null;
    }

    public function setCampaign(?Campaign $campaign): self
    {
        $this->campaign = $campaign;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     *
     * @return Schedule
     */
    public function setType($type): self
    {
        $this->type = $type;

        return $this;
    }

    /**
     * @param mixed $executedAt
     *
     * @return Schedule
     */
    public function setExecutedAt($executedAt)
    {
        $this->executedAt = $executedAt;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getExecutedAt()
    {
        return $this->executedAt;
    }

    /**
     * @param mixed $origin
     *
     * @return Schedule
     */
    public function setOrigin($origin)
    {
        $this->origin = $origin;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getOrigin()
    {
        return $this->origin;
    }

    public function getCreatedBy(): ?User
    {
        return $this->createdBy;
    }

    public function getDataType(): string
    {
        return 'campaignschedule';
    }
}
