<?php

declare(strict_types=1);

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: RegleFraisRepository::class)]
#[ORM\Table(name: 'regle_frais')]
#[ORM\Index(name: 'IDX_REGLE_FRAIS_SCOPE', columns: ['ecole_id', 'annee_scolaire_id', 'section_id', 'classe_id', 'actif'])]
#[ORM\HasLifecycleCallbacks]
class RegleFrais
{
    #[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: 'CASCADE')]
    private ?TypeFrais $typeFrais = null;

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

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

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

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

    #[ORM\Column(nullable: true)]
    private ?int $moisDebut = null;

    #[ORM\Column(nullable: true)]
    private ?int $nombreEcheances = null;

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

    #[ORM\Column(options: ['default' => true])]
    private bool $actif = true;

    #[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 getTypeFrais(): ?TypeFrais { return $this->typeFrais; }
    public function setTypeFrais(?TypeFrais $typeFrais): static { $this->typeFrais = $typeFrais; return $this; }
    public function getSection(): ?Section { return $this->section; }
    public function setSection(?Section $section): static { $this->section = $section; return $this; }
    public function getClasse(): ?Classe { return $this->classe; }
    public function setClasse(?Classe $classe): static { $this->classe = $classe; 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 getPeriodicite(): ?string { return $this->periodicite; }
    public function setPeriodicite(?string $periodicite): static { $this->periodicite = $this->nullableUpper($periodicite); return $this; }
    public function getMoisDebut(): ?int { return $this->moisDebut; }
    public function setMoisDebut(?int $moisDebut): static { $this->moisDebut = $moisDebut; return $this; }
    public function getNombreEcheances(): ?int { return $this->nombreEcheances; }
    public function setNombreEcheances(?int $nombreEcheances): static { $this->nombreEcheances = $nombreEcheances; return $this; }
    public function getDescription(): ?string { return $this->description; }
    public function setDescription(?string $description): static { $this->description = $this->nullableTrim($description); return $this; }
    public function isActif(): bool { return $this->actif; }
    public function setActif(bool $actif): static { $this->actif = $actif; 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; }
    #[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; }
}
