<?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 PhpParser\Node\Scalar\String_;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 *
 */
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
	#[ORM\Id]
	#[ORM\GeneratedValue]
	#[ORM\Column(type: 'integer')]
	private int $id;
	
	#[ORM\Column(type: 'string', length: 180, unique: true)]
	private string $username;
	
	#[ORM\Column(type: 'json')]
	private array $roles = [];
	
	#[ORM\Column(type: 'string')]
	private string $password;
	
	private string $plainPassword;
	
	
	#[ORM\Column(type: 'string', length: 255)]
	private string $email;
	
	#[ORM\Column(type: 'string', length: 255, nullable: true)]
	private ?string $firstName = '';
	
	#[ORM\Column(type: 'string', length: 255, nullable: true)]
	private ?string $lastName = '';
	
	#[ORM\ManyToMany(targetEntity: Projects::class, mappedBy: 'developer')]
	private $projects;
	
	#[ORM\Column(type: 'string', length: 255, nullable: true)]
	private $avatar;
	
	public function __construct()
	{
		$this->projects = new ArrayCollection();
	}
	
	/**
	 * @return null|string
	 */
	public function __toString()
	{
		return $this->username;
	}
	
	public function getId(): ?int
	{
		return $this->id;
	}
	
	/**
	 * @return string
	 */
	public function getPlainPassword(): string
	{
		return $this->plainPassword;
	}
	
	public function getUsername(): ?string
	{
		return $this->username;
	}
	
	public function setUsername(string $username): self
	{
		$this->username = $username;
		
		return $this;
	}
	
	/**
	 * A visual identifier that represents this user.
	 *
	 * @see UserInterface
	 */
	public function getUserIdentifier(): string
	{
		return $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(array: $roles);
	}
	
	public function setRoles(array $roles): self
	{
		$this->roles = $roles;
		
		return $this;
	}
	
	/**
	 * @see PasswordAuthenticatedUserInterface
	 */
	public function getPassword(): string
	{
		return $this->password;
	}
	
	public function setPassword(string $password): self
	{
		$this->password = $password;
		
		return $this;
	}
	
	/**
	 * @see UserInterface
	 */
	public function eraseCredentials()
	{
		$this->plainPassword = '';
	}
	
	public function getEmail(): ?string
	{
		return $this->email;
	}
	
	public function setEmail(string $email): self
	{
		$this->email = $email;
		
		return $this;
	}
	
	/**
	 * @param string $plainPassword
	 */
	public function setPlainPassword(string $plainPassword): void
	{
		$this->plainPassword = $plainPassword;
	}
	
	public function getFirstName(): ?string
	{
		return $this->firstName;
	}
	
	public function setFirstName(?string $firstName): self
	{
		$this->firstName = $firstName;
		
		return $this;
	}
	
	public function getLastName(): ?string
	{
		return $this->lastName;
	}
	
	public function setLastName(?string $lastName): self
	{
		$this->lastName = $lastName;
		
		return $this;
	}
	
	/**
	 * @return Collection<int, Projects>
	 */
	public function getProjects(): Collection
	{
		return $this->projects;
	}
	
	public function addProject(Projects $project): self
	{
		if (!$this->projects->contains(element: $project)) {
			$this->projects[] = $project;
			$project->addDeveloper(developer: $this);
		}
		
		return $this;
	}
	
	public function removeProject(Projects $project): self
	{
		if ($this->projects->removeElement(element: $project)) {
			$project->removeDeveloper(developer: $this);
		}
		
		return $this;
	}
	
	public function getAvatar(): string
	{
		return $this->avatar ?? '';
		
	}
	
	public function setAvatar(?string $avatar): self
	{
		$this->avatar = $avatar;
		
		return $this;
	}
}
