<?php

declare(strict_types=1);

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,
        ]);
    }
}
