<?php

use App\Models\Policy;
use App\Models\Tenant;
use App\Services\Graph\AssignmentFetcher;
use App\Services\Graph\ScopeTagResolver;
use App\Services\Intune\PolicySnapshotService;
use App\Services\Intune\VersionService;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('persists metadata-only snapshot metadata on captured versions', function () {
    $tenant = Tenant::factory()->create();
    $policy = Policy::factory()->for($tenant)->create([
        'policy_type' => 'mamAppConfiguration',
        'platform' => 'mobile',
        'external_id' => 'A_meta_only',
        'display_name' => 'MAM Config Meta',
    ]);

    $this->mock(PolicySnapshotService::class, function ($mock) {
        $mock->shouldReceive('fetch')
            ->once()
            ->andReturn([
                'payload' => [
                    'id' => 'A_meta_only',
                    'displayName' => 'MAM Config Meta',
                    '@odata.type' => '#microsoft.graph.targetedManagedAppConfiguration',
                ],
                'metadata' => [
                    'source' => 'metadata_only',
                    'original_status' => 500,
                    'original_failure' => 'InternalServerError: upstream',
                ],
                'warnings' => [
                    'Snapshot captured from local metadata only (Graph API returned 500).',
                ],
            ]);
    });

    $this->mock(AssignmentFetcher::class, function ($mock) {
        $mock->shouldReceive('fetch')->never();
    });

    $this->mock(ScopeTagResolver::class, function ($mock) {
        $mock->shouldReceive('resolve')->never();
    });

    $service = app(VersionService::class);

    $version = $service->captureFromGraph(
        tenant: $tenant,
        policy: $policy,
        createdBy: 'tester@example.test',
        includeAssignments: false,
        includeScopeTags: false,
    );

    expect($version->metadata['source'])->toBe('metadata_only');
    expect($version->metadata['original_status'])->toBe(500);
    expect($version->metadata['original_failure'])->toContain('InternalServerError');
    expect($version->metadata['capture_source'])->toBe('version_capture');
    expect($version->metadata['warnings'])->toBeArray();
    expect($version->metadata['warnings'][0])->toContain('metadata only');
});
