<?php

declare(strict_types=1);

namespace App\Support\SystemConsole;

use Carbon\CarbonImmutable;

final class SystemConsoleWindow
{
    public const LastHour = '1h';

    public const LastDay = '24h';

    public const LastWeek = '7d';

    private function __construct(
        public readonly string $value,
        private readonly int $minutes,
    ) {}

    public static function fromNullable(?string $window): self
    {
        return match (trim((string) $window)) {
            self::LastHour => new self(self::LastHour, 60),
            self::LastWeek => new self(self::LastWeek, 10080),
            default => new self(self::LastDay, 1440),
        };
    }

    /**
     * @return array<string, string>
     */
    public static function options(): array
    {
        return [
            self::LastHour => 'Last hour',
            self::LastDay => 'Last 24 hours',
            self::LastWeek => 'Last 7 days',
        ];
    }

    /**
     * @return array<int, string>
     */
    public static function allowed(): array
    {
        return array_keys(self::options());
    }

    public function startAt(?CarbonImmutable $now = null): CarbonImmutable
    {
        $now ??= CarbonImmutable::now();

        return $now->subMinutes($this->minutes);
    }
}
