<?php

use App\Models\BackupItem;
use App\Models\BackupSet;
use App\Models\Policy;
use App\Models\ManagedEnvironment;
use App\Services\Graph\GraphClientInterface;
use App\Services\Graph\GraphResponse;
use App\Services\Intune\RestoreService;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('conditional access restores are preview-only and skipped on execution', function () {
    $client = new class implements GraphClientInterface
    {
        public int $applyCalls = 0;

        public function listPolicies(string $policyType, array $options = []): GraphResponse
        {
            return new GraphResponse(true, []);
        }

        public function getPolicy(string $policyType, string $policyId, array $options = []): GraphResponse
        {
            return new GraphResponse(true, ['payload' => []]);
        }

        public function getOrganization(array $options = []): GraphResponse
        {
            return new GraphResponse(true, []);
        }

        public function applyPolicy(string $policyType, string $policyId, array $payload, array $options = []): GraphResponse
        {
            $this->applyCalls++;

            return new GraphResponse(true, []);
        }

        public function getServicePrincipalPermissions(array $options = []): GraphResponse
        {
            return new GraphResponse(true, []);
        }

        public function request(string $method, string $path, array $options = []): GraphResponse
        {
            return new GraphResponse(true, []);
        }
    };

    app()->instance(GraphClientInterface::class, $client);

    $tenant = ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-ca',
        'name' => 'ManagedEnvironment CA',
        'metadata' => [],
    ]);

    ensureDefaultProviderConnection($tenant);

    $policy = Policy::create([
        'managed_environment_id' => $tenant->id,
        'external_id' => 'ca-policy-1',
        'policy_type' => 'conditionalAccessPolicy',
        'display_name' => 'CA Policy',
        'platform' => 'all',
    ]);

    $backupSet = BackupSet::create([
        'managed_environment_id' => $tenant->id,
        'name' => 'CA Backup',
        'status' => 'completed',
        'item_count' => 1,
    ]);

    $backupItem = BackupItem::create([
        'managed_environment_id' => $tenant->id,
        'backup_set_id' => $backupSet->id,
        'policy_id' => $policy->id,
        'policy_identifier' => $policy->external_id,
        'policy_type' => $policy->policy_type,
        'platform' => $policy->platform,
        'payload' => [
            '@odata.type' => '#microsoft.graph.conditionalAccessPolicy',
            'id' => $policy->external_id,
            'displayName' => $policy->display_name,
        ],
    ]);

    $service = app(RestoreService::class);
    $preview = $service->preview($tenant, $backupSet, [$backupItem->id]);

    $previewItem = collect($preview)->first(fn (array $item) => ($item['policy_type'] ?? null) === 'conditionalAccessPolicy');

    expect($previewItem)->not->toBeNull()
        ->and($previewItem['restore_mode'] ?? null)->toBe('preview-only');

    $run = $service->execute(
        tenant: $tenant,
        backupSet: $backupSet,
        selectedItemIds: [$backupItem->id],
        dryRun: false,
        actorEmail: 'tester@example.com',
        actorName: 'Tester',
    );

    expect($run->results['items'] ?? [])->toHaveCount(1);

    $result = $run->results['items'][$backupItem->id] ?? null;
    expect($result)->not->toBeNull();
    expect($result['status'] ?? null)->toBe('skipped');
    expect($result['reason'] ?? null)->toBe('preview_only');

    expect($client->applyCalls)->toBe(0);
});
