<?php

declare(strict_types=1);

use App\Models\EntraGroup;
use App\Models\Policy;
use App\Models\PolicyVersion;

it('renders cached group targets as resolved compact references on policy versions', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $groupId = '11111111-2222-3333-4444-555555555555';

    EntraGroup::factory()->for($tenant)->create([
        'entra_id' => strtolower($groupId),
        'display_name' => 'Group One',
        'security_enabled' => true,
    ]);

    $policy = Policy::factory()->for($tenant)->create();

    $version = PolicyVersion::factory()->for($tenant)->create([
        'policy_id' => (int) $policy->getKey(),
        'assignments' => [
            [
                'intent' => 'apply',
                'target' => [
                    '@odata.type' => '#microsoft.graph.groupAssignmentTarget',
                    'groupId' => $groupId,
                ],
            ],
            [
                'intent' => 'apply',
                'target' => [
                    '@odata.type' => '#microsoft.graph.allDevicesAssignmentTarget',
                ],
            ],
        ],
    ]);

    $this->get(\App\Filament\Resources\PolicyVersionResource::getUrl('view', ['record' => $version], tenant: $tenant))
        ->assertOk()
        ->assertSee('Group One')
        ->assertDontSee('Resolved')
        ->assertSee('All devices');
});

it('renders uncached group targets as external-only references when source context exists', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $policy = Policy::factory()->for($tenant)->create();

    $version = PolicyVersion::factory()->for($tenant)->create([
        'policy_id' => (int) $policy->getKey(),
        'assignments' => [
            [
                'intent' => 'apply',
                'target' => [
                    '@odata.type' => '#microsoft.graph.groupAssignmentTarget',
                    'groupId' => 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
                    'group_display_name' => 'Source-only group',
                ],
            ],
        ],
    ]);

    $this->get(\App\Filament\Resources\PolicyVersionResource::getUrl('view', ['record' => $version], tenant: $tenant))
        ->assertOk()
        ->assertSee('Source-only group')
        ->assertSee('Provider-only');
});
