<?php

use App\Models\ManagedEnvironment;
use App\Models\ProviderConnection;
use App\Models\Workspace;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;

uses(RefreshDatabase::class);

it('owns workspace relation, slug route binding, and selectable lifecycle truth', function (): void {
    $workspace = Workspace::factory()->create();

    $environment = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Contoso Production',
        'display_name' => 'Contoso Prod',
        'slug' => 'contoso-prod',
        'kind' => 'production',
        'lifecycle_status' => ManagedEnvironment::STATUS_ACTIVE,
    ]);

    expect($workspace->managedEnvironments()->whereKey($environment->getKey())->exists())->toBeTrue()
        ->and($workspace->tenants()->whereKey($environment->getKey())->exists())->toBeTrue()
        ->and($environment->workspace->is($workspace))->toBeTrue()
        ->and($environment->getRouteKeyName())->toBe('slug')
        ->and((new ManagedEnvironment)->resolveRouteBinding('contoso-prod')->is($environment))->toBeTrue()
        ->and($environment->isSelectableAsContext())->toBeTrue()
        ->and($environment->getFilamentName())->toBe('Contoso Prod (PRODUCTION)');
});

it('keeps archived environments resolvable by route key but unavailable as active context', function (): void {
    $environment = ManagedEnvironment::factory()->archived()->create([
        'slug' => 'archived-environment',
    ]);

    $resolved = (new ManagedEnvironment)->resolveRouteBinding('archived-environment');

    expect($resolved)->toBeInstanceOf(ManagedEnvironment::class)
        ->and($resolved->is($environment))->toBeTrue()
        ->and($resolved->isSelectableAsContext())->toBeFalse();
});

it('keeps provider-specific identity off managed environment columns', function (): void {
    $columns = Schema::getColumnListing('managed_environments');

    expect($columns)->not->toContain(
        'external_id',
        'tenant_id',
        'managed_environment_id',
        'status',
        'environment',
        'app_client_id',
        'app_client_secret',
        'app_certificate_thumbprint',
        'app_notes',
        'app_status',
        'domain',
        'entra_tenant_id',
        'graph_tenant_id',
    );
});

it('resolves Microsoft tenant identity through provider-owned seams only', function (): void {
    $environment = ManagedEnvironment::factory()->create([
        'slug' => 'provider-neutral-root',
    ]);

    ProviderConnection::factory()->platform()->consentGranted()->create([
        'managed_environment_id' => (int) $environment->getKey(),
        'workspace_id' => (int) $environment->workspace_id,
        'provider' => 'microsoft',
        'entra_tenant_id' => '11111111-1111-1111-1111-111111111111',
        'is_default' => true,
    ]);

    expect($environment->refresh()->graphTenantId())->toBe('11111111-1111-1111-1111-111111111111')
        ->and(fn () => $environment->graphOptions())->toThrow(BadMethodCallException::class);
});

it('treats managed_environment_id as a write-through alias for slug', function (): void {
    $environment = ManagedEnvironment::factory()->create([
        'managed_environment_id' => 'alias-slug',
    ]);

    expect($environment->slug)->toBe('alias-slug')
        ->and($environment->managed_environment_id)->toBe('alias-slug')
        ->and($environment->external_id)->toBe('alias-slug');
});
