<?php

declare(strict_types=1);

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