<?php

use App\Models\BackupItem;
use App\Models\Policy;
use App\Models\Tenant;
use App\Services\Intune\BackupService;
use App\Services\Intune\FoundationSnapshotService;
use App\Services\Intune\PolicySnapshotService;
use Mockery\MockInterface;

beforeEach(function () {
    $this->tenant = Tenant::factory()->create(['status' => 'active']);

    $this->policy = Policy::factory()->create([
        'tenant_id' => $this->tenant->id,
        'policy_type' => 'deviceConfiguration',
        'platform' => 'windows',
        'display_name' => 'Test Policy',
    ]);

    config()->set('tenantpilot.foundation_types', [
        [
            'type' => 'assignmentFilter',
            'label' => 'Assignment Filter',
            'category' => 'Foundations',
            'platform' => 'all',
            'endpoint' => 'deviceManagement/assignmentFilters',
            'backup' => 'full',
            'restore' => 'enabled',
            'risk' => 'low',
        ],
    ]);

    $this->mock(PolicySnapshotService::class, function (MockInterface $mock) {
        $mock->shouldReceive('fetch')
            ->andReturn([
                'payload' => [
                    '@odata.type' => '#microsoft.graph.deviceConfiguration',
                    'id' => 'policy-1',
                    'displayName' => 'Test Policy',
                ],
                'metadata' => [],
                'warnings' => [],
            ]);
    });

    $this->mock(FoundationSnapshotService::class, function (MockInterface $mock) {
        $mock->shouldReceive('fetchAll')
            ->once()
            ->andReturn([
                'items' => [
                    [
                        'source_id' => 'filter-1',
                        'display_name' => 'Filter One',
                        'payload' => [
                            'id' => 'filter-1',
                            'displayName' => 'Filter One',
                        ],
                        'metadata' => [
                            'displayName' => 'Filter One',
                            'kind' => 'assignmentFilter',
                            'graph' => [
                                'resource' => 'deviceManagement/assignmentFilters',
                                'apiVersion' => 'beta',
                            ],
                        ],
                    ],
                ],
                'failures' => [],
            ]);
    });
});

it('creates foundation backup items when requested', function () {
    $service = app(BackupService::class);

    $backupSet = $service->createBackupSet(
        tenant: $this->tenant,
        policyIds: [$this->policy->id],
        name: 'Foundation Backup',
        includeFoundations: true,
    );

    expect($backupSet->items)->toHaveCount(2);

    $foundationItem = BackupItem::query()
        ->where('backup_set_id', $backupSet->id)
        ->where('policy_type', 'assignmentFilter')
        ->first();

    expect($foundationItem)->not->toBeNull();
    expect($foundationItem->policy_id)->toBeNull();
    expect($foundationItem->policy_identifier)->toBe('filter-1');
    expect($foundationItem->metadata['displayName'])->toBe('Filter One');
});
