<?php

namespace App\Entity;

use App\Repository\PrestaireRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: PrestaireRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'Il existe déjà un compte avec cet email')]
class Prestaire implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

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

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

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

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

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

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

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

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

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

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

    public function __construct()
    {
        $this->etatLieuxes = new ArrayCollection();
        $this->contrats = new ArrayCollection();
        $this->orderSessions = new ArrayCollection();
        $this->devis = new ArrayCollection();
    }

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

    /**
     * @return Collection<int, Devis>
     */
    public function getDevis(): Collection
    {
        return $this->devis;
    }

    public function addDevi(Devis $devi): static
    {
        if (!$this->devis->contains($devi)) {
            $this->devis->add($devi);
            $devi->setPrestataire($this);
        }

        return $this;
    }

    public function removeDevi(Devis $devi): static
    {
        if ($this->devis->removeElement($devi)) {
            // set the owning side to null (unless already changed)
            if ($devi->getPrestataire() === $this) {
                $devi->setPrestataire(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, OrderSession>
     */
    public function getOrderSessions(): Collection
    {
        return $this->orderSessions;
    }

    public function addOrderSession(OrderSession $orderSession): static
    {
        if (!$this->orderSessions->contains($orderSession)) {
            $this->orderSessions->add($orderSession);
            $orderSession->setPrestataire($this);
        }

        return $this;
    }

    public function removeOrderSession(OrderSession $orderSession): static
    {
        if ($this->orderSessions->removeElement($orderSession)) {
            // set the owning side to null (unless already changed)
            if ($orderSession->getPrestataire() === $this) {
                $orderSession->setPrestataire(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Contrats>
     */
    public function getContrats(): Collection
    {
        return $this->contrats;
    }

    public function addContrat(Contrats $contrat): static
    {
        if (!$this->contrats->contains($contrat)) {
            $this->contrats->add($contrat);
            $contrat->setPrestataire($this);
        }

        return $this;
    }

    public function removeContrat(Contrats $contrat): static
    {
        if ($this->contrats->removeElement($contrat)) {
            // set the owning side to null (unless already changed)
            if ($contrat->getPrestataire() === $this) {
                $contrat->setPrestataire(null);
            }
        }

        return $this;
    }

    /**
     * @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->setPrestataire($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->getPrestataire() === $this) {
                $etatLieux->setPrestataire(null);
            }
        }

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function getSurname(): ?string
    {
        return $this->surname;
    }

    public function setSurname(string $surname): static
    {
        $this->surname = $surname;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): static
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * @see PasswordAuthenticatedUserInterface
     */
    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): static
    {
        $this->password = $password;

        return $this;
    }

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

        return array_unique($roles);
    }

    /**
     * @param list<string> $roles
     */
    public function setRoles(array $roles): static
    {
        $this->roles = $roles;

        return $this;
    }

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

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