<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\ClasseRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: ClasseRepository::class)]
#[ORM\Table(name: 'classes')]
#[ORM\UniqueConstraint(name: 'UNIQ_CLASSE_ECOLE_SLUG', columns: ['ecole_id', 'slug'])]
#[ORM\HasLifecycleCallbacks]
class Classe
{
    #[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(nullable: false, onDelete: 'CASCADE')]
    private ?Section $section = null;

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

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

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

    #[ORM\Column(length: 160)]
    #[Assert\NotBlank]
    #[Assert\Regex(pattern: '/^[a-z0-9-]+$/')]
    private ?string $slug = null;

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

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

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

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

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

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

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

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

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

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

    #[ORM\Column(options: ['default' => true])]
    private bool $active = 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 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 getAnneeScolaire(): ?AnneeScolaire { return $this->anneeScolaire; }
    public function setAnneeScolaire(?AnneeScolaire $anneeScolaire): static { $this->anneeScolaire = $anneeScolaire; return $this; }
    public function getNom(): ?string { return $this->nom; }
    public function setNom(string $nom): static { $this->nom = trim($nom); return $this; }
    public function getSlug(): ?string { return $this->slug; }
    public function setSlug(string $slug): static { $this->slug = mb_strtolower(trim($slug)); return $this; }
    public function getLangue(): ?string { return $this->langue; }
    public function setLangue(?string $langue): static { $this->langue = $this->nullableTrim($langue); return $this; }
    public function getSexe(): ?string { return $this->sexe; }
    public function setSexe(?string $sexe): static { $this->sexe = $this->nullableTrim($sexe); return $this; }
    public function getMode(): ?string { return $this->mode; }
    public function setMode(?string $mode): static { $this->mode = $this->nullableTrim($mode); return $this; }
    public function getCapacite(): ?int { return $this->capacite; }
    public function setCapacite(?int $capacite): static { $this->capacite = $capacite; return $this; }
    public function getJoursCours(): ?string { return $this->joursCours; }
    public function setJoursCours(?string $joursCours): static { $this->joursCours = $this->nullableTrim($joursCours); return $this; }
    public function getHeureDebut(): ?\DateTimeInterface { return $this->heureDebut; }
    public function setHeureDebut(?\DateTimeInterface $heureDebut): static { $this->heureDebut = $heureDebut; return $this; }
    public function getHeureFin(): ?\DateTimeInterface { return $this->heureFin; }
    public function setHeureFin(?\DateTimeInterface $heureFin): static { $this->heureFin = $heureFin; return $this; }
    public function getTarifDefaut(): ?string { return $this->tarifDefaut; }
    public function setTarifDefaut(?string $tarifDefaut): static { $this->tarifDefaut = $tarifDefaut; return $this; }
    public function getWhatsappGroupId(): ?string { return $this->whatsappGroupId; }
    public function setWhatsappGroupId(?string $whatsappGroupId): static { $this->whatsappGroupId = $this->nullableTrim($whatsappGroupId); return $this; }
    public function getWhatsappGroupInviteLink(): ?string { return $this->whatsappGroupInviteLink; }
    public function setWhatsappGroupInviteLink(?string $whatsappGroupInviteLink): static { $this->whatsappGroupInviteLink = $this->nullableTrim($whatsappGroupInviteLink); return $this; }
    public function isActive(): bool { return $this->active; }
    public function setActive(bool $active): static { $this->active = $active; 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; }
}
