<?php

declare(strict_types=1);

use App\Filament\Pages\ChooseEnvironment;
use App\Filament\Pages\EnvironmentDashboard;
use App\Filament\Pages\Workspaces\ManagedEnvironmentsLanding;
use App\Filament\Resources\ManagedEnvironmentResource;
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;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('keeps the spec 195 managed-tenants landing available without an active tenant context', function (): void {
    $workspace = Workspace::factory()->create(['slug' => 'spec195-managed-tenants']);
    $user = User::factory()->create();

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

    $tenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Spec195 Landing ManagedEnvironment',
    ]);

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

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->get(route('admin.workspace.managed-environments.index', ['workspace' => $workspace]))
        ->assertSuccessful()
        ->assertSee('Spec195 Landing ManagedEnvironment')
        ->assertSee(__('localization.shell.managed_environments_title'))
        ->assertDontSee(__('localization.shell.no_environment_selected'));
});

it('routes the managed-tenants landing back into the chooser flow and open-tenant flow', function (): void {
    $workspace = Workspace::factory()->create(['slug' => 'spec195-managed-routing']);
    $user = User::factory()->create();

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

    $tenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Spec195 Routed ManagedEnvironment',
    ]);

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

    $this->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);
    session([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);

    $component = Livewire::actingAs($user)
        ->test(ManagedEnvironmentsLanding::class, ['workspace' => $workspace]);

    $component
        ->call('goToChooseEnvironment')
        ->assertRedirect(route('admin.workspace.managed-environments.index', ['workspace' => $workspace]));

    $this->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);
    session([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);

    Livewire::actingAs($user)
        ->test(ManagedEnvironmentsLanding::class, ['workspace' => $workspace])
        ->call('openTenant', $tenant->getKey())
        ->assertRedirect(EnvironmentDashboard::getUrl(tenant: $tenant));
});

it('allows workspace-scoped access to open an environment from the landing without explicit membership', function (): void {
    $workspace = Workspace::factory()->create(['slug' => 'spec195-managed-guard']);
    $user = User::factory()->create();

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

    $tenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Spec195 Guarded ManagedEnvironment',
    ]);

    $this->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);
    session([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()]);

    Livewire::actingAs($user)
        ->test(ManagedEnvironmentsLanding::class, ['workspace' => $workspace])
        ->call('openTenant', $tenant->getKey())
        ->assertRedirect(EnvironmentDashboard::getUrl(tenant: $tenant));
});
