<?php

declare(strict_types=1);

use App\Filament\Resources\BaselineProfileResource\Pages\ListBaselineProfiles;
use App\Models\BaselineProfile;
use App\Models\BaselineTenantAssignment;
use App\Models\ManagedEnvironment;
use App\Models\Workspace;
use App\Support\Baselines\BaselineProfileStatus;
use Filament\Tables\Columns\TextColumn;
use Livewire\Livewire;

it('filters baseline profiles by status inside the current workspace', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    expect(defined(BaselineProfile::class.'::STATUS_ACTIVE'))->toBeFalse();

    $active = BaselineProfile::factory()->active()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $draft = BaselineProfile::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => BaselineProfileStatus::Draft->value,
    ]);

    $otherWorkspace = Workspace::factory()->create();
    $otherWorkspaceProfile = BaselineProfile::factory()->active()->create([
        'workspace_id' => (int) $otherWorkspace->getKey(),
    ]);

    Livewire::actingAs($user)
        ->test(ListBaselineProfiles::class)
        ->filterTable('status', BaselineProfileStatus::Active->value)
        ->assertCanSeeTableRecords([$active])
        ->assertCanNotSeeTableRecords([$draft, $otherWorkspaceProfile]);
});

it('clears the baseline profile status filter back to the workspace-owned list', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $active = BaselineProfile::factory()->active()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $draft = BaselineProfile::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => BaselineProfileStatus::Draft->value,
    ]);

    $component = Livewire::actingAs($user)
        ->test(ListBaselineProfiles::class)
        ->filterTable('status', BaselineProfileStatus::Draft->value)
        ->assertCanSeeTableRecords([$draft])
        ->assertCanNotSeeTableRecords([$active]);

    $component
        ->set('tableFilters.status.value', null)
        ->assertCanSeeTableRecords([$active, $draft]);
});

it('shows assigned tenant counts on the baseline profile list', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $assignedProfile = BaselineProfile::factory()->active()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $unassignedProfile = BaselineProfile::factory()->active()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $firstAssignedTenant = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $secondAssignedTenant = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    BaselineTenantAssignment::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $firstAssignedTenant->getKey(),
        'baseline_profile_id' => (int) $assignedProfile->getKey(),
    ]);

    BaselineTenantAssignment::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $secondAssignedTenant->getKey(),
        'baseline_profile_id' => (int) $assignedProfile->getKey(),
    ]);

    Livewire::actingAs($user)
        ->test(ListBaselineProfiles::class)
        ->assertTableColumnExists('tenant_assignments_count', function (TextColumn $column): bool {
            return $column->getLabel() === 'Assigned environments'
                && (int) $column->getState() === 2;
        }, $assignedProfile)
        ->assertTableColumnExists('tenant_assignments_count', function (TextColumn $column): bool {
            return (int) $column->getState() === 0;
        }, $unassignedProfile);
});
