<?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 Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])]
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
class User extends AbstractEntity implements UserInterface, PasswordAuthenticatedUserInterface
{
    use ToggleableTrait;

    const string ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
    const string ROLE_USER = 'ROLE_USER';

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

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

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

    /**
     * @var Collection<int, UserGroup>
     */
    #[ORM\ManyToMany(targetEntity: UserGroup::class, inversedBy: 'users')]
    private Collection $userGroups;

    /**
     * @var Collection<int, OrganizationalUnit>
     */
    #[ORM\ManyToMany(targetEntity: OrganizationalUnit::class, inversedBy: 'users')]
    private Collection $allowedOrganizationalUnits;

    private ?string $currentPassword = null;
    private ?string $newPassword = null;
    private ?string $repeatNewPassword = null;

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


    public function __construct()
    {
        parent::__construct();
        $this->userGroups = new ArrayCollection();
        $this->allowedOrganizationalUnits = new ArrayCollection();
    }

    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
     *
     * @return list<string>
     */
    public function getRoles(): array
    {
        $roles = $this->roles;

        if (!array_search(self::ROLE_USER, $roles, true)){
            $roles[] = self::ROLE_USER;
        }

        foreach ($this->getUserGroups() as $userGroup) {
            if ($userGroup->isEnabled()){
                $roles = array_merge($roles, $userGroup->getPermissions());
            }
        }

        return array_merge(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;
    }

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

    /**
     * @return Collection<int, UserGroup>
     */
    public function getUserGroups(): Collection
    {
        return $this->userGroups;
    }

    public function setUserGroups(array $userGroup): static
    {
        $this->userGroups->clear();

        foreach ($userGroup as $group){
            $this->addUserGroup($group);
        }

        return $this;
    }

    public function addUserGroup(UserGroup $userGroup): static
    {
        if (!$this->userGroups->contains($userGroup)) {
            $this->userGroups->add($userGroup);

        }

        return $this;
    }

    public function removeUserGroup(UserGroup $userGroup): static
    {
        if ($this->userGroups->removeElement($userGroup)) {
            $userGroup->removeUser($this);
        }

        return $this;
    }

    /**
     * @return Collection<int, OrganizationalUnit>
     */
    public function getAllowedOrganizationalUnits(): Collection
    {
        return $this->allowedOrganizationalUnits;
    }

    public function setAllowedOrganizationalUnits(array $allowedOrganizationalUnits): static
    {
        $this->allowedOrganizationalUnits->clear();

        foreach ($allowedOrganizationalUnits as $allowedOrganizationalUnit){
            $this->addAllowedOrganizationalUnit($allowedOrganizationalUnit);
        }

        return $this;
    }

    public function addAllowedOrganizationalUnit(OrganizationalUnit $allowedOrganizationalUnit): static
    {
        if (!$this->allowedOrganizationalUnits->contains($allowedOrganizationalUnit)) {
            $this->allowedOrganizationalUnits->add($allowedOrganizationalUnit);
        }

        return $this;
    }

    public function removeAllowedOrganizationalUnit(OrganizationalUnit $allowedOrganizationalUnit): static
    {
        $this->allowedOrganizationalUnits->removeElement($allowedOrganizationalUnit);

        return $this;
    }

    public function getCurrentPassword(): ?string
    {
        return $this->currentPassword;
    }

    public function setCurrentPassword(?string $currentPassword): static
    {
        $this->currentPassword = $currentPassword;

        return $this;
    }

    public function getNewPassword(): ?string
    {
        return $this->newPassword;
    }

    public function setNewPassword(?string $newPassword): static
    {
        $this->newPassword = $newPassword;

        return $this;
    }

    public function getRepeatNewPassword(): ?string
    {
        return $this->repeatNewPassword;
    }

    public function setRepeatNewPassword(?string $repeatNewPassword): static
    {
        $this->repeatNewPassword = $repeatNewPassword;

        return $this;
    }

    public function getGroupsView(): ?string
    {
        return $this->groupsView;
    }

    public function setGroupsView(?string $groupsView): static
    {
        $this->groupsView = $groupsView;

        return $this;
    }
}
