<?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\Settlement\SettlementReminderService;
use App\Filament\Resources\DberpCustomerReceivables\DberpCustomerReceivableResource;
use App\Filament\Resources\DberpPaymentOrders\DberpPaymentOrderResource;
use App\Filament\Resources\DberpReceiptOrders\DberpReceiptOrderResource;
use App\Filament\Resources\DberpSupplierPayables\DberpSupplierPayableResource;
use App\Services\DberpPermissionService;
use App\Support\DberpNumberFormatter;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Facades\Auth;

class SettlementOverviewStatsWidget extends BaseWidget
{
    protected ?string $pollingInterval = null;

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

    protected function getStats(): array
    {
        $metrics = app(SettlementReminderService::class)->overviewMetrics();

        return [
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_receivable_total'), DberpNumberFormatter::money($metrics['receivable_total'] ?? null))
                ->description((string) __('dberp.widgets.settlement_workbench.link_receivables'))
                ->color('warning')
                ->url(DberpCustomerReceivableResource::getUrl('index')),
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_payable_total'), DberpNumberFormatter::money($metrics['payable_total'] ?? null))
                ->description((string) __('dberp.widgets.settlement_workbench.link_payables'))
                ->color('danger')
                ->url(DberpSupplierPayableResource::getUrl('index')),
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_due_today_receivable'), (string) $metrics['due_today_receivable_count'])
                ->description((string) __('dberp.widgets.settlement_workbench.amount_label').' '.DberpNumberFormatter::money($metrics['due_today_receivable_amount'] ?? null))
                ->color('warning')
                ->url($this->resourceUrl(DberpCustomerReceivableResource::class, 'due_today')),
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_overdue_receivable'), (string) $metrics['overdue_receivable_count'])
                ->description((string) __('dberp.widgets.settlement_workbench.amount_label').' '.DberpNumberFormatter::money($metrics['overdue_receivable_amount'] ?? null))
                ->color('danger')
                ->url($this->resourceUrl(DberpCustomerReceivableResource::class, 'overdue')),
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_due_soon_payable'), (string) $metrics['due_soon_payable_count'])
                ->description((string) __('dberp.widgets.settlement_workbench.amount_label').' '.DberpNumberFormatter::money($metrics['due_soon_payable_amount'] ?? null))
                ->color('warning')
                ->url($this->resourceUrl(DberpSupplierPayableResource::class, 'due_soon')),
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_large_unwritten'), (string) $metrics['large_unwritten_count'])
                ->description((string) __('dberp.widgets.settlement_workbench.link_receipts'))
                ->color('primary')
                ->url($this->resourceUrl(DberpReceiptOrderResource::class, 'large_unwritten')),
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_month_receipt'), DberpNumberFormatter::money($metrics['month_receipt_amount'] ?? null))
                ->description((string) __('dberp.widgets.settlement_workbench.link_receipts'))
                ->color('success')
                ->url(DberpReceiptOrderResource::getUrl('index')),
            Stat::make((string) __('dberp.widgets.settlement_workbench.stat_month_payment'), DberpNumberFormatter::money($metrics['month_payment_amount'] ?? null))
                ->description((string) __('dberp.widgets.settlement_workbench.link_payments'))
                ->color('gray')
                ->url(DberpPaymentOrderResource::getUrl('index')),
        ];
    }

    private function resourceUrl(string $resourceClass, string $attentionStatus): string
    {
        return $resourceClass::getUrl('index').'?'.http_build_query([
            'filters' => [
                'attention_status' => [
                    'value' => $attentionStatus,
                ],
            ],
        ]);
    }
}
