<?php

declare(strict_types=1);

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