<?php

declare(strict_types=1);

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: ParcoursEtudiantRepository::class)]
#[ORM\Table(name: 'parcours_etudiant')]
#[ORM\Index(name: 'IDX_PARCOURS_ETUDIANT_ACTIVE_CLASS', columns: ['etudiant_id', 'annee_scolaire_id', 'classe_id', 'actif', 'statut'])]
#[ORM\HasLifecycleCallbacks]
class ParcoursEtudiant
{
    public const STATUT_ACTIF = 'ACTIF';
    public const STATUT_SUSPENDU = 'SUSPENDU';
    public const STATUT_TERMINE = 'TERMINE';
    public const STATUT_ANNULE = 'ANNULE';

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

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

    #[ORM\ManyToOne(inversedBy: 'parcours')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ?Etudiant $etudiant = null;

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

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

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

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

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

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

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

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

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

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

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

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

    #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $dateDebut = null;

    #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $dateFin = null;

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

    #[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 getEtudiant(): ?Etudiant { return $this->etudiant; }
    public function setEtudiant(?Etudiant $etudiant): static { $this->etudiant = $etudiant; return $this; }
    public function getAnneeScolaire(): ?AnneeScolaire { return $this->anneeScolaire; }
    public function setAnneeScolaire(?AnneeScolaire $anneeScolaire): static { $this->anneeScolaire = $anneeScolaire; return $this; }
    public function getSection(): ?Section { return $this->section; }
    public function setSection(?Section $section): static { $this->section = $section; return $this; }
    public function getNiveau(): ?Niveau { return $this->niveau; }
    public function setNiveau(?Niveau $niveau): static { $this->niveau = $niveau; return $this; }
    public function getClasse(): ?Classe { return $this->classe; }
    public function setClasse(?Classe $classe): static { $this->classe = $classe; return $this; }
    public function getInscription(): ?Inscription { return $this->inscription; }
    public function setInscription(?Inscription $inscription): static { $this->inscription = $inscription; return $this; }
    public function getInscriptionParcours(): ?InscriptionParcours { return $this->inscriptionParcours; }
    public function setInscriptionParcours(?InscriptionParcours $inscriptionParcours): static { $this->inscriptionParcours = $inscriptionParcours; return $this; }
    public function getLangue(): ?string { return $this->langue; }
    public function setLangue(?string $langue): static { $this->langue = $this->nullableUpper($langue); return $this; }
    public function getMode(): ?string { return $this->mode; }
    public function setMode(?string $mode): static { $this->mode = $this->nullableUpper($mode); return $this; }
    public function getTarifApplique(): ?string { return $this->tarifApplique; }
    public function setTarifApplique(?string $tarifApplique): static { $this->tarifApplique = $tarifApplique; 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 isPrincipal(): bool { return $this->principal; }
    public function setPrincipal(bool $principal): static { $this->principal = $principal; return $this; }
    public function isActif(): bool { return $this->actif; }
    public function setActif(bool $actif): static { $this->actif = $actif; return $this; }
    public function getDateDebut(): ?\DateTimeInterface { return $this->dateDebut; }
    public function setDateDebut(?\DateTimeInterface $dateDebut): static { $this->dateDebut = $dateDebut ? \DateTime::createFromInterface($dateDebut) : null; return $this; }
    public function getDateFin(): ?\DateTimeInterface { return $this->dateFin; }
    public function setDateFin(?\DateTimeInterface $dateFin): static { $this->dateFin = $dateFin ? \DateTime::createFromInterface($dateFin) : null; return $this; }
    public function getObservation(): ?string { return $this->observation; }
    public function setObservation(?string $observation): static { $this->observation = $this->nullableTrim($observation); 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; }
}
