<?php

declare(strict_types=1);

use App\Services\Intune\ManagedEnvironmentRequiredPermissionsViewModelBuilder;
use Carbon\CarbonImmutable;

it('marks freshness as stale when last refreshed is missing', function (): void {
    $freshness = ManagedEnvironmentRequiredPermissionsViewModelBuilder::deriveFreshness(
        null,
        CarbonImmutable::parse('2026-02-08 12:00:00'),
    );

    expect($freshness['last_refreshed_at'])->toBeNull()
        ->and($freshness['is_stale'])->toBeTrue();
});

it('marks freshness as stale when last refreshed is older than 30 days', function (): void {
    $freshness = ManagedEnvironmentRequiredPermissionsViewModelBuilder::deriveFreshness(
        CarbonImmutable::parse('2026-01-08 11:59:59'),
        CarbonImmutable::parse('2026-02-08 12:00:00'),
    );

    expect($freshness['is_stale'])->toBeTrue();
});

it('marks freshness as not stale when last refreshed is exactly 30 days old', function (): void {
    $freshness = ManagedEnvironmentRequiredPermissionsViewModelBuilder::deriveFreshness(
        CarbonImmutable::parse('2026-01-09 12:00:00'),
        CarbonImmutable::parse('2026-02-08 12:00:00'),
    );

    expect($freshness['is_stale'])->toBeFalse();
});

it('keeps the thirty day freshness boundary exact to the second', function (): void {
    $reference = CarbonImmutable::parse('2026-02-08 12:00:00');

    $insideWindow = ManagedEnvironmentRequiredPermissionsViewModelBuilder::deriveFreshness(
        CarbonImmutable::parse('2026-01-09 12:00:01'),
        $reference,
    );
    $outsideWindow = ManagedEnvironmentRequiredPermissionsViewModelBuilder::deriveFreshness(
        CarbonImmutable::parse('2026-01-09 11:59:59'),
        $reference,
    );

    expect($insideWindow['is_stale'])->toBeFalse()
        ->and($outsideWindow['is_stale'])->toBeTrue();
});
