<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpReceiptPaymentQueries\Pages;

use App\Filament\Resources\DberpReceiptPaymentQueries\DberpReceiptPaymentQueryResource;
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 ListDberpReceiptPaymentQueries extends ListRecords
{
    protected static string $resource = DberpReceiptPaymentQueryResource::class;

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

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

        return [
            Action::make('receiptOrders')
                ->label(__('dberp.resources.receipt_payment_query.link_receipt_orders'))
                ->url(DberpSettlementEntryFilamentSupport::receiptOrdersUrlFromReceiptPaymentTableFilters($tableFilters))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_RECEIPT_ORDER,
                    DberpPermissionService::CREATE_RECEIPT_ORDER,
                    DberpPermissionService::CONFIRM_RECEIPT_PAYMENT,
                ])),
            Action::make('paymentOrders')
                ->label(__('dberp.resources.receipt_payment_query.link_payment_orders'))
                ->url(DberpSettlementEntryFilamentSupport::paymentOrdersUrlFromReceiptPaymentTableFilters($tableFilters))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_PAYMENT_ORDER,
                    DberpPermissionService::CREATE_PAYMENT_ORDER,
                    DberpPermissionService::CONFIRM_RECEIPT_PAYMENT,
                ])),
            Action::make('customerReceivables')
                ->label(__('dberp.resources.receipt_payment_query.link_customer_receivables'))
                ->url(DberpSettlementEntryFilamentSupport::customerReceivablesUrlFromReceiptPaymentTableFilters($tableFilters))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::VIEW_CUSTOMER_RECEIVABLE)),
            Action::make('supplierPayables')
                ->label(__('dberp.resources.receipt_payment_query.link_supplier_payables'))
                ->url(DberpSettlementEntryFilamentSupport::supplierPayablesUrlFromReceiptPaymentTableFilters($tableFilters))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::VIEW_SUPPLIER_PAYABLE)),
        ];
    }
}
