<?php

declare(strict_types=1);

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

namespace App\Filament\Widgets;

use App\Business\Purchase\Enums\PurchaseOrderStatus;
use App\Business\Workbench\BusinessOverviewMetricService;
use App\Filament\Resources\DberpCustomerReceivables\DberpCustomerReceivableResource;
use App\Filament\Resources\DberpInventoryWarnings\DberpInventoryWarningResource;
use App\Filament\Resources\DberpPurchaseOrders\DberpPurchaseOrderResource;
use App\Filament\Resources\DberpReceiptOrders\DberpReceiptOrderResource;
use App\Filament\Resources\DberpSalesOrders\DberpSalesOrderResource;
use App\Filament\Resources\DberpSupplierPayables\DberpSupplierPayableResource;
use App\Services\DberpPermissionService;
use Filament\Widgets\Widget;
use Illuminate\Support\Facades\Auth;

class BusinessOverviewWorkbenchWidget extends Widget
{
    protected static bool $isLazy = true;

    protected string $view = 'filament.widgets.business-overview-workbench-widget';

    protected int|string|array $columnSpan = 'full';

    public static function canView(): bool
    {
        return app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::VIEW_WORKBENCH_OVERVIEW);
    }

    /**
     * @return list<array{
     *     key: string,
     *     label: string,
     *     value: string,
     *     type: string,
     *     color: string,
     *     secondary_label: ?string,
     *     secondary_value: ?string,
     *     url: ?string,
     * }>
     */
    public function kpiCards(): array
    {
        return array_map(function (array $metric): array {
            $metric['url'] = $this->urlForKpiKey((string) $metric['key']);

            return $metric;
        }, app(BusinessOverviewMetricService::class)->topKpiMetrics());
    }

    private function urlForKpiKey(string $key): ?string
    {
        return match ($key) {
            'month_sales_amount', 'pending_delivery_count' => DberpSalesOrderResource::canViewAny()
                ? DberpSalesOrderResource::getUrl('index')
                : null,
            'month_purchase_amount', 'pending_receipt_count' => DberpPurchaseOrderResource::canViewAny()
                ? $this->pendingPurchaseOrderUrl()
                : null,
            'inventory_warning_count' => DberpInventoryWarningResource::canViewAny()
                ? DberpInventoryWarningResource::getUrl('index')
                : null,
            'receivable_amount' => DberpCustomerReceivableResource::canViewAny()
                ? DberpCustomerReceivableResource::getUrl('index')
                : null,
            'payable_amount' => DberpSupplierPayableResource::canViewAny()
                ? DberpSupplierPayableResource::getUrl('index')
                : null,
            'month_cashflow' => DberpReceiptOrderResource::canViewAny()
                ? DberpReceiptOrderResource::getUrl('index')
                : null,
            default => null,
        };
    }

    private function pendingPurchaseOrderUrl(): string
    {
        return DberpPurchaseOrderResource::getUrl('index').'?'.http_build_query([
            'filters' => [
                'status' => [
                    'value' => PurchaseOrderStatus::Confirmed->value,
                ],
            ],
        ]);
    }
}
