<?php

use App\Filament\Resources\BackupSetResource;
use App\Models\BackupSet;
use App\Models\ManagedEnvironment;

test('backup sets index renders without touching graph', function () {
    $tenant = ManagedEnvironment::factory()->create();
    $otherTenant = ManagedEnvironment::factory()->create();

    [$user] = createUserWithTenant($tenant);
    $user->tenants()->syncWithoutDetaching([
        $otherTenant->getKey() => ['role' => 'owner'],
    ]);

    $visibleSet = BackupSet::factory()->create([
        'managed_environment_id' => $tenant->getKey(),
        'name' => 'visible-backup-set',
    ]);

    $hiddenSet = BackupSet::factory()->create([
        'managed_environment_id' => $otherTenant->getKey(),
        'name' => 'hidden-backup-set',
    ]);

    bindFailHardGraphClient();

    $response = $this->actingAs($user)
        ->get(BackupSetResource::getUrl('index', tenant: $tenant))
        ->assertOk()
        ->assertSee('Created by');

    $content = (string) $response->getContent();
    preg_match('/<tbody[^>]*>(.*?)<\\/tbody>/is', $content, $tbodyMatch);
    $tableCellText = html_entity_decode(strip_tags((string) ($tbodyMatch[1] ?? '')));

    expect($tableCellText)
        ->toContain($visibleSet->name)
        ->not->toContain($hiddenSet->name);
});
