<?php

declare(strict_types=1);

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