<?php

namespace App\Entity;

use AllowDynamicProperties;
use App\Repository\AccountRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

#[AllowDynamicProperties] #[ORM\Entity(repositoryClass: AccountRepository::class)]
#[ORM\Table(name: '`account`')]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])]
#[UniqueEntity(fields: ['email'], message: 'Cette adresse e-mail est déjà utilisée.')]
#[UniqueEntity(fields: ['uuid'], message: 'Cet identifiant unique (UUID) est déjà utilisé.')]
#[Vich\Uploadable()]
class Account implements UserInterface, PasswordAuthenticatedUserInterface, \Serializable
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

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

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

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

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

    #[ORM\Column(type: Types::GUID)]
    private ?string $uuid = null;

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

    #[Vich\UploadableField(mapping: 'avatar',fileNameProperty: 'avatarFileName', size: 'avatarSize', mimeType: 'avatarMineType', originalName: 'avatarOriginalName',dimensions: 'avatarDimensions')]
    private ?File $avatar = null;

    #[ORM\Column(nullable: true)]
    private ?string $avatarFileName = null;
    #[ORM\Column(nullable: true)]
    private ?array $avatarDimensions = [];
    #[ORM\Column(length: 255,nullable: true)]
    private ?string $avatarSize = null;
    #[ORM\Column(length: 255,nullable: true)]
    private ?string $avatarMineType = null;
    #[ORM\Column(length: 255,nullable: true)]
    private ?string $avatarOriginalName = null;
    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $updateAt;

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

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

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


    public function __construct()
    {
        $this->accountLoginRegisters = new ArrayCollection();
        $this->onBoaringEpages = new ArrayCollection();
    }

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

    public function getUsername(): ?string
    {
        return $this->username;
    }

    public function setUsername(string $username): static
    {
        $this->username = $username;

        return $this;
    }

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

    /**
     * @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;

        return $this;
    }

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

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

        return $this;
    }

    /**
     * Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
     */

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

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

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

        return $this;
    }

    public function getUuid(): ?string
    {
        return $this->uuid;
    }

    public function setUuid(string $uuid): static
    {
        $this->uuid = $uuid;

        return $this;
    }

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

    public function setIsFirstLogin(?bool $isFirstLogin): static
    {
        $this->isFirstLogin = $isFirstLogin;

        return $this;
    }

    public function setAvatar(?File $avatar): self
    {
        $this->avatar = $avatar;
        if($avatar != null) {
            $this->updateAt = new \DateTimeImmutable();
        }
        return $this;
    }

    public function getAvatar(): ?File
    {
        return $this->avatar;
    }

    public function setAvatarSize(?string $avatarSize): self
    {
        $this->avatarSize = $avatarSize;
        return $this;
    }
    public function getAvatarSize(): ?string
    {
        return $this->avatarSize;
    }

    public function setAvatarDimensions(?array $avatarDimensions): static
    {
        $this->avatarDimensions = $avatarDimensions;
        return $this;
    }

    public function getAvatarDimensions(): ?array
    {
        return $this->avatarDimensions;
    }

    public function setAvatarMineType(?string $avatarMineType): static
    {
        $this->avatarMineType = $avatarMineType;
        return $this;
    }

    public function getAvatarMineType(): ?string
    {
        return $this->avatarMineType;
    }

    public function setAvatarOriginalName(?string $avatarOriginalName): self
    {
        $this->avatarOriginalName = $avatarOriginalName;
        return $this;
    }

    public function getAvatarOriginalName(): ?string
    {
        return $this->avatarOriginalName;
    }

    public function setAvatarFileName(?string $avatarFileName): static
    {
        $this->avatarFileName = $avatarFileName;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getAvatarFileName(): ?string
    {
        return $this->avatarFileName;
    }
    public function getUpdateAt(): ?\DateTimeImmutable
    {
        return $this->updateAt;
    }

    public function setUpdateAt(?\DateTimeImmutable $updateAt): static
    {
        $this->updateAt = $updateAt;
        return $this;
    }


    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->email,
            $this->username,
            $this->avatarFileName,
        ));
    }

    public function unserialize(string $data)
    {
        list (
            $this->id,
            $this->email,
            $this->username,
            $this->avatarFileName,
            ) = unserialize($data);
    }

    /**
     * @return Collection<int, AccountLoginRegister>
     */
    public function getAccountLoginRegisters(): Collection
    {
        return $this->accountLoginRegisters;
    }

    public function addAccountLoginRegister(AccountLoginRegister $accountLoginRegister): static
    {
        if (!$this->accountLoginRegisters->contains($accountLoginRegister)) {
            $this->accountLoginRegisters->add($accountLoginRegister);
            $accountLoginRegister->setAccount($this);
        }

        return $this;
    }

    public function removeAccountLoginRegister(AccountLoginRegister $accountLoginRegister): static
    {
        if ($this->accountLoginRegisters->removeElement($accountLoginRegister)) {
            // set the owning side to null (unless already changed)
            if ($accountLoginRegister->getAccount() === $this) {
                $accountLoginRegister->setAccount(null);
            }
        }

        return $this;
    }

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

    public function setIsActif(?bool $isActif): static
    {
        $this->isActif = $isActif;

        return $this;
    }

    /**
     * @return Collection<int, OnBoaringEpage>
     */
    public function getOnBoaringEpages(): Collection
    {
        return $this->onBoaringEpages;
    }

    public function addOnBoaringEpage(OnBoaringEpage $onBoaringEpage): static
    {
        if (!$this->onBoaringEpages->contains($onBoaringEpage)) {
            $this->onBoaringEpages->add($onBoaringEpage);
            $onBoaringEpage->setAccount($this);
        }

        return $this;
    }

    public function removeOnBoaringEpage(OnBoaringEpage $onBoaringEpage): static
    {
        if ($this->onBoaringEpages->removeElement($onBoaringEpage)) {
            // set the owning side to null (unless already changed)
            if ($onBoaringEpage->getAccount() === $this) {
                $onBoaringEpage->setAccount(null);
            }
        }

        return $this;
    }
}
