<?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 Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
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;

#[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é.')]
class Account implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface
{
    #[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(nullable: true)]
    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;
    #[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;

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

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

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

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $googleAuthenticatorSecret = null;

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

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

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $confirmationToken;

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

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

    /**
     * @return Collection<int, EtatLieux>
     */
    public function getEtatLieuxes(): Collection
    {
        return $this->etatLieuxes;
    }

    public function addEtatLieux(EtatLieux $etatLieux): static
    {
        if (!$this->etatLieuxes->contains($etatLieux)) {
            $this->etatLieuxes->add($etatLieux);
            $etatLieux->setAccount($this);
        }

        return $this;
    }

    public function removeEtatLieux(EtatLieux $etatLieux): static
    {
        if ($this->etatLieuxes->removeElement($etatLieux)) {
            // set the owning side to null (unless already changed)
            if ($etatLieux->getAccount() === $this) {
                $etatLieux->setAccount(null);
            }
        }

        return $this;
    }

    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 getUpdateAt(): ?\DateTimeImmutable
    {
        return $this->updateAt;
    }

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


    public function __serialize(): array
    {
        return [
            'id' => $this->id,
            'email' => $this->email,
            'username' => $this->username,
            'googleAuthenticatorSecret' => $this->googleAuthenticatorSecret,
            'uuid' => $this->uuid,
        ];
    }

    public function __unserialize(array $data): void
    {
        $this->id = $data['id'] ?? null;
        $this->email = $data['email'] ?? null;
        $this->username = $data['username'] ?? null;
        $this->googleAuthenticatorSecret = $data['googleAuthenticatorSecret'] ?? null;
        $this->uuid = $data['uuid'] ?? null;
    }

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

    public function getKeycloakId(): ?string
    {
        return $this->keycloakId;
    }

    public function setKeycloakId(?string $keycloakId): static
    {
        $this->keycloakId = $keycloakId;

        return $this;
    }

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

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

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): static
    {
        $this->name = $name;

        return $this;
    }

    public function isGoogleAuthenticatorEnabled(): bool
    {
        // La 2FA est active si l'utilisateur a un secret généré
        return null !== $this->googleAuthenticatorSecret;
    }

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

    public function getGoogleAuthenticatorSecret(): ?string
    {
        return $this->googleAuthenticatorSecret;
    }

    public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
    {
        $this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
    }

    /**
     * @return Collection<int, AuditLog>
     */
    public function getAuditLogs(): Collection
    {
        return $this->auditLogs;
    }

    public function addAuditLog(AuditLog $auditLog): static
    {
        if (!$this->auditLogs->contains($auditLog)) {
            $this->auditLogs->add($auditLog);
            $auditLog->setAccount($this);
        }

        return $this;
    }

    public function removeAuditLog(AuditLog $auditLog): static
    {
        if ($this->auditLogs->removeElement($auditLog)) {
            // set the owning side to null (unless already changed)
            if ($auditLog->getAccount() === $this) {
                $auditLog->setAccount(null);
            }
        }

        return $this;
    }

    public function getConfirmationToken(): ?string
    {
        return $this->confirmationToken;
    }

    public function setConfirmationTokenName(?string $token): static
    {
        $this->confirmationToken = $token;

        return $this;
    }
}
