package cz.moneta.test.harness.messaging;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;

/**
 * Wrapper for extracted JSON path values.
 * Provides fluent methods for value extraction and conversion.
 */
public class JsonPathValue {

    private final JsonNode node;
    private final String rawValue;

    public JsonPathValue(JsonNode node) {
        this.node = node;
        this.rawValue = node != null ? node.asText() : null;
    }

    public JsonPathValue(String rawValue) {
        this.node = null;
        this.rawValue = rawValue;
    }

    /**
     * Get the value as a string.
     */
    public String asText() {
        if (node != null && !(node instanceof NullNode)) {
            return node.asText();
        }
        return rawValue;
    }

    /**
     * Get the value as an integer.
     */
    public int asInt() {
        if (node != null && !(node instanceof NullNode)) {
            return node.asInt();
        }
        return Integer.parseInt(rawValue);
    }

    /**
     * Get the value as a long.
     */
    public long asLong() {
        if (node != null && !(node instanceof NullNode)) {
            return node.asLong();
        }
        return Long.parseLong(rawValue);
    }

    /**
     * Get the value as a boolean.
     */
    public boolean asBoolean() {
        if (node != null && !(node instanceof NullNode)) {
            return node.asBoolean();
        }
        return Boolean.parseBoolean(rawValue);
    }

    /**
     * Check if the value is null or missing.
     */
    public boolean isNull() {
        return node == null || node instanceof NullNode || rawValue == null;
    }

    /**
     * Get the underlying JsonNode.
     */
    public JsonNode getNode() {
        return node;
    }

    @Override
    public String toString() {
        return asText();
    }
}
