<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Index(columns: ['advert_id'], name: 'idx_payment_reminder_advert')]
#[ORM\Index(columns: ['step'], name: 'idx_payment_reminder_step')]
class PaymentReminder
{
    // Etapes de relance (jour apres envoi de l'avis)
    public const STEP_REMINDER_15 = 'reminder_15';           // J+15 : Rappel
    public const STEP_WARNING_10 = 'warning_10';              // J+10 : Rappel + avertissement
    public const STEP_SUSPENSION_WARNING_5 = 'suspension_5';  // J+5 : Avertissement suspension services
    public const STEP_FINAL_REMINDER_3 = 'final_reminder_3';  // J+3 : Ultime rappel
    public const STEP_SUSPENSION_1 = 'suspension_1';           // J+1 : Suspension des services
    public const STEP_FORMAL_NOTICE = 'formal_notice';         // J-1 (apres echeance) : Mise en demeure
    public const STEP_TERMINATION_WARNING = 'termination_15';  // J+15 post-echeance : Avertissement resiliation + suppression donnees
    public const STEP_TERMINATION = 'termination_30';          // J+30 post-echeance : Resiliation + suppression + recouvrement legal

    public const STEPS_CONFIG = [
        self::STEP_REMINDER_15 => ['days' => 15, 'label' => 'Rappel de paiement', 'severity' => 'info'],
        self::STEP_WARNING_10 => ['days' => 20, 'label' => 'Rappel + avertissement', 'severity' => 'warning'],
        self::STEP_SUSPENSION_WARNING_5 => ['days' => 25, 'label' => 'Avertissement suspension services', 'severity' => 'danger'],
        self::STEP_FINAL_REMINDER_3 => ['days' => 27, 'label' => 'Ultime rappel', 'severity' => 'danger'],
        self::STEP_SUSPENSION_1 => ['days' => 29, 'label' => 'Suspension des services', 'severity' => 'critical'],
        self::STEP_FORMAL_NOTICE => ['days' => 31, 'label' => 'Mise en demeure', 'severity' => 'critical'],
        self::STEP_TERMINATION_WARNING => ['days' => 45, 'label' => 'Avertissement resiliation + suppression donnees', 'severity' => 'critical'],
        self::STEP_TERMINATION => ['days' => 60, 'label' => 'Resiliation contrat + recouvrement legal', 'severity' => 'critical'],
    ];

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

    #[ORM\ManyToOne(targetEntity: Advert::class)]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private Advert $advert;

    #[ORM\Column(length: 30)]
    private string $step;

    #[ORM\Column]
    private \DateTimeImmutable $sentAt;

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $details = null;

    public function __construct(Advert $advert, string $step, ?string $details = null)
    {
        $this->advert = $advert;
        $this->step = $step;
        $this->details = $details;
        $this->sentAt = new \DateTimeImmutable();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getAdvert(): Advert
    {
        return $this->advert;
    }

    public function getStep(): string
    {
        return $this->step;
    }

    public function getSentAt(): \DateTimeImmutable
    {
        return $this->sentAt;
    }

    public function getDetails(): ?string
    {
        return $this->details;
    }

    public function getStepLabel(): string
    {
        return self::STEPS_CONFIG[$this->step]['label'] ?? $this->step;
    }

    public function getSeverity(): string
    {
        return self::STEPS_CONFIG[$this->step]['severity'] ?? 'info';
    }
}
