<?php

use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentPermission;
use App\Services\Graph\GraphClientInterface;
use App\Services\Graph\GraphResponse;
use App\Services\Intune\ManagedEnvironmentPermissionService;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);
function requiredPermissions(): array
{
    $service = app(ManagedEnvironmentPermissionService::class);
    $required = $service->getRequiredPermissions();

    if (empty($required)) {
        test()->markTestSkipped('No required permissions configured.');
    }

    return $required;
}

it('returns ok when all permissions exist', function () {
    // Mock GraphClient to return all permissions as granted
    $this->mock(GraphClientInterface::class, function ($mock) {
        $mock->shouldReceive('getServicePrincipalPermissions')
            ->andReturn(new GraphResponse(true, [
                'value' => collect(config('intune_permissions.permissions', []))
                    ->map(fn ($p) => ['value' => $p['key']])
                    ->toArray(),
            ]));
    });

    $tenant = ManagedEnvironment::factory()->create([
        'managed_environment_id' => 'tenant-ok',
        'name' => 'ManagedEnvironment OK',
    ]);

    ensureDefaultProviderConnection($tenant, 'microsoft');

    foreach (requiredPermissions() as $permission) {
        ManagedEnvironmentPermission::create([
            'managed_environment_id' => $tenant->id,
            'permission_key' => $permission['key'],
            'status' => 'granted',
        ]);
    }

    $result = app(ManagedEnvironmentPermissionService::class)->compare($tenant);

    expect($result['overall_status'])->toBe('granted');
    expect(ManagedEnvironmentPermission::where('managed_environment_id', $tenant->id)->where('status', 'granted')->count())
        ->toBe(count(requiredPermissions()));
});

it('marks missing permissions when not granted', function () {
    $permissions = requiredPermissions();

    // Mock GraphClient to return only first permission as granted
    $this->mock(GraphClientInterface::class, function ($mock) use ($permissions) {
        $mock->shouldReceive('getServicePrincipalPermissions')
            ->andReturn(new GraphResponse(true, [
                'permissions' => [$permissions[0]['key']],
            ]));
    });

    $tenant = ManagedEnvironment::factory()->create([
        'managed_environment_id' => 'tenant-missing',
        'name' => 'ManagedEnvironment Missing',
    ]);

    ensureDefaultProviderConnection($tenant, 'microsoft');

    $first = $permissions[0]['key'];
    ManagedEnvironmentPermission::create([
        'managed_environment_id' => $tenant->id,
        'permission_key' => $first,
        'status' => 'ok',
    ]);

    // Use liveCheck=true to trigger Graph API call
    $result = app(ManagedEnvironmentPermissionService::class)->compare($tenant, null, true, true);

    expect($result['overall_status'])->toBe('missing');
    $missingKey = $permissions[1]['key'] ?? null;

    if ($missingKey) {
        $this->assertDatabaseHas('managed_environment_permissions', [
            'managed_environment_id' => $tenant->id,
            'permission_key' => $missingKey,
            'status' => 'missing',
        ]);
    }
});

it('reports error statuses from graph comparison', function () {
    // Mock GraphClient to return an error
    $this->mock(GraphClientInterface::class, function ($mock) {
        $mock->shouldReceive('getServicePrincipalPermissions')
            ->andReturn(new GraphResponse(false, [], 500, ['Graph API error']));
    });

    $tenant = ManagedEnvironment::factory()->create([
        'managed_environment_id' => 'tenant-error',
        'name' => 'ManagedEnvironment Error',
    ]);

    ensureDefaultProviderConnection($tenant, 'microsoft');

    $permissions = requiredPermissions();
    $first = $permissions[0]['key'];

    $result = app(ManagedEnvironmentPermissionService::class)->compare($tenant, [
        $first => [
            'status' => 'error',
            'details' => ['message' => 'forbidden'],
        ],
    ]);

    expect($result['overall_status'])->toBe('error');

    $this->assertDatabaseHas('managed_environment_permissions', [
        'managed_environment_id' => $tenant->id,
        'permission_key' => $first,
        'status' => 'error',
    ]);
});

it('ignores configured stub permissions when requested', function () {
    $originalPermissions = config('intune_permissions.permissions');
    $originalStub = config('intune_permissions.granted_stub');

    config()->set('intune_permissions.permissions', [
        [
            'key' => 'DeviceManagementRBAC.ReadWrite.All',
            'type' => 'application',
            'description' => null,
            'features' => [],
        ],
    ]);
    config()->set('intune_permissions.granted_stub', ['DeviceManagementRBAC.ReadWrite.All']);

    $tenant = ManagedEnvironment::factory()->create();

    ensureDefaultProviderConnection($tenant, 'microsoft');

    ManagedEnvironmentPermission::create([
        'managed_environment_id' => $tenant->id,
        'permission_key' => 'DeviceManagementRBAC.ReadWrite.All',
        'status' => 'granted',
        'details' => ['source' => 'configured'],
    ]);

    $result = app(ManagedEnvironmentPermissionService::class)->compare($tenant, persist: false, useConfiguredStub: false);

    expect($result['overall_status'])->toBe('missing');
    expect($result['permissions'][0]['status'])->toBe('missing');

    config()->set('intune_permissions.permissions', $originalPermissions);
    config()->set('intune_permissions.granted_stub', $originalStub);
});

it('persists permissions with workspace_id even when model events are disabled', function () {
    $tenant = ManagedEnvironment::factory()->create();

    ensureDefaultProviderConnection($tenant, 'microsoft');

    ManagedEnvironmentPermission::withoutEvents(function () use ($tenant): void {
        app(ManagedEnvironmentPermissionService::class)->compare($tenant);
    });

    $this->assertDatabaseHas('managed_environment_permissions', [
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
    ]);
});

it('persists permissions when the tenant instance does not have workspace_id loaded', function () {
    $tenant = ManagedEnvironment::factory()->create();

    ensureDefaultProviderConnection($tenant, 'microsoft');

    $tenantWithoutWorkspaceId = ManagedEnvironment::query()
        ->select(['id', 'managed_environment_id', 'external_id', 'name', 'status', 'environment'])
        ->findOrFail((int) $tenant->getKey());

    expect($tenantWithoutWorkspaceId->getAttribute('workspace_id'))->toBeNull();

    app(ManagedEnvironmentPermissionService::class)->compare($tenantWithoutWorkspaceId);

    $this->assertDatabaseHas('managed_environment_permissions', [
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
    ]);
});

it('does not persist when tenant workspace_id is missing', function () {
    $tenant = ManagedEnvironment::withoutEvents(function (): ManagedEnvironment {
        return ManagedEnvironment::create([
            'managed_environment_id' => 'tenant-no-workspace',
            'external_id' => 'tenant-no-workspace',
            'name' => 'ManagedEnvironment No Workspace',
            'status' => ManagedEnvironment::STATUS_ACTIVE,
            'environment' => 'other',
            'workspace_id' => null,
        ]);
    });

    app(ManagedEnvironmentPermissionService::class)->compare($tenant, persist: true);

    expect(ManagedEnvironmentPermission::query()->where('managed_environment_id', (int) $tenant->getKey())->count())->toBe(0);
});
