<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSupplierSettlementQueries\Pages;

use App\Filament\Resources\DberpSupplierSettlementQueries\DberpSupplierSettlementQueryResource;
use App\Services\DberpPermissionService;
use App\Support\DberpSettlementEntryFilamentSupport;
use Filament\Actions\Action;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Auth;

class ListDberpSupplierSettlementQueries extends ListRecords
{
    protected static string $resource = DberpSupplierSettlementQueryResource::class;

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

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

        return [
            Action::make('supplierPayables')
                ->label(__('dberp.resources.supplier_settlement_query.link_supplier_payables'))
                ->url(DberpSettlementEntryFilamentSupport::supplierPayablesUrl($tableFilters))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::VIEW_SUPPLIER_PAYABLE)),
            Action::make('receiptPaymentQuery')
                ->label(__('dberp.resources.supplier_settlement_query.link_receipt_payment_query'))
                ->url(DberpSettlementEntryFilamentSupport::receiptPaymentQueryUrl($tableFilters, 'payment'))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_SUPPLIER_PAYABLE,
                    DberpPermissionService::CREATE_PAYMENT_ORDER,
                    DberpPermissionService::VIEW_WORKBENCH_OVERVIEW,
                ])),
            Action::make('settlementWriteOffs')
                ->label(__('dberp.resources.supplier_settlement_query.link_write_offs'))
                ->url(DberpSettlementEntryFilamentSupport::settlementWriteOffsUrl($tableFilters, 'payable'))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::WRITE_OFF)),
            Action::make('settlementStatements')
                ->label(__('dberp.resources.supplier_settlement_query.link_statements'))
                ->url(DberpSettlementEntryFilamentSupport::settlementStatementsUrl($tableFilters, 'payable'))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_SETTLEMENT_STATEMENT,
                    DberpPermissionService::CONFIRM_SETTLEMENT_STATEMENT,
                ])),
        ];
    }
}
