<?php

declare(strict_types=1);

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: TypeFraisRepository::class)]
#[ORM\Table(name: 'type_frais')]
#[ORM\UniqueConstraint(name: 'UNIQ_TYPE_FRAIS_ECOLE_CODE', columns: ['ecole_id', 'code'])]
#[ORM\HasLifecycleCallbacks]
class TypeFrais
{
    public const NATURE_INSCRIPTION = 'INSCRIPTION';
    public const NATURE_REINSCRIPTION = 'REINSCRIPTION';
    public const NATURE_ANNUEL = 'ANNUEL';
    public const NATURE_MENSUEL = 'MENSUEL';
    public const NATURE_DOCUMENT = 'DOCUMENT';
    public const NATURE_PERSONNALISE = 'PERSONNALISE';
    public const NATURE_AUTRE = 'AUTRE';

    public const PERIODICITE_UNIQUE = 'UNIQUE';
    public const PERIODICITE_ANNUELLE = 'ANNUELLE';
    public const PERIODICITE_MENSUELLE = 'MENSUELLE';
    public const PERIODICITE_PERSONNALISEE = 'PERSONNALISEE';

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

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

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

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

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

    #[ORM\Column(length: 40)]
    private string $nature = self::NATURE_AUTRE;

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

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

    #[ORM\Column(options: ['default' => 1])]
    private int $ordre = 1;

    #[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 getCode(): ?string { return $this->code; }
    public function setCode(string $code): static { $this->code = $this->upper($code); return $this; }
    public function getLibelle(): ?string { return $this->libelle; }
    public function setLibelle(string $libelle): static { $this->libelle = trim($libelle); return $this; }
    public function getDescription(): ?string { return $this->description; }
    public function setDescription(?string $description): static { $this->description = $this->nullableTrim($description); return $this; }
    public function getNature(): string { return $this->nature; }
    public function setNature(string $nature): static { $this->nature = $this->upper($nature); return $this; }
    public function getPeriodicite(): ?string { return $this->periodicite; }
    public function setPeriodicite(?string $periodicite): static { $this->periodicite = $this->nullableUpper($periodicite); return $this; }
    public function getMontantDefaut(): ?string { return $this->montantDefaut; }
    public function setMontantDefaut(null|string|int|float $montantDefaut): static { $this->montantDefaut = $montantDefaut === null || $montantDefaut === '' ? null : (string) $montantDefaut; return $this; }
    public function getOrdre(): int { return $this->ordre; }
    public function setOrdre(int $ordre): static { $this->ordre = $ordre; 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 ? $this->upper($value) : null; }
    private function upper(string $value): string { return mb_strtoupper(trim($value)); }
}
