<?php
namespace App\Entity;
use App\Repository\AlertRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: AlertRepository::class)]
#[ORM\Index(name: "idx_alert_user", columns: ["user_id"])]
#[ORM\Index(name: "idx_alert_concerned_user", columns: ["concerned_user_id"])]
#[ORM\Index(name: "idx_alert_statut", columns: ["statut"])]
#[ORM\Index(name: "idx_alert_priority", columns: ["priorite"])]
#[ORM\Index(name: "idx_alert_created", columns: ["created_at"])]
class Alert
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 100)]
private ?string $type = null; // Type d'alerte
#[ORM\Column(length: 50)]
private ?string $categorie = null; // stocks, ventes, rh, caisse, comptabilite, etc.
#[ORM\Column(length: 20)]
private ?string $priorite = 'normale'; // critique, urgente, normale, info
#[ORM\Column(length: 255)]
private ?string $titre = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $message = null;
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $data = null; // Données additionnelles en JSON
#[ORM\Column(length: 255, nullable: true)]
private ?string $lien = null; // Lien vers la ressource concernée
#[ORM\Column(length: 50, nullable: true)]
private ?string $icone = null; // Icône Bootstrap Icons
#[ORM\Column(length: 50, nullable: true)]
private ?string $couleur = null; // Couleur badge/notification
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: true)]
private ?User $user = null; // Si alerte pour un utilisateur spécifique
#[ORM\Column(length: 255, nullable: true)]
private ?string $roles = null; // Rôles autorisés à voir l'alerte (JSON array serialized)
#[ORM\Column(length: 20)]
private ?string $statut = 'nouvelle'; // nouvelle, vue, traitee, archivee
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $viewedAt = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $treatedAt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $expireAt = null; // Date d'expiration de l'alerte
#[ORM\Column]
private ?bool $persistent = false; // Si l'alerte reste même après traitement
#[ORM\Column(nullable: true)]
private ?int $entityId = null; // ID de l'entité concernée
#[ORM\Column(length: 100, nullable: true)]
private ?string $entityType = null; // Type d'entité (Conge, Produit, etc.)
#[ORM\ManyToOne(targetEntity: Boutique::class)]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?Boutique $boutique = null; // Boutique concernée par l'alerte
#[ORM\Column(length: 100, nullable: true)]
private ?string $messageTemplate = null; // Template de message pour personnalisation
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $messageData = null; // Données pour remplacer les placeholders du template
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?User $concernedUser = null; // Utilisateur directement concerné par l'alerte
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->statut = 'nouvelle';
$this->priorite = 'normale';
}
// Getters et Setters
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): static
{
$this->type = $type;
return $this;
}
public function getCategorie(): ?string
{
return $this->categorie;
}
public function setCategorie(string $categorie): static
{
$this->categorie = $categorie;
return $this;
}
public function getPriorite(): ?string
{
return $this->priorite;
}
public function setPriorite(string $priorite): static
{
$this->priorite = $priorite;
return $this;
}
public function getTitre(): ?string
{
return $this->titre;
}
public function setTitre(string $titre): static
{
$this->titre = $titre;
return $this;
}
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage(string $message): static
{
$this->message = $message;
return $this;
}
public function getData(): ?array
{
return $this->data;
}
public function setData(?array $data): static
{
$this->data = $data;
return $this;
}
public function getLien(): ?string
{
return $this->lien;
}
public function setLien(?string $lien): static
{
$this->lien = $lien;
return $this;
}
public function getIcone(): ?string
{
return $this->icone;
}
public function setIcone(?string $icone): static
{
$this->icone = $icone;
return $this;
}
public function getCouleur(): ?string
{
return $this->couleur;
}
public function setCouleur(?string $couleur): static
{
$this->couleur = $couleur;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
public function getRoles(): ?string
{
return $this->roles;
}
public function setRoles(?string $roles): static
{
$this->roles = $roles;
return $this;
}
public function getRolesArray(): array
{
return $this->roles ? json_decode($this->roles, true) : [];
}
public function setRolesArray(array $roles): static
{
$this->roles = json_encode($roles);
return $this;
}
public function getStatut(): ?string
{
return $this->statut;
}
public function setStatut(string $statut): static
{
$this->statut = $statut;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getViewedAt(): ?\DateTimeImmutable
{
return $this->viewedAt;
}
public function setViewedAt(?\DateTimeImmutable $viewedAt): static
{
$this->viewedAt = $viewedAt;
return $this;
}
public function getTreatedAt(): ?\DateTimeImmutable
{
return $this->treatedAt;
}
public function setTreatedAt(?\DateTimeImmutable $treatedAt): static
{
$this->treatedAt = $treatedAt;
return $this;
}
public function getExpireAt(): ?\DateTimeInterface
{
return $this->expireAt;
}
public function setExpireAt(?\DateTimeInterface $expireAt): static
{
$this->expireAt = $expireAt;
return $this;
}
public function isPersistent(): ?bool
{
return $this->persistent;
}
public function setPersistent(bool $persistent): static
{
$this->persistent = $persistent;
return $this;
}
public function getEntityId(): ?int
{
return $this->entityId;
}
public function setEntityId(?int $entityId): static
{
$this->entityId = $entityId;
return $this;
}
public function getEntityType(): ?string
{
return $this->entityType;
}
public function setEntityType(?string $entityType): static
{
$this->entityType = $entityType;
return $this;
}
// Méthodes utilitaires
public function isExpired(): bool
{
if (!$this->expireAt) {
return false;
}
return $this->expireAt < new \DateTime();
}
public function isNouvelle(): bool
{
return $this->statut === 'nouvelle';
}
public function isVue(): bool
{
return in_array($this->statut, ['vue', 'traitee', 'archivee']);
}
public function isCritique(): bool
{
return $this->priorite === 'critique';
}
public function isUrgente(): bool
{
return $this->priorite === 'urgente';
}
public function isNormale(): bool
{
return $this->priorite === 'normale';
}
public function isInfo(): bool
{
return $this->priorite === 'info';
}
public function isTraitee(): bool
{
return $this->statut === 'traitee';
}
public function isArchivee(): bool
{
return $this->statut === 'archivee';
}
public function marquerCommeVue(): void
{
if ($this->statut === 'nouvelle') {
$this->statut = 'vue';
$this->viewedAt = new \DateTimeImmutable();
}
}
public function marquerCommeTraitee(): void
{
$this->statut = 'traitee';
$this->treatedAt = new \DateTimeImmutable();
}
public function archiver(): void
{
$this->statut = 'archivee';
}
public function getMessageTemplate(): ?string
{
return $this->messageTemplate;
}
public function setMessageTemplate(?string $messageTemplate): static
{
$this->messageTemplate = $messageTemplate;
return $this;
}
public function getMessageData(): ?array
{
return $this->messageData;
}
public function setMessageData(?array $messageData): static
{
$this->messageData = $messageData;
return $this;
}
public function getConcernedUser(): ?User
{
return $this->concernedUser;
}
public function setConcernedUser(?User $concernedUser): static
{
$this->concernedUser = $concernedUser;
return $this;
}
public function getBoutique(): ?Boutique
{
return $this->boutique;
}
public function setBoutique(?Boutique $boutique): static
{
$this->boutique = $boutique;
return $this;
}
/**
* Vérifie si un utilisateur donné est directement concerné par cette alerte
* (soit il est le concernedUser, soit l'alerte lui est personnellement adressée)
*/
public function isUserConcerned(User $user): bool
{
// Si l'utilisateur est le concerné direct
if ($this->concernedUser && $this->concernedUser->getId() === $user->getId()) {
return true;
}
// Si l'alerte est personnelle pour cet utilisateur
if ($this->user && $this->user->getId() === $user->getId()) {
return true;
}
return false;
}
public function __toString(): string
{
return $this->titre ?? 'Alerte #' . $this->id;
}
}