<?php

use App\Models\InventoryItem;
use App\Services\Baselines\CurrentStateHashResolver;
use Illuminate\Support\Facades\DB;

it('Baseline resolver remains batch-oriented (bounded query count)', function () {
    [, $tenant] = createUserWithTenant();

    $inventoryItems = InventoryItem::factory()
        ->count(500)
        ->create([
            'managed_environment_id' => (int) $tenant->getKey(),
            'policy_type' => 'deviceConfiguration',
            'meta_jsonb' => [
                'odata_type' => '#microsoft.graph.deviceConfiguration',
                'etag' => 'E',
                'scope_tag_ids' => [],
                'assignment_target_count' => 1,
            ],
            'last_seen_operation_run_id' => null,
            'last_seen_at' => now(),
        ]);

    $subjects = $inventoryItems->map(static fn (InventoryItem $item): array => [
        'policy_type' => (string) $item->policy_type,
        'subject_external_id' => (string) $item->external_id,
    ])->all();

    DB::enableQueryLog();
    DB::flushQueryLog();

    app(CurrentStateHashResolver::class)->resolveForSubjects(
        tenant: $tenant,
        subjects: $subjects,
        since: null,
        latestInventorySyncRunId: null,
    );

    $queryCount = count(DB::getQueryLog());

    expect($queryCount)->toBeLessThanOrEqual(20);
});
