<?php

declare(strict_types=1);

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: RecuPaiementRepository::class)]
#[ORM\Table(name: 'recu_paiement')]
#[ORM\UniqueConstraint(name: 'UNIQ_RECU_PAIEMENT_NUMERO', columns: ['numero'])]
#[ORM\HasLifecycleCallbacks]
class RecuPaiement
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

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

    #[ORM\OneToOne]
    #[ORM\JoinColumn(nullable: false, unique: true, onDelete: 'CASCADE')]
    private ?Paiement $paiement = null;

    #[ORM\Column(length: 60, unique: true)]
    private ?string $numero = null;

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

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

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

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private ?\DateTimeImmutable $createdAt = 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 getPaiement(): ?Paiement { return $this->paiement; }
    public function setPaiement(?Paiement $paiement): static { $this->paiement = $paiement; return $this; }
    public function getNumero(): ?string { return $this->numero; }
    public function setNumero(string $numero): static { $this->numero = trim($numero); return $this; }
    public function getPdfPath(): ?string { return $this->pdfPath; }
    public function setPdfPath(?string $pdfPath): static { $this->pdfPath = $this->nullableTrim($pdfPath); return $this; }
    public function getQrPayload(): ?string { return $this->qrPayload; }
    public function setQrPayload(?string $qrPayload): static { $this->qrPayload = $this->nullableTrim($qrPayload); return $this; }
    public function getGeneratedAt(): ?\DateTimeImmutable { return $this->generatedAt; }
    public function setGeneratedAt(\DateTimeImmutable $generatedAt): static { $this->generatedAt = $generatedAt; return $this; }
    public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; }

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