<?php

use App\Models\SettingsCatalogCategory;
use App\Models\SettingsCatalogDefinition;
use App\Services\Intune\PolicyNormalizer;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('settings catalog normalized diff keys use definition display names', function () {
    SettingsCatalogCategory::create([
        'category_id' => 'cat-1',
        'display_name' => 'Account Management',
        'description' => null,
    ]);

    SettingsCatalogDefinition::create([
        'definition_id' => 'device_vendor_msft_accountmanagement_userprofilemanagement_deletionpolicy',
        'display_name' => 'Deletion Policy',
        'description' => null,
        'help_text' => null,
        'category_id' => 'cat-1',
        'ux_behavior' => null,
        'raw' => [],
    ]);

    $flat = app(PolicyNormalizer::class)->flattenForDiff(
        snapshot: [
            '@odata.type' => '#microsoft.graph.deviceManagementConfigurationPolicy',
            'id' => 'scp-policy-1',
            'name' => 'Settings Catalog Policy',
            'platforms' => 'windows10',
            'technologies' => 'mdm',
            'settings' => [
                [
                    'id' => 's1',
                    'settingInstance' => [
                        '@odata.type' => '#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance',
                        'settingDefinitionId' => 'device_vendor_msft_accountmanagement_userprofilemanagement_deletionpolicy',
                        'choiceSettingValue' => [
                            'value' => 'enabled',
                        ],
                    ],
                ],
            ],
        ],
        policyType: 'settingsCatalogPolicy',
        platform: 'windows',
    );

    $keys = array_keys($flat);

    expect($keys)->toContain('Settings > Account Management > Deletion Policy');
    expect(implode("\n", $keys))->not->toContain('device_vendor_msft_accountmanagement_userprofilemanagement_deletionpolicy');
});
