<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\Types;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
use Misd\PhoneNumberBundle\Validator\Constraints as MisdAssert;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME, unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
    private ?Uuid $id = null;

    #[Assert\Email]
    #[ORM\Column(length: 180)]
    private ?string $email = null;

    /**
     * @var list<string> The user roles
     */
    #[ORM\Column]
    private array $roles = [];

    /**
     * @var string The hashed password
     */
    #[ORM\Column]
    private ?string $password = null;

    #[ORM\Column]
    private bool $isVerified = false;

    #[ORM\Column(length: 180)]
    private ?string $firstName = null;

    #[ORM\Column(length: 180)]
    private ?string $lastName = null;

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

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

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

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

    /**
     * @var Collection<int, Game>
     */
    #[ORM\OneToMany(targetEntity: Game::class, mappedBy: 'addBy')]
    private Collection $games;

    #[ORM\OneToOne(mappedBy: 'linkToUser')]
    private ?Gamemaster $linkToGamemaster = null;

    /**
     * @var Collection<int, Party>
     */
    #[ORM\OneToMany(targetEntity: Party::class, mappedBy: 'submitter')]
    private Collection $submittedParties;

    /**
     * @var Collection<int, PartyRequest>
     */
    #[ORM\OneToMany(targetEntity: PartyRequest::class, mappedBy: 'requester', orphanRemoval: true)]
    private Collection $partyRequests;

    /**
     * @var Collection<int, Announcement>
     */
    #[ORM\OneToMany(targetEntity: Announcement::class, mappedBy: 'editedBy')]
    private Collection $announcements;

    public function __construct()
    {
        $this->lastUpdate = new \DateTime('now');
        $this->games = new ArrayCollection();
        $this->submittedParties = new ArrayCollection();
        $this->partyRequests = new ArrayCollection();
        $this->announcements = new ArrayCollection();
    }

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

    public function getIdentifier(): string
    {
        return (string) $this->id->toString();
    }

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

    public function setEmail(string $email): static
    {
        $this->email = $email;
        $this->lastUpdate = new \DateTime('now');

        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);
    }

    /**
     * @param list<string> $roles
     */
    public function setRoles(array $roles): static
    {
        $this->roles = $roles;
        $this->lastUpdate = new \DateTime('now');

        return $this;
    }

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

    public function setPassword(string $password): static
    {
        $this->password = $password;
        $this->lastUpdate = new \DateTime('now');

        return $this;
    }

    /**
     * Getter / Setter pour prénom
     */
    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(string $firstName): static
    {
        $this->firstName = $firstName;
        $this->lastUpdate = new \DateTime('now');

        return $this;
    }

    /**
     * Getter / Setter pour nom
     */
    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(string $lastName): static
    {
        $this->lastName = $lastName;
        $this->lastUpdate = new \DateTime('now');

        return $this;
    }

    public function getFullName(): ?string
    {
        $fullName = $this->firstName . " " . $this->lastName; 
        return $fullName;
    }

    /**
     * Getter / Setter pour numéro de téléphone
     */
    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(?string $phone): static
    {
        $this->phone = $phone;
        $this->lastUpdate = new \DateTime('now');

        return $this;
    }

    /**
     * Getter / Setter lastUpdate
     */
    public function getLastUpdate(): ?\DateTimeInterface
    {
        return $this->lastUpdate;
    }

    public function setLastUpdate(?\DateTimeInterface $lastUpdate = null): static
    {
        // Si la date est vide, on mets à jour à la date actuelle
        if (!$lastUpdate) {
            $lastUpdate = new \DateTime('now');
        }
        $this->lastUpdate = $lastUpdate;

        return $this;
    }
    /**
     * Getter / Setter pour le dernier login
     */
    public function getLastLogin(): ?\DateTimeInterface
    {
        return $this->lastLogin;
    }

    public function setLastLogin(?\DateTimeInterface $lastLogin = null): static
    {
        // Si la date est vide, on mets à jour à la date actuelle
        if (!$lastLogin) {
            $lastLogin = new \DateTime('now');
        }
        $this->lastLogin = $lastLogin;

        return $this;
    }
    /**
     * Getter / Setter pour la dernière action
     */
    public function getLastAction(): ?\DateTimeInterface
    {
        return $this->lastAction;
    }

    public function setLastAction(?\DateTimeInterface $lastAction = null): static
    {
        // Si la date est vide, on mets à jour à la date actuelle
        if (!$lastAction) {
            $lastAction = new \DateTime('now');
        }
        $this->lastAction = $lastAction;

        return $this;
    }

    /**
     * Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
     */
    public function __serialize(): array
    {
        $data = (array) $this;
        $data["\0".self::class."\0password"] = hash('crc32c', $this->password);

        return $data;
    }

    #[\Deprecated]
    public function eraseCredentials(): void
    {
        // @deprecated, to be removed when upgrading to Symfony 8
    }

    public function isVerified(): bool
    {
        return $this->isVerified;
    }

    public function setIsVerified(bool $isVerified): static
    {
        $this->isVerified = $isVerified;

        return $this;
    }

    /**
     * @return Collection<int, Game>
     */
    public function getGames(): Collection
    {
        return $this->games;
    }

    public function addGame(Game $game): static
    {
        if (!$this->games->contains($game)) {
            $this->games->add($game);
            $game->setAddBy($this);
        }

        return $this;
    }

    public function removeGame(Game $game): static
    {
        if ($this->games->removeElement($game)) {
            // set the owning side to null (unless already changed)
            if ($game->getAddBy() === $this) {
                $game->setAddBy(null);
            }
        }

        return $this;
    }

    public function getLinkToGamemaster(): ?Gamemaster
    {
        return $this->linkToGamemaster;
    }

    public function setLinkToGamemaster(?Gamemaster $linkToGamemaster): static
    {
        // unset the owning side of the relation if necessary
        if ($linkToGamemaster === null && $this->linkToGamemaster !== null) {
            $this->linkToGamemaster->setLinkToUser(null);
        }

        // set the owning side of the relation if necessary
        if ($linkToGamemaster !== null && $linkToGamemaster->getLinkToUser() !== $this) {
            $linkToGamemaster->setLinkToUser($this);
        }

        $this->linkToGamemaster = $linkToGamemaster;

        return $this;
    }

    /**
     * @return Collection<int, Party>
     */
    public function getSubmittedParties(): Collection
    {
        return $this->submittedParties;
    }

    public function addSubmittedParty(Party $submittedParty): static
    {
        if (!$this->submittedParties->contains($submittedParty)) {
            $this->submittedParties->add($submittedParty);
            $submittedParty->setSubmitter($this);
        }

        return $this;
    }

    public function removeSubmittedParty(Party $submittedParty): static
    {
        if ($this->submittedParties->removeElement($submittedParty)) {
            // set the owning side to null (unless already changed)
            if ($submittedParty->getSubmitter() === $this) {
                $submittedParty->setSubmitter(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, PartyRequest>
     */
    public function getPartyRequests(): Collection
    {
        return $this->partyRequests;
    }

    public function addPartyRequest(PartyRequest $partyRequest): static
    {
        if (!$this->partyRequests->contains($partyRequest)) {
            $this->partyRequests->add($partyRequest);
            $partyRequest->setRequester($this);
        }

        return $this;
    }

    public function removePartyRequest(PartyRequest $partyRequest): static
    {
        if ($this->partyRequests->removeElement($partyRequest)) {
            // set the owning side to null (unless already changed)
            if ($partyRequest->getRequester() === $this) {
                $partyRequest->setRequester(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Announcement>
     */
    public function getAnnouncements(): Collection
    {
        return $this->announcements;
    }

    public function addAnnouncement(Announcement $announcement): static
    {
        if (!$this->announcements->contains($announcement)) {
            $this->announcements->add($announcement);
            $announcement->setEditedBy($this);
        }

        return $this;
    }

    public function removeAnnouncement(Announcement $announcement): static
    {
        if ($this->announcements->removeElement($announcement)) {
            // set the owning side to null (unless already changed)
            if ($announcement->getEditedBy() === $this) {
                $announcement->setEditedBy(null);
            }
        }

        return $this;
    }
}
