<?php

declare(strict_types=1);

use App\Models\ManagedEnvironment;
use App\Services\Auth\WorkspaceRoleCapabilityMap;
use App\Support\Auth\Capabilities;

it('registers managed environment onboarding wizard capabilities in the canonical registry', function (): void {
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_IDENTIFY))->toBeTrue();
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_CONNECTION_VIEW))->toBeTrue();
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_CONNECTION_MANAGE))->toBeTrue();
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_VERIFICATION_START))->toBeTrue();
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_BOOTSTRAP_INVENTORY_SYNC))->toBeTrue();
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_BOOTSTRAP_POLICY_SYNC))->toBeTrue();
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_BOOTSTRAP_BACKUP_BOOTSTRAP))->toBeTrue();
    expect(Capabilities::isKnown(Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_ACTIVATE))->toBeTrue();
});

it('maps onboarding wizard capabilities to workspace roles (least privilege)', function (): void {
    expect(WorkspaceRoleCapabilityMap::hasCapability('owner', Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_IDENTIFY))->toBeTrue();
    expect(WorkspaceRoleCapabilityMap::hasCapability('manager', Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_IDENTIFY))->toBeTrue();
    expect(WorkspaceRoleCapabilityMap::hasCapability('operator', Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_IDENTIFY))->toBeFalse();
    expect(WorkspaceRoleCapabilityMap::hasCapability('readonly', Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_IDENTIFY))->toBeFalse();

    expect(WorkspaceRoleCapabilityMap::hasCapability('owner', Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_ACTIVATE))->toBeTrue();
    expect(WorkspaceRoleCapabilityMap::hasCapability('manager', Capabilities::WORKSPACE_MANAGED_ENVIRONMENT_ONBOARD_ACTIVATE))->toBeFalse();
});

it('supports the v1 managed environment lifecycle statuses', function (): void {
    $draft = ManagedEnvironment::factory()->create(['status' => ManagedEnvironment::STATUS_DRAFT]);
    $onboarding = ManagedEnvironment::factory()->create(['status' => ManagedEnvironment::STATUS_ONBOARDING]);
    $active = ManagedEnvironment::factory()->create(['status' => ManagedEnvironment::STATUS_ACTIVE]);

    expect(ManagedEnvironment::activeQuery()->pluck('id')->all())->toContain((int) $active->getKey());
    expect(ManagedEnvironment::activeQuery()->pluck('id')->all())->not->toContain((int) $draft->getKey());
    expect(ManagedEnvironment::activeQuery()->pluck('id')->all())->not->toContain((int) $onboarding->getKey());

    $onboarding->delete();
    $onboarding->refresh();

    expect($onboarding->status)->toBe(ManagedEnvironment::STATUS_ARCHIVED);

    $onboarding->restore();
    $onboarding->refresh();

    expect($onboarding->status)->toBe(ManagedEnvironment::STATUS_ACTIVE);
});
