<?php

use App\Filament\Resources\PolicyVersionResource;
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\SettingsCatalogCategory;
use App\Models\SettingsCatalogDefinition;
use App\Models\ManagedEnvironment;
use App\Services\Intune\PolicyNormalizer;
use Carbon\CarbonImmutable;
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');
});

test('settings catalog policy version detail renders the shared normalized settings family', 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' => [],
    ]);

    $tenant = ManagedEnvironment::factory()->create([
        'status' => 'active',
    ]);

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

    $policy = Policy::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'external_id' => 'settings-catalog-policy-1',
        'policy_type' => 'settingsCatalogPolicy',
        'display_name' => 'Settings Catalog Policy',
        'platform' => 'windows',
    ]);

    $version = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'policy_id' => (int) $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.deviceManagementConfigurationPolicy',
            'settings' => [
                [
                    'id' => 's1',
                    'settingInstance' => [
                        '@odata.type' => '#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance',
                        'settingDefinitionId' => 'device_vendor_msft_accountmanagement_userprofilemanagement_deletionpolicy',
                        'choiceSettingValue' => [
                            'value' => 'enabled',
                        ],
                    ],
                ],
            ],
        ],
    ]);

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

    $response->assertSuccessful()->assertSee('Deletion Policy');

    expect($response->getContent())
        ->toContain('data-shared-detail-family="normalized-settings"')
        ->toContain('data-shared-normalized-settings-host="policy_version"');
});
