<?php

namespace App\Filament\Resources\BackupSetResource\Pages;

use App\Filament\Resources\BackupSetResource;
use App\Support\BackupHealth\TenantBackupHealthAssessment;
use App\Support\Filament\CanonicalAdminEnvironmentFilterState;
use Filament\Resources\Pages\ListRecords;

class ListBackupSets extends ListRecords
{
    protected static string $resource = BackupSetResource::class;

    public function mount(): void
    {
        app(CanonicalAdminEnvironmentFilterState::class)->sync(
            $this->getTableFiltersSessionKey(),
            request: request(),
            environmentFilterName: null,
        );

        parent::mount();
    }

    private function tableHasRecords(): bool
    {
        return $this->getTableRecords()->count() > 0;
    }

    protected function getHeaderActions(): array
    {
        return [
            BackupSetResource::makeCreateAction()
                ->visible(fn (): bool => $this->tableHasRecords()),
        ];
    }

    protected function getTableEmptyStateActions(): array
    {
        return [
            BackupSetResource::makeCreateAction(),
        ];
    }

    public function getSubheading(): ?string
    {
        return match (request()->string('backup_health_reason')->toString()) {
            TenantBackupHealthAssessment::REASON_NO_BACKUP_BASIS => 'No usable completed backup basis is currently available for this tenant.',
            TenantBackupHealthAssessment::REASON_LATEST_BACKUP_STALE => 'The latest backup detail is no longer available, so this view stays on the backup-set list.',
            TenantBackupHealthAssessment::REASON_LATEST_BACKUP_DEGRADED => 'The latest backup detail is no longer available, so this view stays on the backup-set list.',
            default => null,
        };
    }
}
