<?php declare(strict_types=1);

namespace App\Engine\AdministratorUser;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator as V;

#[ORM\Entity]
#[V\Constraints\UniqueEntity(fields: ["email"], message: "admin.administrator-user.validator.duplicate-email")]
class AdministratorUser implements \Symfony\Component\Security\Core\User\UserInterface, \Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
{

    public const string SYSTEM_ADMINISTRATOR = "debug@lifecode.cz";
    public bool $userAuthorizedToManageCredentials = false;
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;
    #[ORM\Column(type: 'uuid', unique: true)]
    private string $uuid;
    #[ORM\Column(type: 'string', length: 180, unique: true)]
    private ?string $email = null;
    #[ORM\Column(type: 'string')]
    private string $firstName = "";
    #[ORM\Column(type: 'string')]
    private string $lastName = "";
    #[ORM\Column(type: 'json')]
    private array $roles = [];
    #[ORM\Column(type: 'string')]
    private string $password = "";
    #[ORM\Column(type: 'string', enumType: StatusEnum::class)]
    private StatusEnum $status = StatusEnum::Inactive;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $contactAddress = null;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $employer = null;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $jobPosition = null;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $socialsWebsite = null;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $socialsPlatformX = null;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $socialsFacebook = null;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $socialsInstagram = null;
    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $socialsLinkedIn = null;

    public function __construct()
    {
        $this->uuid = \Symfony\Component\Uid\UuidV7::generate();
    }

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

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

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

    public function getRoles(): array
    {
        $roles = $this->roles;
        if (empty($roles)) {
            $roles[] = \App\Engine\Security\Role::ADMINISTRATOR->value;
        }

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;
        return $this;
    }

    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;
        return $this;
    }

    public function getStatus(): StatusEnum
    {
        return $this->status;
    }

    public function setStatus(StatusEnum $status): self
    {
        if ($this->email === self::SYSTEM_ADMINISTRATOR && $status !== StatusEnum::Active) {
            throw new \InvalidArgumentException("Nelze deaktivovat hlavního správce!");
        }

        $this->status = $status;
        return $this;
    }

    public function eraseCredentials(): void
    {
        // Zatím nic
    }

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

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

    public function setEmail(string $email): self
    {
        $this->email = $email;
        return $this;
    }

    public function getContactAddress(): ?string
    {
        return $this->contactAddress;
    }

    public function setContactAddress(?string $contactAddress): AdministratorUser
    {
        $this->contactAddress = $contactAddress;
        return $this;
    }

    public function getEmployer(): ?string
    {
        return $this->employer;
    }

    public function setEmployer(?string $employer): AdministratorUser
    {
        $this->employer = $employer;
        return $this;
    }

    public function getJobPosition(): ?string
    {
        return $this->jobPosition;
    }

    public function setJobPosition(?string $jobPosition): AdministratorUser
    {
        $this->jobPosition = $jobPosition;
        return $this;
    }

    public function getSocialsWebsite(): ?string
    {
        return $this->socialsWebsite;
    }

    public function setSocialsWebsite(?string $socialsWebsite): AdministratorUser
    {
        $this->socialsWebsite = $socialsWebsite;
        return $this;
    }

    public function getSocialsPlatformX(): ?string
    {
        return $this->socialsPlatformX;
    }

    public function setSocialsPlatformX(?string $socialsPlatformX): AdministratorUser
    {
        $this->socialsPlatformX = $socialsPlatformX;
        return $this;
    }

    public function getSocialsFacebook(): ?string
    {
        return $this->socialsFacebook;
    }

    public function setSocialsFacebook(?string $socialsFacebook): AdministratorUser
    {
        $this->socialsFacebook = $socialsFacebook;
        return $this;
    }

    public function getSocialsInstagram(): ?string
    {
        return $this->socialsInstagram;
    }

    public function setSocialsInstagram(?string $socialsInstagram): AdministratorUser
    {
        $this->socialsInstagram = $socialsInstagram;
        return $this;
    }

    public function getSocialsLinkedIn(): ?string
    {
        return $this->socialsLinkedIn;
    }

    public function setSocialsLinkedIn(?string $socialsLinkedIn): AdministratorUser
    {
        $this->socialsLinkedIn = $socialsLinkedIn;
        return $this;
    }

    public function getDisplayName(): string
    {
        return "{$this->getFirstName()} {$this->getLastName()}";
    }

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

    public function isSystemAdministrator(): bool
    {
        return $this->email === self::SYSTEM_ADMINISTRATOR;
    }

}