<?php

declare(strict_types=1);

use App\Models\ManagedEnvironment;
use App\Models\OperationRun;
use App\Models\ProviderConnection;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

pest()->browser()->timeout(20_000);

it('smokes the workspace-first admin flow from workspace selection to environment dashboard to operations hub', function (): void {
    $workspace = Workspace::factory()->create([
        'name' => 'Spec 280 Workspace',
    ]);
    $otherWorkspace = Workspace::factory()->create([
        'name' => 'Spec 280 Other Workspace',
    ]);

    $tenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Spec 280 Production',
        'slug' => 'spec-280-production',
    ]);
    $secondaryTenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Spec 280 Secondary',
        'slug' => 'spec-280-secondary',
    ]);
    $otherWorkspaceTenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $otherWorkspace->getKey(),
        'name' => 'Spec 280 Other Workspace ManagedEnvironment',
        'slug' => 'spec-280-other-workspace',
    ]);

    $user = User::factory()->create();

    foreach ([$workspace, $otherWorkspace] as $memberWorkspace) {
        WorkspaceMembership::factory()->create([
            'workspace_id' => (int) $memberWorkspace->getKey(),
            'user_id' => (int) $user->getKey(),
            'role' => 'owner',
        ]);
    }

    foreach ([$tenant, $secondaryTenant, $otherWorkspaceTenant] as $memberTenant) {
        $user->tenants()->syncWithoutDetaching([
            (int) $memberTenant->getKey() => ['role' => 'owner'],
        ]);
    }

    ProviderConnection::factory()->platform()->consentGranted()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $workspace->getKey(),
        'is_default' => true,
    ]);

    $run = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $workspace->getKey(),
        'type' => 'inventory_sync',
        'status' => OperationRunStatus::Queued->value,
        'outcome' => OperationRunOutcome::Pending->value,
    ]);

    $this->actingAs($user);

    $workspaceChooser = visit('/admin')
        ->waitForText('Spec 280 Workspace')
        ->assertSee('Spec 280 Other Workspace')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    $environmentChooser = visit(route('admin.workspace.managed-environments.index', ['workspace' => $workspace]))
        ->waitForText('Spec 280 Production')
        ->assertSee('Spec 280 Secondary')
        ->assertDontSee('Spec 280 Other Workspace ManagedEnvironment')
        ->assertScript("window.location.pathname.includes('/admin/workspaces/{$workspace->getRouteKey()}/environments')", true)
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    $dashboard = $environmentChooser
        ->click('[wire\\:key="tenant-'.$tenant->getKey().'"]')
        ->waitForText('Spec 280 Production')
        ->waitForText('Show all operations')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    $dashboard
        ->click('Show all operations')
        ->waitForText('Monitoring landing')
        ->assertSee('Open run detail')
        ->assertSee('Spec 280 Production')
        ->assertScript("window.location.pathname.includes('/admin/workspaces/{$workspace->getRouteKey()}/operations')", true)
        ->assertScript("window.location.search.includes('environment_id={$tenant->getKey()}')", true)
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();
});
