<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Entity\Trait\DeletableTrait;
use App\Entity\Trait\TimestampableTrait;
use App\Repository\EventSponsorRepository;
use Doctrine\ORM\Mapping\UniqueConstraint;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\HasLifecycleCallbacks]
#[UniqueEntity(fields: ["event", "sponsor"], message: "admin.event-performer.fields.@event+sponsor.errors.already-existing")]
#[ORM\Entity(repositoryClass: EventSponsorRepository::class)]
#[UniqueConstraint(name: "IDX_event_sponsor_event_id_sponsor_id", columns: ["event_id", "sponsor_id"])]
#[ORM\Table(name: 'event_sponsor')]
class EventSponsor
{
    use DeletableTrait;
    use TimestampableTrait;

    const t_root = 'admin.event-sponsor';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'sponsors', targetEntity: Event::class)]
    #[ORM\JoinColumn(name: 'event_id', nullable: false)]
    private ?Event $event = null;

    #[ORM\ManyToOne(inversedBy: 'events', targetEntity: Sponsor::class)]
    #[ORM\JoinColumn(name: 'sponsor_id', nullable: false)]
    private ?Sponsor $sponsor = null;

    #[ORM\Column(length: 255)]
    #[Assert\Url(protocols: ['https'], message: self::t_root . ".fields.@url.errors.url-invalid")]
    #[Assert\NotBlank(message: self::t_root . ".fields.@url.errors.not-null")]
    private ?string $url = null;

    #[ORM\Column(options: ['default' => false])]
    private ?bool $main = null;

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

    public function getEvent(): ?Event
    {
        return $this->event;
    }

    public function setEvent(?Event $event): static
    {
        $this->event = $event;

        return $this;
    }

    public function getSponsor(): ?Sponsor
    {
        return $this->sponsor;
    }

    public function setSponsor(?Sponsor $sponsor): static
    {
        $this->sponsor = $sponsor;

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(string $url): static
    {
        $this->url = $url;

        return $this;
    }
    
    public function __serialize() : array
    {
        return [
            "id"         => $this->id,
            "event"      => ["id" => ($this->event   ? $this->event->getId()   : -1)],
            "sponsor"    => ["id" => ($this->sponsor ? $this->sponsor->getId() : -1)],
            "url"        => $this->url,
            "main"       => $this->main,
        ];
    }

    public function __unserialize(array $data)
    {
        $this->id            = $data["id"];
        $this->event         = null; // ignore
        $this->sponsor       = null; // ignore
        $this->url           = $data["url"];
    }

    public function isMain(): ?bool
    {
        return $this->main;
    }

    public function setMain(bool $main): static
    {
        $this->main = $main;

        return $this;
    }

    #[ORM\PrePersist]
    public function prePersistMain(): void
    {
        if ($this->isMain() == null) {
            $this->setMain(false);
        }
    }


}
