<?php

declare(strict_types=1);

use App\Filament\Resources\PolicyVersionResource\Pages\ListPolicyVersions;
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\ManagedEnvironment;
use Filament\Facades\Filament;
use Livewire\Livewire;

function policyVersionFilterIndicatorLabels($component): array
{
    return collect($component->instance()->getTable()->getFilterIndicators())
        ->map(fn ($indicator): string => (string) $indicator->getLabel())
        ->all();
}

it('filters policy versions by type and platform without leaking other tenants', function (): void {
    $tenantA = ManagedEnvironment::factory()->create();
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

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

    createUserWithTenant(tenant: $tenantB, user: $user, role: 'owner');

    $policyA = Policy::query()->create([
        'managed_environment_id' => (int) $tenantA->getKey(),
        'external_id' => 'policy-a',
        'policy_type' => 'deviceConfiguration',
        'display_name' => 'Policy A',
        'platform' => 'windows',
    ]);

    $policyB = Policy::query()->create([
        'managed_environment_id' => (int) $tenantB->getKey(),
        'external_id' => 'policy-b',
        'policy_type' => 'deviceConfiguration',
        'display_name' => 'Policy B',
        'platform' => 'windows',
    ]);

    $matching = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenantA->getKey(),
        'policy_id' => (int) $policyA->getKey(),
        'version_number' => 1,
        'policy_type' => 'deviceConfiguration',
        'platform' => 'windows',
    ]);

    $wrongPlatform = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenantA->getKey(),
        'policy_id' => (int) $policyA->getKey(),
        'version_number' => 2,
        'policy_type' => 'deviceConfiguration',
        'platform' => 'android',
    ]);

    $otherTenant = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenantB->getKey(),
        'policy_id' => (int) $policyB->getKey(),
        'policy_type' => 'deviceConfiguration',
        'platform' => 'windows',
    ]);

    $this->actingAs($user);
    $tenantA->makeCurrent();
    Filament::setTenant($tenantA, true);

    Livewire::test(ListPolicyVersions::class)
        ->filterTable('policy_type', 'deviceConfiguration')
        ->filterTable('platform', 'windows')
        ->assertCanSeeTableRecords([$matching])
        ->assertCanNotSeeTableRecords([$wrongPlatform, $otherTenant]);
});

it('filters policy versions by captured date range and archived visibility', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $policy = Policy::query()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'external_id' => 'policy-date',
        'policy_type' => 'deviceConfiguration',
        'display_name' => 'Policy Date',
        'platform' => 'windows',
    ]);

    $recentActive = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'policy_id' => (int) $policy->getKey(),
        'version_number' => 1,
        'captured_at' => now()->subDay(),
    ]);

    $recentArchived = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'policy_id' => (int) $policy->getKey(),
        'version_number' => 2,
        'captured_at' => now()->subDay(),
        'deleted_at' => now(),
    ]);

    $oldActive = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'policy_id' => (int) $policy->getKey(),
        'version_number' => 3,
        'captured_at' => now()->subDays(10),
    ]);

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

    $component = Livewire::test(ListPolicyVersions::class)
        ->set('tableFilters.captured_at.from', now()->subDays(2)->toDateString())
        ->set('tableFilters.captured_at.until', now()->toDateString())
        ->filterTable('trashed', true)
        ->assertCanSeeTableRecords([$recentActive, $recentArchived])
        ->assertCanNotSeeTableRecords([$oldActive]);

    expect(policyVersionFilterIndicatorLabels($component))
        ->toContain('Captured from '.now()->subDays(2)->toFormattedDateString())
        ->toContain('Captured until '.now()->toFormattedDateString());

    $component
        ->filterTable('trashed', false)
        ->assertCanSeeTableRecords([$recentArchived])
        ->assertCanNotSeeTableRecords([$recentActive, $oldActive]);
});

it('clears policy version filters back to active records', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $policy = Policy::query()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'external_id' => 'policy-clear',
        'policy_type' => 'deviceConfiguration',
        'display_name' => 'Policy Clear',
        'platform' => 'windows',
    ]);

    $active = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'policy_id' => (int) $policy->getKey(),
        'version_number' => 1,
        'platform' => 'windows',
        'captured_at' => now()->subDay(),
    ]);

    $archived = PolicyVersion::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'policy_id' => (int) $policy->getKey(),
        'version_number' => 2,
        'platform' => 'windows',
        'captured_at' => now()->subDay(),
        'deleted_at' => now(),
    ]);

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

    $component = Livewire::test(ListPolicyVersions::class)
        ->filterTable('platform', 'windows')
        ->filterTable('trashed', false)
        ->assertCanSeeTableRecords([$archived])
        ->assertCanNotSeeTableRecords([$active]);

    $component
        ->set('tableFilters.platform.value', null)
        ->filterTable('trashed', null)
        ->assertCanSeeTableRecords([$active])
        ->assertCanNotSeeTableRecords([$archived]);
});
