<?php

declare(strict_types=1);

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;
    }
}
