<?php

declare(strict_types=1);

use App\Filament\Resources\PolicyResource;
use App\Models\ManagedEnvironment;
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\ProviderConnection;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

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

it('smokes environment-first chooser and policy helper copy', function (): void {
    $workspace = Workspace::factory()->create([
        'name' => 'Spec 286 Workspace',
    ]);

    $environment = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Spec 286 Production',
        'slug' => 'spec-286-production',
    ]);

    $secondaryEnvironment = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Spec 286 Secondary',
        'slug' => 'spec-286-secondary',
    ]);

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

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

    foreach ([$environment, $secondaryEnvironment] as $memberEnvironment) {
        $user->tenants()->syncWithoutDetaching([
            (int) $memberEnvironment->getKey() => ['role' => 'owner'],
        ]);
    }

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

    $policy = Policy::factory()->create([
        'managed_environment_id' => (int) $environment->getKey(),
        'display_name' => 'Spec 286 Policy',
    ]);

    PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $environment->getKey(),
        'policy_id' => (int) $policy->getKey(),
        'metadata' => [],
    ]);

    $this->actingAs($user);

    $landing = visit(route('admin.workspace.managed-environments.index', ['workspace' => $workspace]))
        ->waitForText('Managed environments')
        ->assertSee('Choose environment')
        ->assertSee('Spec 286 Production')
        ->assertSee('Spec 286 Secondary')
        ->assertDontSee('Managed tenants')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    $landing
        ->click('[wire\:key="tenant-'.$environment->getKey().'"]')
        ->waitForText('Spec 286 Production')
        ->waitForText('Environment governance overview')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    visit(PolicyResource::getUrl('view', ['record' => $policy], tenant: $environment))
        ->waitForText('Capture snapshot')
        ->assertSee('Restore to environment')
        ->assertDontSee('Restore to Microsoft Intune')
        ->click('Capture snapshot')
        ->waitForText('Capture snapshot now')
        ->assertSee('current environment')
        ->assertSee('Source: Microsoft Intune')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();
});