<?php

declare(strict_types=1);

namespace App\Filament\Resources\StoredReportResource\Pages;

use App\Filament\Resources\StoredReportResource;
use App\Models\StoredReport;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Database\Eloquent\Model;

class ViewStoredReport extends ViewRecord
{
    protected static string $resource = StoredReportResource::class;

    protected function resolveRecord(int|string $key): Model
    {
        return StoredReportResource::resolveScopedRecordOrFail($key);
    }

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('open_current_report')
                ->label('Open current report')
                ->icon('heroicon-o-arrow-top-right-on-square')
                ->url(fn (): ?string => $this->record instanceof StoredReport
                    ? StoredReportResource::currentReportUrlFor($this->record)
                    : null)
                ->visible(fn (): bool => $this->record instanceof StoredReport
                    && StoredReportResource::currentReportUrlFor($this->record) !== null),
        ];
    }
}
