<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\DepenseRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: DepenseRepository::class)]
#[ORM\Table(name: 'depenses')]
#[ORM\Index(name: 'IDX_DEPENSE_ECOLE_DATE', columns: ['ecole_id', 'date_depense', 'statut'])]
#[ORM\HasLifecycleCallbacks]
class Depense
{
    public const STATUT_VALIDEE = 'VALIDEE';
    public const STATUT_ANNULEE = 'ANNULEE';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ?Ecole $ecole = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(onDelete: 'SET NULL')]
    private ?AnneeScolaire $anneeScolaire = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false, onDelete: 'RESTRICT')]
    private ?CategorieDepense $categorie = null;

    #[ORM\Column(type: Types::DECIMAL, precision: 12, scale: 2)]
    private ?string $montant = null;

    #[ORM\Column(type: Types::DATE_MUTABLE)]
    private ?\DateTimeInterface $dateDepense = null;

    #[ORM\Column(length: 160, nullable: true)]
    private ?string $beneficiaire = null;

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

    #[ORM\Column(length: 40, nullable: true)]
    private ?string $modePaiement = null;

    #[ORM\Column(length: 120, nullable: true)]
    private ?string $reference = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $justificatifPath = null;

    #[ORM\Column(length: 30, options: ['default' => self::STATUT_VALIDEE])]
    private string $statut = self::STATUT_VALIDEE;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private ?\DateTimeImmutable $updatedAt = null;

    public function getId(): ?int { return $this->id; }
    public function getEcole(): ?Ecole { return $this->ecole; }
    public function setEcole(?Ecole $ecole): static { $this->ecole = $ecole; return $this; }
    public function getAnneeScolaire(): ?AnneeScolaire { return $this->anneeScolaire; }
    public function setAnneeScolaire(?AnneeScolaire $anneeScolaire): static { $this->anneeScolaire = $anneeScolaire; return $this; }
    public function getCategorie(): ?CategorieDepense { return $this->categorie; }
    public function setCategorie(?CategorieDepense $categorie): static { $this->categorie = $categorie; return $this; }
    public function getMontant(): ?string { return $this->montant; }
    public function setMontant(string|int|float $montant): static { $this->montant = (string) $montant; return $this; }
    public function getDateDepense(): ?\DateTimeInterface { return $this->dateDepense; }
    public function setDateDepense(\DateTimeInterface $dateDepense): static { $this->dateDepense = \DateTime::createFromInterface($dateDepense); return $this; }
    public function getBeneficiaire(): ?string { return $this->beneficiaire; }
    public function setBeneficiaire(?string $beneficiaire): static { $this->beneficiaire = $this->nullableTrim($beneficiaire); return $this; }
    public function getDescription(): ?string { return $this->description; }
    public function setDescription(?string $description): static { $this->description = $this->nullableTrim($description); return $this; }
    public function getModePaiement(): ?string { return $this->modePaiement; }
    public function setModePaiement(?string $modePaiement): static { $this->modePaiement = $this->nullableUpper($modePaiement); return $this; }
    public function getReference(): ?string { return $this->reference; }
    public function setReference(?string $reference): static { $this->reference = $this->nullableTrim($reference); return $this; }
    public function getJustificatifPath(): ?string { return $this->justificatifPath; }
    public function setJustificatifPath(?string $justificatifPath): static { $this->justificatifPath = $this->nullableTrim($justificatifPath); return $this; }
    public function getStatut(): string { return $this->statut; }
    public function setStatut(string $statut): static { $this->statut = mb_strtoupper(trim($statut)); return $this; }
    public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; }
    public function getUpdatedAt(): ?\DateTimeImmutable { return $this->updatedAt; }

    #[ORM\PrePersist]
    public function initializeTimestamps(): void { $now = new \DateTimeImmutable(); $this->createdAt ??= $now; $this->updatedAt = $now; $this->dateDepense ??= new \DateTimeImmutable(); }
    #[ORM\PreUpdate]
    public function refreshUpdatedAt(): void { $this->updatedAt = new \DateTimeImmutable(); }
    private function nullableTrim(?string $value): ?string { $value = $value !== null ? trim($value) : null; return $value !== '' ? $value : null; }
    private function nullableUpper(?string $value): ?string { $value = $this->nullableTrim($value); return $value !== null ? mb_strtoupper($value) : null; }
}
