<?php

namespace DMD\LaLigaApi\Entity;

use DMD\LaLigaApi\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\HasLifecycleCallbacks]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 180, unique: true)]
    private ?string $email = null;

    #[ORM\Column]
    private array $roles = [];

    #[ORM\Column]
    private ?string $password = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $firstName = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $lastName = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $phone = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $profilePicture = null;

    #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $birthday = null;

    #[ORM\Column(nullable: true)]
    private ?bool $active = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\Column(type: Types::ARRAY, nullable: true)]
    private ?array $noteList = null;

    #[ORM\OneToOne(mappedBy: 'captain', cascade: ['persist', 'remove'])]
    private ?Team $team = null;

    #[ORM\OneToMany(mappedBy: 'userWhoFiredEvent', targetEntity: Notification::class)]
    private Collection $sentNotifications;

    #[ORM\OneToMany(mappedBy: 'user', targetEntity: CustomRole::class, cascade: ['persist', 'remove'])]
    private Collection $customRoles;

    #[ORM\OneToMany(mappedBy: 'userToNotify', targetEntity: Notification::class, orphanRemoval: true)]
    private Collection $receivedNotifications;

    #[ORM\Column(nullable: true)]
    private ?bool $privacyPolicy = null;


    public function __construct()
    {
        $this->sentNotifications = new ArrayCollection();
        $this->customRoles = new ArrayCollection();
        $this->receivedNotifications = new ArrayCollection();
    }

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

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): static
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUserIdentifier(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): static
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see PasswordAuthenticatedUserInterface
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): static
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials(): void
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(?string $firstName): static
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(?string $lastName): static
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(?string $phone): static
    {
        $this->phone = $phone;

        return $this;
    }

    public function getProfilePicture(): ?string
    {
        return $this->profilePicture;
    }

    public function setProfilePicture(?string $profilePicture): static
    {
        $this->profilePicture = $profilePicture;

        return $this;
    }

    public function getBirthday(): ?\DateTimeInterface
    {
        return $this->birthday;
    }

    public function setBirthday(?\DateTimeInterface $birthday): static
    {
        $this->birthday = $birthday;

        return $this;
    }

    public function isActive(): ?bool
    {
        return $this->active;
    }

    public function setActive(?bool $active): static
    {
        $this->active = $active;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    #[ORM\PrePersist]
    public function setCreatedAt(): void
    {
        $timezone = new \DateTimeZone('Europe/Madrid');
        $this->createdAt = new \DateTimeImmutable('now', $timezone);
    }

    public function getNoteList(): ?array
    {
        return $this->noteList;
    }

    public function setNoteList(?array $noteList): static
    {
        $this->noteList = $noteList;

        return $this;
    }

    public function getTeam(): ?Team
    {
        return $this->team;
    }

    public function setTeam(?Team $team): static
    {
        // unset the owning side of the relation if necessary
        if ($team === null && $this->team !== null) {
            $this->team->setCaptain(null);
        }

        // set the owning side of the relation if necessary
        if ($team !== null && $team->getCaptain() !== $this) {
            $team->setCaptain($this);
        }

        $this->team = $team;

        return $this;
    }

    /**
     * @return Collection<int, Notification>
     */
    public function getSentNotifications(): Collection
    {
        return $this->sentNotifications;
    }

    public function addSentNotifications(Notification $sentNotifications): static
    {
        if (!$this->sentNotifications->contains($sentNotifications)) {
            $this->sentNotifications->add($sentNotifications);
            $sentNotifications->setUserWhoFiredEvent($this);
        }

        return $this;
    }

    public function removeSentNotifications(Notification $sentNotifications): static
    {
        if ($this->sentNotifications->removeElement($sentNotifications)) {
            // set the owning side to null (unless already changed)
            if ($sentNotifications->getUserWhoFiredEvent() === $this) {
                $sentNotifications->setUserWhoFiredEvent(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, CustomRole>
     */
    public function getCustomRoles(): Collection
    {
        return $this->customRoles;
    }

    public function addCustomRole(CustomRole $customRole): static
    {
        if (!$this->customRoles->contains($customRole)) {
            $this->customRoles->add($customRole);
            $customRole->setUser($this);
        }

        return $this;
    }

    public function removeCustomRole(CustomRole $customRole): static
    {
        if ($this->customRoles->removeElement($customRole)) {
            // set the owning side to null (unless already changed)
            if ($customRole->getUser() === $this) {
                $customRole->setUser(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, Notification>
     */
    public function getReceivedNotifications(): Collection
    {
        return $this->receivedNotifications;
    }

    public function addReceivedNotification(Notification $receivedNotification): static
    {
        if (!$this->receivedNotifications->contains($receivedNotification)) {
            $this->receivedNotifications->add($receivedNotification);
            $receivedNotification->setUserToNotify($this);
        }

        return $this;
    }

    public function removeReceivedNotification(Notification $receivedNotification): static
    {
        if ($this->receivedNotifications->removeElement($receivedNotification)) {
            // set the owning side to null (unless already changed)
            if ($receivedNotification->getUserToNotify() === $this) {
                $receivedNotification->setUserToNotify(null);
            }
        }

        return $this;
    }

    public function isPrivacyPolicy(): ?bool
    {
        return $this->privacyPolicy;
    }

    public function setPrivacyPolicy(?bool $privacyPolicy): static
    {
        $this->privacyPolicy = $privacyPolicy;

        return $this;
    }

}
