<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
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]
    private ?int $id = null;

    #[ORM\Column(length: 180, unique: true)]
    private ?string $email = null;

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

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

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
    private ?MediaObject $image = null;

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

    #[ORM\Column]
    private array $roles = [];

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

    #[ORM\Column]
    private bool $active;

    #[ORM\Column]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Posting::class)]
    private Collection $postings;

    #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Comment::class)]
    private Collection $comments;

    #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Sale::class)]
    private Collection $sales;

    #[ORM\OneToMany(mappedBy: 'user', targetEntity: PartnerFollow::class)]
    private Collection $partnerFollows;

    #[ORM\OneToMany(mappedBy: 'createdBy', targetEntity: Document::class)]
    private Collection $documentObjects;

    #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserProduct::class)]
    private Collection $userProducts;


    public function __construct()
    {
        $this->createdAt = new \DateTimeImmutable();
        $this->postings = new ArrayCollection();
        $this->active = true;
        $this->comments = new ArrayCollection();
        $this->sales = new ArrayCollection();
        $this->partnerFollows = new ArrayCollection();
        $this->documentObjects = new ArrayCollection();
        $this->userProducts = new ArrayCollection();
    }

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

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

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

        return $this;
    }

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

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

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

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

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

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(string $lastName): static
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getGoals(): ?string
    {
        return $this->goals;
    }

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

    public function getImage(): ?MediaObject
    {
        return $this->image;
    }

    public function setImage(?MediaObject $image): void
    {
        $this->image = $image;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    /**
     * @return Collection<int, Posting>
     */
    public function getPostings(): Collection
    {
        return $this->postings;
    }

    public function setPostings(Collection $postings): void
    {
        $this->postings = $postings;
    }

    public function addUserPost(Posting $post): static
    {
        if (!$this->postings->contains($post)) {
            $this->postings->add($post);
            $post->setOwner($this);
        }

        return $this;
    }

    public function removeUserPost(Posting $post): static
    {
        if ($this->postings->removeElement($post)) {
            // set the owning side to null (unless already changed)
            if ($post->getOwner() === $this) {
                $post->setOwner(null);
            }
        }

        return $this;
    }

    public function isActive(): bool
    {
        return $this->active;
    }

    public function setActive(bool $active): static
    {
        $this->active = $active;

        return $this;
    }

    /**
     * @return Collection<int, Comment>
     */
    public function getComments(): Collection
    {
        return $this->comments;
    }

    public function addComment(Comment $comment): static
    {
        if (!$this->comments->contains($comment)) {
            $this->comments->add($comment);
            $comment->setOwner($this);
        }

        return $this;
    }

    public function removeComment(Comment $comment): static
    {
        if ($this->comments->removeElement($comment)) {
            // set the owning side to null (unless already changed)
            if ($comment->getOwner() === $this) {
                $comment->setOwner(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Sale>
     */
    public function getSales(): Collection
    {
        return $this->sales;
    }

    public function addSale(Sale $sale): static
    {
        if (!$this->sales->contains($sale)) {
            $this->sales->add($sale);
            $sale->setOwner($this);
        }

        return $this;
    }

    public function removeSale(Sale $sale): static
    {
        if ($this->sales->removeElement($sale)) {
            // set the owning side to null (unless already changed)
            if ($sale->getOwner() === $this) {
                $sale->setOwner(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, PartnerFollow>
     */
    public function getPartnerFollows(): Collection
    {
        return $this->partnerFollows;
    }

    public function addPartnerFollow(PartnerFollow $partnerFollow): static
    {
        if (!$this->partnerFollows->contains($partnerFollow)) {
            $this->partnerFollows->add($partnerFollow);
            $partnerFollow->setUser($this);
        }

        return $this;
    }

    public function removePartnerFollow(PartnerFollow $partnerFollow): static
    {
        if ($this->partnerFollows->removeElement($partnerFollow)) {
            // set the owning side to null (unless already changed)
            if ($partnerFollow->getUser() === $this) {
                $partnerFollow->setUser(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Document>
     */
    public function getDocumentObjects(): Collection
    {
        return $this->documentObjects;
    }

    public function addDocumentObject(Document $documentObject): static
    {
        if (!$this->documentObjects->contains($documentObject)) {
            $this->documentObjects->add($documentObject);
            $documentObject->setCreatedBy($this);
        }

        return $this;
    }

    public function removeDocumentObject(Document $documentObject): static
    {
        if ($this->documentObjects->removeElement($documentObject)) {
            // set the owning side to null (unless already changed)
            if ($documentObject->getCreatedBy() === $this) {
                $documentObject->setCreatedBy(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, UserProduct>
     */
    public function getUserProducts(): Collection
    {
        return $this->userProducts;
    }

    public function addUserProduct(UserProduct $userProduct): static
    {
        if (!$this->userProducts->contains($userProduct)) {
            $this->userProducts->add($userProduct);
            $userProduct->setUser($this);
        }

        return $this;
    }

    public function removeUserProduct(UserProduct $userProduct): static
    {
        if ($this->userProducts->removeElement($userProduct)) {
            // set the owning side to null (unless already changed)
            if ($userProduct->getUser() === $this) {
                $userProduct->setUser(null);
            }
        }

        return $this;
    }
}
