<?php

use App\Jobs\EntraGroupSyncJob;
use App\Services\Directory\EntraGroupSyncService;
use App\Services\Providers\ProviderOperationStartResult;
use App\Support\OperationRunType;
use Illuminate\Support\Facades\Queue;

it('starts a manual group sync by creating a run and dispatching a job', function () {
    Queue::fake();

    [$user, $tenant] = createUserWithTenant(role: 'owner', fixtureProfile: 'credential-enabled');
    spec283SeedRequirementRows($tenant, ['permissions.directory_groups']);

    $service = app(EntraGroupSyncService::class);

    $result = $service->startManualSync($tenant, $user);
    $run = $result->run;

    expect($result)->toBeInstanceOf(ProviderOperationStartResult::class)
        ->and($result->status)->toBe('started');

    expect($run)
        ->and($run->managed_environment_id)->toBe($tenant->getKey())
        ->and($run->user_id)->toBe($user->getKey())
        ->and($run->type)->toBe(OperationRunType::DirectoryGroupsSync->value)
        ->and($run->status)->toBe('queued')
        ->and($run->context['selection_key'] ?? null)->toBe('groups-v1:all')
        ->and($run->context['provider_connection_id'] ?? null)->toBeInt();

    Queue::assertPushed(EntraGroupSyncJob::class);
});
