<?php

use App\Services\Intune\PolicyNormalizer;

it('normalizes deviceManagementScript into readable settings', function () {
    $normalizer = app(PolicyNormalizer::class);

    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceManagementScript',
        'displayName' => 'My PS script',
        'description' => 'Does a thing',
        'scriptContent' => str_repeat('A', 10),
        'runFrequency' => 'weekly',
    ];

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

    expect($result['status'])->toBe('ok');
    expect($result['settings'])->toBeArray()->not->toBeEmpty();
    expect($result['settings'][0]['type'])->toBe('keyValue');
    expect(collect($result['settings'][0]['entries'])->pluck('key')->all())->toContain('Display name');
});

it('normalizes deviceShellScript into readable settings', function () {
    $normalizer = app(PolicyNormalizer::class);

    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceShellScript',
        'displayName' => 'My macOS shell script',
        'scriptContent' => str_repeat('B', 5),
    ];

    $result = $normalizer->normalize($snapshot, 'deviceShellScript', 'macOS');

    expect($result['status'])->toBe('ok');
    expect($result['settings'])->toBeArray()->not->toBeEmpty();
    expect($result['settings'][0]['type'])->toBe('keyValue');
});

it('normalizes deviceHealthScript into readable settings', function () {
    $normalizer = app(PolicyNormalizer::class);

    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceHealthScript',
        'displayName' => 'My remediation',
        'detectionScriptContent' => str_repeat('C', 3),
        'remediationScriptContent' => str_repeat('D', 4),
    ];

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

    expect($result['status'])->toBe('ok');
    expect($result['settings'])->toBeArray()->not->toBeEmpty();
    expect($result['settings'][0]['type'])->toBe('keyValue');
});

it('summarizes script content by default', function () {
    config([
        'tenantpilot.display.show_script_content' => false,
    ]);

    $normalizer = app(PolicyNormalizer::class);

    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceManagementScript',
        'displayName' => 'My PS script',
        'scriptContent' => 'ABC',
    ];

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

    $entries = collect($result['settings'][0]['entries']);

    expect($entries->firstWhere('key', 'scriptContent')['value'])->toBe('[content: 3 chars]');
});

it('shows script content when enabled', function () {
    config([
        'tenantpilot.display.show_script_content' => true,
        'tenantpilot.display.max_script_content_chars' => 100,
    ]);

    $normalizer = app(PolicyNormalizer::class);

    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceManagementScript',
        'displayName' => 'My PS script',
        'scriptContent' => "line1\nline2",
    ];

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

    $entries = collect($result['settings'][0]['entries']);

    expect($entries->firstWhere('key', 'scriptContent')['value'])->toBe("line1\nline2");
});

it('decodes scriptContentBase64 when enabled and scriptContent is missing', function () {
    config([
        'tenantpilot.display.show_script_content' => true,
        'tenantpilot.display.max_script_content_chars' => 50,
    ]);

    $normalizer = app(PolicyNormalizer::class);

    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceShellScript',
        'displayName' => 'My macOS shell script',
        'scriptContentBase64' => base64_encode('echo hello'),
    ];

    $result = $normalizer->normalize($snapshot, 'deviceShellScript', 'macOS');

    $entries = collect($result['settings'][0]['entries']);

    expect($entries->firstWhere('key', 'scriptContent')['value'])->toBe('echo hello');
});

it('decodes base64-looking scriptContent when enabled', function () {
    config([
        'tenantpilot.display.show_script_content' => true,
        'tenantpilot.display.max_script_content_chars' => 5000,
    ]);

    $normalizer = app(PolicyNormalizer::class);

    $plain = "# hello\nWrite-Host \"hi\"";
    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceManagementScript',
        'displayName' => 'My PS script',
        'scriptContent' => base64_encode($plain),
    ];

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

    $entries = collect($result['settings'][0]['entries']);

    expect($entries->firstWhere('key', 'scriptContent')['value'])->toBe($plain);
});

it('decodes base64-looking detection/remediation script content when enabled', function () {
    config([
        'tenantpilot.display.show_script_content' => true,
        'tenantpilot.display.max_script_content_chars' => 5000,
    ]);

    $normalizer = app(PolicyNormalizer::class);

    $detection = "# detection\nWrite-Host \"detect\"";
    $remediation = "# remediation\nWrite-Host \"fix\"";

    $snapshot = [
        '@odata.type' => '#microsoft.graph.deviceHealthScript',
        'displayName' => 'My remediation',
        'detectionScriptContent' => base64_encode($detection),
        'remediationScriptContent' => base64_encode($remediation),
    ];

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

    $entries = collect($result['settings'][0]['entries']);

    expect($entries->firstWhere('key', 'detectionScriptContent')['value'])->toBe($detection);
    expect($entries->firstWhere('key', 'remediationScriptContent')['value'])->toBe($remediation);
});
