<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Support\PluginIntegration;

use App\Models\DberpCustomer;
use App\Models\DberpSalesOrder;
use Filament\Actions\Action;

final class InvoiceAssistantFilamentBridge
{
    private const INTEGRATION_CLASS = 'Loongdom\DberpInvoiceAssistant\Filament\InvoiceAssistantHostFilamentIntegration';

    /**
     * @return list<class-string>
     */
    public static function customerRelationManagers(): array
    {
        return self::call('customerRelationManagers', []);
    }

    /**
     * @return list<class-string>
     */
    public static function salesOrderRelationManagers(): array
    {
        return self::call('salesOrderRelationManagers', []);
    }

    /**
     * @return list<Action>
     */
    public static function customerEditHeaderActions(DberpCustomer $record): array
    {
        return self::call('customerEditHeaderActions', [], $record);
    }

    /**
     * @return list<Action>
     */
    public static function salesOrderEditHeaderActions(DberpSalesOrder $record): array
    {
        return self::call('salesOrderEditHeaderActions', [], $record);
    }

    /**
     * @return list<array{label: string, value: string}>
     */
    public static function salesOrderOverviewHeaderItems(?DberpSalesOrder $salesOrder): array
    {
        return self::call('salesOrderOverviewHeaderItems', [], $salesOrder);
    }

    /**
     * @return list<array<string, mixed>>
     */
    public static function salesOrderOverviewInvoiceRows(DberpSalesOrder $salesOrder): array
    {
        return self::call('salesOrderOverviewInvoiceRows', [], $salesOrder);
    }

    public static function salesOrderOverviewInvoicesVisible(?DberpSalesOrder $salesOrder): bool
    {
        return self::call('salesOrderOverviewInvoicesVisible', false, $salesOrder);
    }

    /**
     * @return array{
     *     visible: bool,
     *     disabled_hint: ?string,
     *     permission_hint: ?string,
     *     header: list<array{label: string, value: string}>,
     *     recent_invoices: list<array<string, mixed>>,
     *     quick_links: list<array{label: string, url: string}>,
     * }
     */
    public static function customerOverviewInvoiceData(?DberpCustomer $customer): array
    {
        return self::call('customerOverviewInvoiceData', self::emptyCustomerOverviewInvoiceData(), $customer);
    }

    /**
     * @param  array<string, mixed>  $tableFilters
     * @return list<Action>
     */
    public static function receivableListHeaderActions(array $tableFilters = []): array
    {
        return self::call('receivableListHeaderActions', [], $tableFilters);
    }

    /**
     * @param  array<string, mixed>  $tableFilters
     * @return list<Action>
     */
    public static function settlementQueryHeaderActions(array $tableFilters = []): array
    {
        return self::call('settlementQueryHeaderActions', [], $tableFilters);
    }

    /**
     * @return array{
     *     visible: bool,
     *     disabled_hint: ?string,
     *     permission_hint: ?string,
     *     header: list<array{label: string, value: string}>,
     *     recent_invoices: list<array<string, mixed>>,
     *     quick_links: list<array{label: string, url: string}>,
     * }
     */
    private static function emptyCustomerOverviewInvoiceData(): array
    {
        return [
            'visible' => false,
            'disabled_hint' => null,
            'permission_hint' => null,
            'header' => [],
            'recent_invoices' => [],
            'quick_links' => [],
        ];
    }

    private static function call(string $method, mixed $default, mixed ...$arguments): mixed
    {
        return OptionalPluginClassDelegate::call(
            self::INTEGRATION_CLASS,
            static fn (string $integrationClass): mixed => $integrationClass::$method(...$arguments),
            $default,
        );
    }
}
