<?php

declare(strict_types=1);

use App\Filament\Resources\ReviewPackResource;
use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentMembership;
use App\Models\ReviewPack;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Services\Auth\CapabilityResolver;
use App\Services\Auth\ManagedEnvironmentAccessScopeResolver;
use App\Support\Auth\WorkspaceRole;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Gate;

it('denies cross-workspace review pack direct access as not found', function (): void {
    $targetTenant = ManagedEnvironment::factory()->active()->create();
    $otherTenant = ManagedEnvironment::factory()->active()->create();

    [$user] = createUserWithTenant($otherTenant, role: WorkspaceRole::Owner->value);

    $pack = ReviewPack::factory()->ready()->create([
        'managed_environment_id' => (int) $targetTenant->getKey(),
        'workspace_id' => (int) $targetTenant->workspace_id,
    ]);

    $this->actingAs($user)
        ->get(ReviewPackResource::getUrl('view', ['record' => $pack], panel: 'admin', tenant: $targetTenant))
        ->assertNotFound();
});

it('denies same-workspace wrong-environment review pack direct access as not found', function (): void {
    $workspace = Workspace::factory()->create();
    $allowedTenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
    ]);
    $deniedTenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $workspace->getKey(),
    ]);
    $user = User::factory()->create();

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => WorkspaceRole::Manager->value,
    ]);
    ManagedEnvironmentMembership::query()->create([
        'managed_environment_id' => (int) $allowedTenant->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => WorkspaceRole::Readonly->value,
        'source' => 'manual',
    ]);
    app(CapabilityResolver::class)->clearCache();
    app(ManagedEnvironmentAccessScopeResolver::class)->clearCache();

    $pack = ReviewPack::factory()->ready()->create([
        'managed_environment_id' => (int) $deniedTenant->getKey(),
        'workspace_id' => (int) $workspace->getKey(),
    ]);

    $allowedTenant->makeCurrent();
    Filament::setTenant($allowedTenant, true);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->get(ReviewPackResource::getUrl('view', ['record' => $pack], panel: 'admin', tenant: $allowedTenant))
        ->assertNotFound();
});

it('denies readonly review pack mutation server-side', function (): void {
    [$user, $tenant] = createUserWithTenant(role: WorkspaceRole::Readonly->value);

    $pack = ReviewPack::factory()->ready()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    $response = Gate::forUser($user)->inspect('delete', $pack);

    expect($response->denied())->toBeTrue()
        ->and($response->status())->not->toBe(404);
});
