<?php

namespace App\Enums;

use Illuminate\Support\Carbon;

enum PriceReliability: string
{
    case Reliable = 'reliable';
    case Stale = 'stale';
    case Outdated = 'outdated';

    public static function fromUpdatedAt(?Carbon $updatedAt): self
    {
        if ($updatedAt === null) {
            return self::Outdated;
        }

        $hours = $updatedAt->diffInHours(now());

        return match (true) {
            $hours <= 72 => self::Reliable,
            $hours <= 168 => self::Stale,
            default => self::Outdated,
        };
    }

    public function weight(): int
    {
        return match ($this) {
            self::Reliable => 0,
            self::Stale => 1,
            self::Outdated => 2,
        };
    }

    public function label(): string
    {
        return match ($this) {
            self::Reliable => 'Reliable',
            self::Stale => 'Older price — verify before driving',
            self::Outdated => 'Outdated — may be inaccurate',
        };
    }
}
