<?php

declare(strict_types=1);

use App\Filament\Resources\Workspaces\Pages\ViewWorkspace;
use App\Filament\Resources\Workspaces\WorkspaceResource;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    Filament::setCurrentPanel('admin');
    Filament::bootCurrentPanel();
});

it('renders a closed workspace as read-only in the admin workspace detail surface', function (): void {
    $workspace = Workspace::factory()->create([
        'name' => 'Closed Admin Workspace',
        'closed_at' => now(),
        'closed_reason' => 'Closed after customer offboarding.',
    ]);

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

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

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->get(WorkspaceResource::getUrl('view', ['record' => $workspace], panel: 'admin'))
        ->assertSuccessful()
        ->assertSee('Closed')
        ->assertSee('Closed after customer offboarding.')
        ->assertDontSee('Suspended read-only');

    expect(WorkspaceResource::canEdit($workspace))->toBeFalse();

    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspace->getKey());

    Livewire::actingAs($user)
        ->test(ViewWorkspace::class, ['record' => $workspace->getRouteKey()])
        ->assertActionHidden('edit');
});
