<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Service;

class HealthIndicatorCalculator
{
    private const GREEN_THRESHOLD_DAYS = 7;
    private const YELLOW_THRESHOLD_DAYS = 14;

    public function calculate(\DateTimeInterface $stageEnteredAt, ?\DateTimeInterface $referenceDate = null): string
    {
        $now = $referenceDate ?? new \DateTimeImmutable();
        $daysInStage = (int) $stageEnteredAt->diff($now)->days;

        if ($daysInStage <= self::GREEN_THRESHOLD_DAYS) {
            return 'green';
        }

        if ($daysInStage <= self::YELLOW_THRESHOLD_DAYS) {
            return 'yellow';
        }

        return 'red';
    }

    public function getDaysInStage(\DateTimeInterface $stageEnteredAt, ?\DateTimeInterface $referenceDate = null): int
    {
        $now = $referenceDate ?? new \DateTimeImmutable();

        return (int) $stageEnteredAt->diff($now)->days;
    }
}
