<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpCustomerSettlementQueries\Pages;

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

    public function getSubheading(): string|Htmlable|null
    {
        return __('dberp.resources.customer_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('customerReceivables')
                ->label(__('dberp.resources.customer_settlement_query.link_customer_receivables'))
                ->url(DberpSettlementEntryFilamentSupport::customerReceivablesUrl($tableFilters))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::VIEW_CUSTOMER_RECEIVABLE)),
            Action::make('receiptPaymentQuery')
                ->label(__('dberp.resources.customer_settlement_query.link_receipt_payment_query'))
                ->url(DberpSettlementEntryFilamentSupport::receiptPaymentQueryUrl($tableFilters, 'receipt'))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_CUSTOMER_RECEIVABLE,
                    DberpPermissionService::CREATE_RECEIPT_ORDER,
                    DberpPermissionService::VIEW_WORKBENCH_OVERVIEW,
                ])),
            Action::make('settlementWriteOffs')
                ->label(__('dberp.resources.customer_settlement_query.link_write_offs'))
                ->url(DberpSettlementEntryFilamentSupport::settlementWriteOffsUrl($tableFilters, 'receivable'))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::WRITE_OFF)),
            Action::make('settlementStatements')
                ->label(__('dberp.resources.customer_settlement_query.link_statements'))
                ->url(DberpSettlementEntryFilamentSupport::settlementStatementsUrl($tableFilters, 'receivable'))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_SETTLEMENT_STATEMENT,
                    DberpPermissionService::CONFIRM_SETTLEMENT_STATEMENT,
                ])),
        ];
    }
}
