<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpPurchaseStatistics\Pages;

use App\Filament\Resources\DberpPurchaseStatistics\DberpPurchaseStatisticResource;
use App\Services\DberpPermissionService;
use App\Support\DberpPurchaseStatisticsFilamentSupport;
use Filament\Actions\Action;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Auth;

class ListDberpPurchaseStatistics extends ListRecords
{
    protected static string $resource = DberpPurchaseStatisticResource::class;

    public function getSubheading(): string|Htmlable|null
    {
        return __('dberp.resources.purchase_statistics.scope_hint');
    }

    protected function getHeaderActions(): array
    {
        $permissions = app(DberpPermissionService::class);
        $user = Auth::user();
        $tableFilters = is_array($this->tableFilters) ? $this->tableFilters : [];

        return [
            Action::make('purchaseOrders')
                ->label(__('dberp.resources.purchase_statistics.link_purchase_orders'))
                ->url(DberpPurchaseStatisticsFilamentSupport::purchaseOrderIndexUrlFromTableState($tableFilters))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::VIEW_PURCHASE_ORDER)),
            Action::make('supplierPayables')
                ->label(__('dberp.resources.purchase_statistics.link_supplier_payables'))
                ->url(DberpPurchaseStatisticsFilamentSupport::supplierPayableIndexUrlFromTableState($tableFilters))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::VIEW_SUPPLIER_PAYABLE)),
        ];
    }
}
