<?php

use App\Services\Intune\PolicyNormalizer;

beforeEach(function () {
    $this->normalizer = app(PolicyNormalizer::class);
});

it('normalizes oma uri settings', function () {
    $snapshot = [
        '@odata.type' => '#microsoft.graph.windows10CustomConfiguration',
        'omaSettings' => [
            [
                'displayName' => 'Setting A',
                'omaUri' => './Vendor/MSFT/SettingA',
                'value' => 'Enabled',
            ],
        ],
    ];

    $result = $this->normalizer->normalize($snapshot, 'deviceConfiguration', 'windows');

    expect($result['status'])->toBe('success');
    expect($result['warnings'])->toBe([]);
    expect($result['settings'][0]['type'])->toBe('table');
    expect($result['settings'][0]['rows'][0]['path'])->toBe('./Vendor/MSFT/SettingA');
    expect($result['settings'][0]['rows'][0]['value'])->toBe('Enabled');
});

it('normalizes settings catalog structures', function () {
    $snapshot = [
        'settings' => [
            [
                'displayName' => 'Enable feature',
                'value' => ['value' => 'on'],
            ],
        ],
    ];

    $result = $this->normalizer->normalize($snapshot, 'deviceConfiguration', 'windows');

    expect($result['settings'][0]['type'])->toBe('keyValue');
    expect($result['settings'][0]['entries'][0]['key'])->toBe('Enable feature');
    expect($result['settings'][0]['entries'][0]['value'])->toContain('on');
});

it('adds warning for malformed snapshots', function () {
    $snapshot = ['only', 'values'];

    $result = $this->normalizer->normalize($snapshot, 'deviceConfiguration', 'windows');

    expect($result['status'])->toBe('warning');
    expect($result['warnings'])->toContain('This snapshot may be incomplete or malformed');
});

it('detects @odata.type mismatch', function () {
    $snapshot = [
        '@odata.type' => '#microsoft.graph.targetedManagedAppProtection',
        'displayName' => 'Policy',
    ];

    $result = $this->normalizer->normalize($snapshot, 'deviceConfiguration', 'windows');

    expect(collect($result['warnings'])->join(' '))->toContain('@odata.type mismatch');
});

it('normalizes enrollment platform restriction payload', function () {
    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceEnrollmentPlatformRestrictionConfiguration',
        'deviceEnrollmentConfigurationType' => 'deviceEnrollmentPlatformRestrictionConfiguration',
        'displayName' => 'DeviceTypeRestriction',
        'version' => 2,
        'platformRestriction' => [
            'platformBlocked' => false,
            'personalDeviceEnrollmentBlocked' => true,
        ],
    ];

    $result = $this->normalizer->normalize($snapshot, 'deviceEnrollmentPlatformRestrictionsConfiguration', 'all');

    $block = collect($result['settings'])->firstWhere('title', 'Platform restrictions (enrollment)');
    expect($block)->not->toBeNull();

    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Platform: Platform blocked')['value'] ?? null)->toBe('Disabled');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Platform: Personal device enrollment blocked')['value'] ?? null)->toBe('Enabled');

    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Platform: OS minimum version')['value'] ?? null)->toBe('None');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Platform: OS maximum version')['value'] ?? null)->toBe('None');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Platform: Blocked manufacturers')['value'] ?? null)->toBe(['None']);
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Platform: Blocked SKUs')['value'] ?? null)->toBe(['None']);
});

it('normalizes Autopilot deployment profile key fields', function () {
    $snapshot = [
        '@odata.type' => '#microsoft.graph.azureADWindowsAutopilotDeploymentProfile',
        'displayName' => 'Autopilot Profile A',
        'description' => 'Used for standard devices',
        'deviceNameTemplate' => 'DEV-%SERIAL%',
        'deploymentMode' => 'singleUser',
        'deviceType' => 'windowsPc',
        'enableWhiteGlove' => true,
        'outOfBoxExperienceSettings' => [
            'hideEULA' => true,
            'userType' => 'standard',
        ],
    ];

    $result = $this->normalizer->normalize($snapshot, 'windowsAutopilotDeploymentProfile', 'windows');

    expect($result['status'])->toBe('ok');
    expect($result['warnings'])->toBe([]);

    $general = collect($result['settings'])->firstWhere('title', 'General');
    expect($general)->not->toBeNull();
    expect(collect($general['entries'] ?? [])->firstWhere('key', 'Type')['value'] ?? null)->toBe('windowsAutopilotDeploymentProfile');
    expect(collect($general['entries'] ?? [])->firstWhere('key', 'Display name')['value'] ?? null)->toBe('Autopilot Profile A');

    $block = collect($result['settings'])->firstWhere('title', 'Autopilot profile');
    expect($block)->not->toBeNull();
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Device name template')['value'] ?? null)->toBe('DEV-%SERIAL%');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Pre-provisioning (White Glove)')['value'] ?? null)->toBe('Enabled');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'OOBE: Hide EULA')['value'] ?? null)->toBe('Enabled');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'OOBE: User type')['value'] ?? null)->toBe('standard');
});

it('normalizes Enrollment Status Page key fields', function () {
    $snapshot = [
        '@odata.type' => '#microsoft.graph.windowsEnrollmentStatusPageConfiguration',
        'displayName' => 'ESP A',
        'priority' => 1,
        'showInstallationProgress' => true,
        'blockDeviceSetupRetryByUser' => false,
        'installProgressTimeoutInMinutes' => 60,
        'selectedMobileAppIds' => ['app-1', 'app-2'],
    ];

    $result = $this->normalizer->normalize($snapshot, 'windowsEnrollmentStatusPage', 'windows');

    expect($result['status'])->toBe('ok');
    expect($result['warnings'])->toBe([]);

    $general = collect($result['settings'])->firstWhere('title', 'General');
    expect($general)->not->toBeNull();
    expect(collect($general['entries'] ?? [])->firstWhere('key', 'Type')['value'] ?? null)->toBe('windowsEnrollmentStatusPage');
    expect(collect($general['entries'] ?? [])->firstWhere('key', 'Display name')['value'] ?? null)->toBe('ESP A');

    $block = collect($result['settings'])->firstWhere('title', 'Enrollment Status Page (ESP)');
    expect($block)->not->toBeNull();
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Priority')['value'] ?? null)->toBe(1);
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Show installation progress')['value'] ?? null)->toBe('Enabled');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Block retry by user')['value'] ?? null)->toBe('Disabled');
    expect(collect($block['entries'] ?? [])->firstWhere('key', 'Selected mobile app IDs')['value'] ?? null)->toBe(['app-1', 'app-2']);
});
