<?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\Sales\SalesControlReminderService;
use App\Filament\Pages\DberpCustomerCreditControlPage;
use App\Filament\Resources\DberpCustomerPriceRules\DberpCustomerPriceRuleResource;
use App\Filament\Resources\DberpSalesOrders\DberpSalesOrderResource;
use App\Filament\Resources\DberpSalesQuotes\DberpSalesQuoteResource;
use App\Services\DberpPermissionService;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Facades\Auth;

class SalesControlOverviewStatsWidget 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(SalesControlReminderService::class)->overviewMetrics();

        return [
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_pending_quote'), (string) $metrics['pending_effective_quote_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_quotes'))
                ->color('warning')
                ->url($this->quoteUrl('pending_effective')),
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_expiring_quote'), (string) $metrics['expiring_quote_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_quotes'))
                ->color('warning')
                ->url($this->quoteUrl('expiring_soon')),
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_long_unconverted'), (string) $metrics['long_unconverted_quote_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_quotes'))
                ->color('danger')
                ->url($this->quoteUrl('long_unconverted')),
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_blocked_credit'), (string) $metrics['blocked_credit_customer_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_credit'))
                ->color('danger')
                ->url($this->creditControlUrl('blocked')),
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_near_limit'), (string) $metrics['near_limit_customer_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_credit'))
                ->color('primary')
                ->url($this->creditControlUrl('near_limit')),
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_released_order'), (string) $metrics['released_override_order_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_orders'))
                ->color('danger')
                ->url($this->salesOrderUrl('released_by_override')),
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_expiring_price'), (string) $metrics['expiring_price_rule_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_prices'))
                ->color('warning')
                ->url($this->priceRuleUrl('expiring_soon')),
            Stat::make((string) __('dberp.widgets.sales_workbench.stat_expired_price'), (string) $metrics['expired_price_rule_count'])
                ->description((string) __('dberp.widgets.sales_workbench.link_prices'))
                ->color('gray')
                ->url($this->priceRuleUrl('expired')),
        ];
    }

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

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

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

    private function creditControlUrl(string $attentionStatus): string
    {
        return DberpCustomerCreditControlPage::getUrl().'?'.http_build_query([
            'attention_status' => $attentionStatus,
        ]);
    }
}
