<?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\DberpPaymentOrders\DberpPaymentOrderResource;
use App\Filament\Resources\DberpReceiptOrders\DberpReceiptOrderResource;
use App\Models\DberpReceiptPaymentQuery;

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

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

    public static function editUrlOrNull(?DberpReceiptPaymentQuery $record): ?string
    {
        if ($record === null) {
            return null;
        }

        return self::editUrl($record);
    }

    public static function hasEditUrl(?DberpReceiptPaymentQuery $record): bool
    {
        return self::editUrlOrNull($record) !== null;
    }

    public static function editUrl(DberpReceiptPaymentQuery $record): ?string
    {
        $recordId = self::sourceRecordId($record);

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

        return match ((string) $record->direction) {
            'receipt' => DberpReceiptOrderResource::getUrl('edit', ['record' => $recordId]),
            'payment' => DberpPaymentOrderResource::getUrl('edit', ['record' => $recordId]),
            default => null,
        };
    }
}
