<?php

declare(strict_types=1);

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