<?php

declare(strict_types=1);

namespace App\Enums;

use App\Models\AssetWarranty;

/**
 * The state of one coverage period, purely as a function of today's date
 * against `end_date`.
 *
 * Unlike every other enum in this app, nothing persists this — it cannot: a
 * warranty moves from Active to ExpiringSoon to Expired without a single
 * write happening, just because time passes. A non-backed enum still gets
 * the full `<x-status-badge>` treatment via {@see HasBadge}, computed fresh
 * every time it's asked for.
 */
enum WarrantyStatus implements HasBadge
{
    case Active;
    case ExpiringSoon;
    case Expired;

    /**
     * Anything ending within this many days counts as "expiring soon" for a
     * single badge. The finer 30/60/90 buckets on the warranty dashboard are
     * a separate, coarser concern — see WarrantyService::UI_BUCKETS.
     */
    private const SOON_THRESHOLD_DAYS = 90;

    public static function for(AssetWarranty $warranty): self
    {
        if ($warranty->isExpired()) {
            return self::Expired;
        }

        if ($warranty->daysRemaining() <= self::SOON_THRESHOLD_DAYS) {
            return self::ExpiringSoon;
        }

        return self::Active;
    }

    public function label(): string
    {
        return match ($this) {
            self::Active => 'Active',
            self::ExpiringSoon => 'Expiring Soon',
            self::Expired => 'Expired',
        };
    }

    public function color(): string
    {
        return match ($this) {
            self::Active => 'success',
            self::ExpiringSoon => 'warning',
            self::Expired => 'danger',
        };
    }

    public function icon(): string
    {
        return match ($this) {
            self::Active => 'shield-check',
            self::ExpiringSoon => 'hourglass-split',
            self::Expired => 'shield-x',
        };
    }
}
