<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Metadata\ApiFilter;
use App\Filter\SearchFilter;
use App\Repository\CustomerRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: CustomerRepository::class)]
#[ApiResource(
denormalizationContext: ['groups' => ['customer']],
normalizationContext: ['groups' => ['customer']],
security: "is_granted('ROLE_ADMIN')",
)]
#[ApiFilter(OrderFilter::class)]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
#[ApiFilter(SearchFilter::class, properties: ['email', 'firstName', 'lastName', 'phoneNumber', 'ponds.address1', 'ponds.postcode'])]
class Customer
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['customer', 'messageLog', 'schedule'])]
private int $id;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['customer', 'messageLog', 'schedule'])]
private ?string $email;
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['customer', 'messageLog', 'schedule', 'job', 'pond', 'serviceCampaignRecipient'])]
private string $firstName;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['customer', 'messageLog', 'schedule', 'job', 'pond', 'serviceCampaignRecipient'])]
private ?string $lastName = null;
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['customer', 'messageLog', 'schedule', 'serviceCampaignRecipient'])]
private string $phoneNumber;
#[ORM\Column(options: ["default" => 0])]
#[ApiProperty(attributes: ["openapi_context" => ["type" => "boolean", "default" => "false"]])]
#[Groups(['customer', 'messageLog'])]
private bool $consentToMarketing = false;
#[ORM\Column(type: 'integer', length: 1, options: ["default" => 0])]
#[Groups(['customer', 'messageLog', 'schedule'])]
private int $communicationPreference;
#[ORM\Column(type: 'text', nullable: true)]
#[Groups(['customer', 'schedule'])]
private ?string $notes;
#[ORM\OneToMany(mappedBy: 'customer', targetEntity: Pond::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups('customer')]
private Collection $ponds;
#[ORM\OneToMany(mappedBy: 'customer', targetEntity: MessageLog::class)]
#[Groups('customer')]
private Collection $messageLogs;
#[Gedmo\Timestampable(on: 'create')]
#[ORM\Column(type: 'datetime_immutable')]
private DateTimeImmutable $createdAt;
#[Gedmo\Timestampable]
#[ORM\Column(type: 'datetime_immutable')]
private DateTimeImmutable $updatedAt;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?DateTimeImmutable $deletedAt;
#[Pure] public function __construct()
{
$this->ponds = new ArrayCollection();
$this->messageLogs = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
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 getPhoneNumber(): string
{
return $this->phoneNumber;
}
public function setPhoneNumber(string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function hasConsentToMarketing(): bool
{
return $this->consentToMarketing;
}
public function setConsentToMarketing(int $consentToMarketing): self
{
$this->consentToMarketing = $consentToMarketing;
return $this;
}
public function getCommunicationPreference(): int
{
return $this->communicationPreference;
}
public function setCommunicationPreference(int $communicationPreference): self
{
$this->communicationPreference = $communicationPreference;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): void
{
$this->notes = $notes;
}
public function getPonds(): Collection
{
return $this->ponds;
}
public function addPond(Pond $pond): self
{
if (!$this->ponds->contains($pond)) {
$this->ponds[] = $pond;
$pond->setCustomer($this);
}
return $this;
}
public function removePond(Pond $pond): self
{
$this->ponds->removeElement($pond);
return $this;
}
public function getMessageLogs(): Collection
{
return $this->messageLogs;
}
public function addMessageLog(MessageLog $messageLog): self
{
if (!$this->messageLogs->contains($messageLog)) {
$this->messageLogs[] = $messageLog;
$messageLog->setCustomer($this);
}
return $this;
}
public function removeMessageLog(MessageLog $messageLog): self
{
if ($this->messageLogs->removeElement($messageLog)) {
// set the owning side to null (unless already changed)
if ($messageLog->getCustomer() === $this) {
$messageLog->setCustomer(null);
}
}
return $this;
}
public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}
public function getUpdatedAt(): DateTimeImmutable
{
return $this->updatedAt;
}
public function getDeletedAt(): ?DateTimeImmutable
{
return $this->deletedAt;
}
}