package com.agent.management.engine;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class VariableConverterTest {

    @Test
    void toStringConvertsPrimitivesAndCollections() {
        assertThat(VariableConverter.toString(42)).isEqualTo("42");
        assertThat(VariableConverter.toString(true)).isEqualTo("true");
        assertThat(VariableConverter.toString(List.of(1, 2))).isEqualTo("[1,2]");
    }

    @Test
    void toNumberExtractsDigits() {
        assertThat(VariableConverter.toNumber(42)).isEqualTo(42);
        assertThat(VariableConverter.toNumber("result: 3.14")).isEqualTo(3.14);
        assertThat(VariableConverter.toNumber("-7")).isEqualTo(-7L);
        assertThat(VariableConverter.toNumber("no digits")).isEqualTo(Double.NaN);
    }

    @Test
    void toBooleanFollowsTruthyRules() {
        assertThat(VariableConverter.toBoolean(true)).isTrue();
        assertThat(VariableConverter.toBoolean(0)).isFalse();
        assertThat(VariableConverter.toBoolean(List.of())).isFalse();
        assertThat(VariableConverter.toBoolean("yes")).isTrue();
    }

    @Test
    void toObjectParsesJsonAndWrapsPojo() {
        RecordPojo pojo = new RecordPojo("foo", 1);
        Map<String, Object> map = VariableConverter.toObject(pojo);
        assertThat(map).containsEntry("name", "foo").containsEntry("value", 1);
    }

    @Test
    void toArrayHandlesCollectionAndJsonString() {
        assertThat(VariableConverter.toArray(List.of("a", "b"))).containsExactly("a", "b");
        assertThat(VariableConverter.toArray("[1,2,3]")).containsExactly(1, 2, 3);
        assertThat(VariableConverter.toArray("single")).containsExactly("single");
    }

    @Test
    void convertDispatchesByType() {
        assertThat(VariableConverter.convert("123", "number")).isEqualTo(123L);
        assertThat((Boolean) VariableConverter.convert(1, "boolean")).isTrue();
        assertThat(VariableConverter.convert("[1,2]", "array")).isInstanceOf(List.class);
        assertThat(VariableConverter.convert(List.of(1, 2), "object")).isInstanceOf(Map.class);
    }

    public record RecordPojo(String name, int value) {}
}
