<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Support;

use App\Business\Report\InventoryReportService;
use App\Filament\Resources\DberpInventoryBalances\DberpInventoryBalanceResource;
use App\Filament\Resources\DberpInventoryTransactions\DberpInventoryTransactionResource;
use App\Models\DberpInventoryBalance;

final class DberpInventorySummaryFilamentSupport
{
    /**
     * @param  array<string, mixed>  $tableFilters
     */
    public static function inventoryTransactionIndexUrlFromTableState(array $tableFilters): string
    {
        return DberpFilamentListPageUrl::append(
            DberpInventoryTransactionResource::getUrl('index'),
            self::tableFiltersForInventoryTransactionIndex($tableFilters),
        );
    }

    /**
     * @param  array<string, mixed>  $tableFilters
     */
    public static function inventoryTransactionIndexUrl(DberpInventoryBalance $record, array $tableFilters = []): string
    {
        return DberpFilamentListPageUrl::append(
            DberpInventoryTransactionResource::getUrl('index'),
            self::tableFiltersForInventoryTransactionIndex($tableFilters, $record),
        );
    }

    /**
     * @param  array<string, mixed>  $tableFilters
     */
    public static function inventoryBalanceIndexUrl(DberpInventoryBalance $record, array $tableFilters = []): string
    {
        return DberpFilamentListPageUrl::append(
            DberpInventoryBalanceResource::getUrl('index'),
            self::tableFiltersForInventoryBalanceIndex($tableFilters, $record),
        );
    }

    /**
     * @param  array<string, mixed>  $tableFilters
     * @return array<string, mixed>
     */
    public static function tableFiltersForInventoryTransactionIndex(array $tableFilters, ?DberpInventoryBalance $record = null): array
    {
        $parsed = InventoryReportService::filtersFromTableState($tableFilters);
        $filters = [];

        $warehouseId = (int) ($parsed['warehouse_id'] ?? 0);
        if ($warehouseId <= 0 && $record !== null) {
            $warehouseId = (int) $record->warehouse_id;
        }
        if ($warehouseId > 0) {
            $filters['warehouse_id'] = ['value' => $warehouseId];
        }

        $productId = (int) ($parsed['product_id'] ?? 0);
        if ($productId <= 0 && $record !== null) {
            $productId = (int) $record->product_id;
        }
        if ($productId > 0) {
            $filters['product_id'] = ['value' => $productId];
        }

        $productSkuId = (int) ($parsed['product_sku_id'] ?? 0);
        if ($productSkuId <= 0 && $record !== null) {
            $attributeSkuId = $record->getAttribute('product_sku_id');
            $productSkuId = $attributeSkuId !== null ? (int) $attributeSkuId : 0;
        }
        if ($productSkuId > 0) {
            $filters['product_sku_id'] = ['value' => $productSkuId];
        }

        if (! empty($parsed['business_time_from']) || ! empty($parsed['business_time_until'])) {
            $filters['business_time'] = array_filter([
                'from' => $parsed['business_time_from'] ?? null,
                'until' => $parsed['business_time_until'] ?? null,
            ]);
        }

        return $filters;
    }

    /**
     * @param  array<string, mixed>  $tableFilters
     * @return array<string, mixed>
     */
    private static function tableFiltersForInventoryBalanceIndex(array $tableFilters, DberpInventoryBalance $record): array
    {
        $parsed = InventoryReportService::filtersFromTableState($tableFilters);
        $filters = [
            'product_id' => ['value' => (int) $record->product_id],
        ];

        $warehouseId = (int) ($parsed['warehouse_id'] ?? 0);
        if ($warehouseId <= 0) {
            $warehouseId = (int) $record->warehouse_id;
        }
        if ($warehouseId > 0) {
            $filters['warehouse_id'] = ['value' => $warehouseId];
        }

        $productSkuId = $record->getAttribute('product_sku_id');
        if ($productSkuId !== null && (int) $productSkuId > 0) {
            $filters['product_sku_id'] = ['value' => (int) $productSkuId];
        }

        if (($parsed['category_id'] ?? 0) > 0) {
            $filters['category_id'] = ['value' => (int) $parsed['category_id']];
        }

        return $filters;
    }
}
