<?php

declare(strict_types=1);

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

namespace App\Support;

use App\Filament\Resources\DberpOtherStockInOrders\DberpOtherStockInOrderResource;
use App\Filament\Resources\DberpOtherStockOutOrders\DberpOtherStockOutOrderResource;
use App\Filament\Resources\DberpPaymentOrders\DberpPaymentOrderResource;
use App\Filament\Resources\DberpPurchaseOrders\DberpPurchaseOrderResource;
use App\Filament\Resources\DberpPurchaseReceipts\DberpPurchaseReceiptResource;
use App\Filament\Resources\DberpPurchaseRequests\DberpPurchaseRequestResource;
use App\Filament\Resources\DberpPurchaseReturns\DberpPurchaseReturnResource;
use App\Filament\Resources\DberpReceiptOrders\DberpReceiptOrderResource;
use App\Filament\Resources\DberpSalesOrders\DberpSalesOrderResource;
use App\Filament\Resources\DberpSalesOutbounds\DberpSalesOutboundResource;
use App\Filament\Resources\DberpSalesQuotes\DberpSalesQuoteResource;
use App\Filament\Resources\DberpSalesReturns\DberpSalesReturnResource;
use App\Filament\Resources\DberpSettlementAdjustments\DberpSettlementAdjustmentResource;
use App\Filament\Resources\DberpSettlementStatements\DberpSettlementStatementResource;
use App\Filament\Resources\DberpSettlementWriteOffs\DberpSettlementWriteOffResource;
use App\Filament\Resources\DberpStocktakeOrders\DberpStocktakeOrderResource;
use App\Filament\Resources\DberpTransferOrders\DberpTransferOrderResource;
use App\Models\DberpDocumentQuery;
use Filament\Resources\Resource;

final class DberpDocumentQuerySourceUrlResolver
{
    public const int ROW_ID_SEGMENT = 1_000_000_000;

    /** @var array<string, class-string<resource>> */
    private const array RESOURCE_BY_DOCUMENT_TYPE = [
        'other_stock_in' => DberpOtherStockInOrderResource::class,
        'other_stock_out' => DberpOtherStockOutOrderResource::class,
        'transfer_order' => DberpTransferOrderResource::class,
        'stocktake_order' => DberpStocktakeOrderResource::class,
        'purchase_order' => DberpPurchaseOrderResource::class,
        'purchase_receipt' => DberpPurchaseReceiptResource::class,
        'purchase_return' => DberpPurchaseReturnResource::class,
        'sales_order' => DberpSalesOrderResource::class,
        'sales_outbound' => DberpSalesOutboundResource::class,
        'sales_return' => DberpSalesReturnResource::class,
        'receipt_order' => DberpReceiptOrderResource::class,
        'payment_order' => DberpPaymentOrderResource::class,
        'purchase_request' => DberpPurchaseRequestResource::class,
        'sales_quote' => DberpSalesQuoteResource::class,
        'settlement_write_off' => DberpSettlementWriteOffResource::class,
        'settlement_statement' => DberpSettlementStatementResource::class,
        'settlement_adjustment' => DberpSettlementAdjustmentResource::class,
    ];

    public static function sourceRecordId(DberpDocumentQuery $record): int
    {
        return (int) ($record->row_id % self::ROW_ID_SEGMENT);
    }

    public static function editUrl(DberpDocumentQuery $record): ?string
    {
        $documentType = (string) $record->document_type;
        $resourceClass = self::RESOURCE_BY_DOCUMENT_TYPE[$documentType] ?? null;

        if ($resourceClass === null) {
            return null;
        }

        $recordId = self::sourceRecordId($record);

        if ($recordId <= 0) {
            return null;
        }

        return $resourceClass::getUrl('edit', ['record' => $recordId]);
    }
}
