<?php

use App\Filament\Resources\PolicyVersionResource;
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Services\Intune\VersionService;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('policy versions render with timeline data', function () {
    $tenant = ManagedEnvironment::factory()->create();

    $policy = Policy::create([
        'managed_environment_id' => $tenant->id,
        'external_id' => 'policy-1',
        'policy_type' => 'deviceConfiguration',
        'display_name' => 'Policy A',
        'platform' => 'windows',
    ]);

    $service = app(VersionService::class);
    $service->captureVersion($policy, ['value' => 1], 'tester');
    $service->captureVersion($policy, ['value' => 2], 'tester');

    $user = User::factory()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, user: $user, role: 'owner');

    $this->actingAs($user)
        ->get(PolicyVersionResource::getUrl('index', tenant: $tenant))
        ->assertOk()
        ->assertSee('Policy A')
        ->assertSee('Backup quality')
        ->assertSee('Full payload')
        ->assertSee((string) PolicyVersion::max('version_number'));
});

test('policy version detail renders readable normalized RBAC assignment content', function () {
    $tenant = ManagedEnvironment::factory()->create();

    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner');

    $policy = Policy::factory()->create([
        'managed_environment_id' => $tenant->id,
        'external_id' => 'rbac-assign-1',
        'policy_type' => 'intuneRoleAssignment',
        'display_name' => 'Current assignment name',
        'platform' => 'all',
        'last_synced_at' => null,
    ]);

    $version = PolicyVersion::factory()->create([
        'managed_environment_id' => $tenant->id,
        'policy_id' => $policy->id,
        'policy_type' => 'intuneRoleAssignment',
        'platform' => 'all',
        'snapshot' => [
            '@odata.type' => '#microsoft.graph.deviceAndAppManagementRoleAssignment',
            'displayName' => 'Helpdesk Assignment',
            'description' => 'Delegated access for helpdesk operators',
            'scopeType' => 'allDevicesAssignment',
            'members' => [
                ['displayName' => 'Helpdesk Group', 'id' => 'group-1'],
                'group-2',
            ],
            'scopeMembers' => ['scope-group-1'],
            'resourceScopes' => ['/', '/deviceManagement/managedDevices'],
            'roleDefinition' => [
                'id' => 'role-1',
                'displayName' => 'Policy and Profile Manager',
            ],
        ],
    ]);

    $tenant->makeCurrent();

    $response = $this->actingAs($user)
        ->get(PolicyVersionResource::getUrl('view', ['record' => $version, 'tab' => 'normalized-settings'], tenant: $tenant));

    $response->assertOk();
    $response->assertSee('Backup quality');
    $response->assertSee('Helpdesk Assignment');
    $response->assertSee('Role assignment');
    $response->assertSee('Policy and Profile Manager (role-1)');
    $response->assertSee('Helpdesk Group (group-1)');
    $response->assertSee('group-2');
    $response->assertSee('scope-group-1');
    $response->assertSee('/deviceManagement/managedDevices');
});

test('policy version detail returns 404 for non-members on RBAC versions', function () {
    $tenant = ManagedEnvironment::factory()->create();

    [$owner, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner');

    $policy = Policy::factory()->create([
        'managed_environment_id' => $tenant->id,
        'external_id' => 'rbac-assign-404',
        'policy_type' => 'intuneRoleAssignment',
        'display_name' => 'Hidden assignment',
        'platform' => 'all',
        'last_synced_at' => null,
    ]);

    $version = PolicyVersion::factory()->create([
        'managed_environment_id' => $tenant->id,
        'policy_id' => $policy->id,
        'policy_type' => 'intuneRoleAssignment',
        'platform' => 'all',
        'snapshot' => [
            'displayName' => 'Hidden assignment',
        ],
    ]);

    $outsider = User::factory()->create();

    $this->actingAs($outsider)
        ->get(PolicyVersionResource::getUrl('view', ['record' => $version], tenant: $tenant))
        ->assertNotFound();
});
