<?php

declare(strict_types=1);

use App\Filament\Resources\RestoreRunResource\Pages\ListRestoreRuns;
use App\Models\BackupSet;
use App\Models\RestoreRun;
use App\Models\ManagedEnvironment;
use App\Support\RestoreRunStatus;
use Filament\Facades\Filament;
use Livewire\Livewire;

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

it('filters restore runs by status and derived outcome 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,
    ]);

    $backupSetA = BackupSet::factory()->create([
        'managed_environment_id' => (int) $tenantA->getKey(),
    ]);

    $backupSetB = BackupSet::factory()->create([
        'managed_environment_id' => (int) $tenantB->getKey(),
    ]);

    $matching = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenantA->getKey(),
        'backup_set_id' => (int) $backupSetA->getKey(),
        'status' => RestoreRunStatus::Completed->value,
        'started_at' => now()->subDay(),
    ]);

    $partial = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenantA->getKey(),
        'backup_set_id' => (int) $backupSetA->getKey(),
        'status' => RestoreRunStatus::Partial->value,
        'started_at' => now()->subDay(),
    ]);

    $otherTenant = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenantB->getKey(),
        'backup_set_id' => (int) $backupSetB->getKey(),
        'status' => RestoreRunStatus::Completed->value,
        'started_at' => now()->subDay(),
    ]);

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

    Livewire::test(ListRestoreRuns::class)
        ->filterTable('status', RestoreRunStatus::Completed->value)
        ->filterTable('outcome', 'succeeded')
        ->assertCanSeeTableRecords([$matching])
        ->assertCanNotSeeTableRecords([$partial, $otherTenant]);
});

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

    $backupSet = BackupSet::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

    $recentActive = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'backup_set_id' => (int) $backupSet->getKey(),
        'status' => RestoreRunStatus::Completed->value,
        'started_at' => now()->subDay(),
    ]);

    $recentArchived = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'backup_set_id' => (int) $backupSet->getKey(),
        'status' => RestoreRunStatus::Failed->value,
        'started_at' => now()->subDay(),
        'deleted_at' => now(),
    ]);

    $oldActive = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'backup_set_id' => (int) $backupSet->getKey(),
        'status' => RestoreRunStatus::Completed->value,
        'started_at' => now()->subDays(10),
    ]);

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

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

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

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

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

    $backupSet = BackupSet::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

    $active = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'backup_set_id' => (int) $backupSet->getKey(),
        'status' => RestoreRunStatus::Completed->value,
    ]);

    $archived = RestoreRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'backup_set_id' => (int) $backupSet->getKey(),
        'status' => RestoreRunStatus::Failed->value,
        'deleted_at' => now(),
    ]);

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

    $component = Livewire::test(ListRestoreRuns::class)
        ->filterTable('status', RestoreRunStatus::Failed->value)
        ->filterTable('trashed', false)
        ->assertCanSeeTableRecords([$archived])
        ->assertCanNotSeeTableRecords([$active]);

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