<?php

use App\Filament\Resources\PolicyResource;
use App\Models\Policy;
use App\Models\PolicyVersion;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

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

    $this->tenant = $tenant;
    $this->user = $user;
});

test('policy detail renders normalized settings for Autopilot profiles', function () {
    $policy = Policy::factory()->create([
        'managed_environment_id' => $this->tenant->getKey(),
        'external_id' => 'autopilot-1',
        'policy_type' => 'windowsAutopilotDeploymentProfile',
        'display_name' => 'Autopilot Profile A',
        'platform' => 'windows',
    ]);

    PolicyVersion::factory()->create([
        'managed_environment_id' => $this->tenant->getKey(),
        'policy_id' => $policy->getKey(),
        'version_number' => 1,
        'policy_type' => $policy->policy_type,
        'platform' => $policy->platform,
        'created_by' => 'tester@example.com',
        'captured_at' => CarbonImmutable::now(),
        'snapshot' => [
            '@odata.type' => '#microsoft.graph.azureADWindowsAutopilotDeploymentProfile',
            'displayName' => 'Autopilot Profile A',
            'deviceNameTemplate' => 'DEV-%SERIAL%',
            'enableWhiteGlove' => true,
            'outOfBoxExperienceSettings' => [
                'hideEULA' => true,
                'userType' => 'standard',
            ],
        ],
    ]);

    $response = $this->actingAs($this->user)
        ->get(PolicyResource::getUrl('view', ['record' => $policy], tenant: $this->tenant));

    $response->assertOk();
    $response->assertSee('Settings');
    $response->assertSee('Autopilot profile');
    $response->assertSee('Device name template');
    $response->assertSee('DEV-%SERIAL%');
    $response->assertSee('Pre-provisioning (White Glove)');
    $response->assertSee('Enabled');
    $response->assertSee('OOBE: Hide EULA');
    $response->assertSee('OOBE: User type');
});

test('policy detail renders normalized settings for Enrollment Status Page (ESP)', function () {
    $policy = Policy::factory()->create([
        'managed_environment_id' => $this->tenant->getKey(),
        'external_id' => 'esp-1',
        'policy_type' => 'windowsEnrollmentStatusPage',
        'display_name' => 'ESP A',
        'platform' => 'windows',
    ]);

    PolicyVersion::factory()->create([
        'managed_environment_id' => $this->tenant->getKey(),
        'policy_id' => $policy->getKey(),
        'version_number' => 1,
        'policy_type' => $policy->policy_type,
        'platform' => $policy->platform,
        'created_by' => 'tester@example.com',
        'captured_at' => CarbonImmutable::now(),
        'snapshot' => [
            '@odata.type' => '#microsoft.graph.windowsEnrollmentStatusPageConfiguration',
            'displayName' => 'ESP A',
            'priority' => 1,
            'showInstallationProgress' => true,
            'installProgressTimeoutInMinutes' => 60,
            'selectedMobileAppIds' => ['app-1', 'app-2'],
        ],
    ]);

    $response = $this->actingAs($this->user)
        ->get(PolicyResource::getUrl('view', ['record' => $policy], tenant: $this->tenant));

    $response->assertOk();
    $response->assertSee('Settings');
    $response->assertSee('Enrollment Status Page (ESP)');
    $response->assertSee('Priority');
    $response->assertSee('1');
    $response->assertSee('Show installation progress');
    $response->assertSee('Enabled');
    $response->assertSee('Selected mobile app IDs');
    $response->assertSee('app-1');
    $response->assertSee('app-2');
});

test('policy detail renders normalized settings for platform restrictions (enrollment)', function () {
    $policy = Policy::factory()->create([
        'managed_environment_id' => $this->tenant->getKey(),
        'external_id' => 'enroll-restrict-1',
        'policy_type' => 'deviceEnrollmentPlatformRestrictionsConfiguration',
        'display_name' => 'Restriction A',
        'platform' => 'all',
    ]);

    PolicyVersion::factory()->create([
        'managed_environment_id' => $this->tenant->getKey(),
        'policy_id' => $policy->getKey(),
        'version_number' => 1,
        'policy_type' => $policy->policy_type,
        'platform' => $policy->platform,
        'created_by' => 'tester@example.com',
        'captured_at' => CarbonImmutable::now(),
        'snapshot' => [
            '@odata.type' => '#microsoft.graph.deviceEnrollmentPlatformRestrictionConfiguration',
            'displayName' => 'Restriction A',
            'deviceEnrollmentConfigurationType' => 'deviceEnrollmentPlatformRestrictionConfiguration',
            'platformRestriction' => [
                'platformBlocked' => false,
                'personalDeviceEnrollmentBlocked' => true,
                'blockedSkus' => ['sku-1'],
            ],
        ],
    ]);

    $response = $this->actingAs($this->user)
        ->get(PolicyResource::getUrl('view', ['record' => $policy], tenant: $this->tenant));

    $response->assertOk();
    $response->assertSee('Settings');
    $response->assertSee('Platform restrictions (enrollment)');
    $response->assertSee('Platform: Personal device enrollment blocked');
    $response->assertSee('Enabled');
    $response->assertSee('Platform: Blocked SKUs');
    $response->assertSee('sku-1');
});
