<?php

declare(strict_types=1);

use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('shows onboarding and archived tenants on the managed-tenants landing with lifecycle labels', function (): void {
    $workspace = Workspace::factory()->create(['slug' => 'lifecycle-ws']);
    $user = User::factory()->create();

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => 'owner',
    ]);

    $active = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Active ManagedEnvironment',
    ]);
    $onboarding = ManagedEnvironment::factory()->onboarding()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Onboarding ManagedEnvironment',
    ]);
    $archived = ManagedEnvironment::factory()->archived()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Archived ManagedEnvironment',
    ]);
    $outsider = ManagedEnvironment::factory()->active()->create(['name' => 'Other Workspace ManagedEnvironment']);

    $user->tenants()->syncWithoutDetaching([
        $active->getKey() => ['role' => 'owner'],
        $onboarding->getKey() => ['role' => 'owner'],
        $archived->getKey() => ['role' => 'owner'],
        $outsider->getKey() => ['role' => 'owner'],
    ]);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->get(route('admin.workspace.managed-environments.index', ['workspace' => $workspace]))
        ->assertSuccessful()
        ->assertSee('Active ManagedEnvironment')
        ->assertSee('Onboarding ManagedEnvironment')
        ->assertSee('Archived ManagedEnvironment')
        ->assertSee('Active')
        ->assertSee('Onboarding')
        ->assertSee('Archived')
        ->assertDontSee('Other Workspace ManagedEnvironment');
});

it('keeps managed tenants discoverable with no selected tenant context', function (): void {
    $workspace = Workspace::factory()->create(['slug' => 'discoverable-managed-tenants']);
    $user = User::factory()->create();

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => 'owner',
    ]);

    $onboardingTenant = ManagedEnvironment::factory()->onboarding()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Discoverable Onboarding ManagedEnvironment',
    ]);
    $draftTenant = ManagedEnvironment::factory()->draft()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Discoverable Draft ManagedEnvironment',
    ]);
    $archivedTenant = ManagedEnvironment::factory()->archived()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Discoverable Archived ManagedEnvironment',
    ]);

    $user->tenants()->syncWithoutDetaching([
        $draftTenant->getKey() => ['role' => 'owner'],
        $onboardingTenant->getKey() => ['role' => 'owner'],
        $archivedTenant->getKey() => ['role' => 'owner'],
    ]);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->get(route('admin.workspace.managed-environments.index', ['workspace' => $workspace]))
        ->assertSuccessful()
        ->assertSee('Discoverable Draft ManagedEnvironment')
        ->assertSee('Discoverable Onboarding ManagedEnvironment')
        ->assertSee('Discoverable Archived ManagedEnvironment');
});

it('keeps administrative landing discoverability broader than choose-environment selection', function (): void {
    $workspace = Workspace::factory()->create(['slug' => 'landing-vs-selector']);
    $user = User::factory()->create();

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => 'owner',
    ]);

    $activeEnvironment = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Landing Active ManagedEnvironment',
    ]);
    $draftTenant = ManagedEnvironment::factory()->draft()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Landing Draft ManagedEnvironment',
    ]);
    $onboardingTenant = ManagedEnvironment::factory()->onboarding()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Landing Onboarding ManagedEnvironment',
    ]);
    $archivedTenant = ManagedEnvironment::factory()->archived()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Landing Archived ManagedEnvironment',
    ]);

    $user->tenants()->syncWithoutDetaching([
        $activeEnvironment->getKey() => ['role' => 'owner'],
        $draftTenant->getKey() => ['role' => 'owner'],
        $onboardingTenant->getKey() => ['role' => 'owner'],
        $archivedTenant->getKey() => ['role' => 'owner'],
    ]);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->get(route('admin.workspace.managed-environments.index', ['workspace' => $workspace]))
        ->assertSuccessful()
        ->assertSee('Landing Active ManagedEnvironment')
        ->assertSee('Landing Draft ManagedEnvironment')
        ->assertSee('Landing Onboarding ManagedEnvironment')
        ->assertSee('Landing Archived ManagedEnvironment');

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->get('/admin/choose-environment')
        ->assertSuccessful()
        ->assertSee('Landing Active ManagedEnvironment')
        ->assertDontSee('Landing Draft ManagedEnvironment')
        ->assertDontSee('Landing Onboarding ManagedEnvironment')
        ->assertDontSee('Landing Archived ManagedEnvironment');
});
