// tests/complex.rs

use axioma::prelude::*;

#[test]
fn test_complex_realworld_structure() {
    // A real-world level JSON structure containing deep nesting, mixed arrays/objects,
    // trailing commas, and empty structures.
    let engine = axioma!({
        "api_version": 2,
        "payload": {
            "users": [
                {
                    "id": "u_123",
                    "metadata": [ null, true, { "role": "admin" } ],
                    "active": false,
                },
                {}, // Empty object
                [ 1, 2, ], // Array with trailing comma
            ]
        },
        "trailing": "ok",
    });

    // ========================================================================
    // True 2D Space Mapping (Row: Depth, Col: Sibling Offset)
    // ========================================================================
    // (r, c)
    // (0, 0): "api_version"   -> (1, 0): 2
    // (0, 1): "payload"       -> (1, 1): "users"
    //                                      -> (2, 1): "id"       -> (3, 1): "u_123"
    //                                      -> (2, 2): "metadata" -> (3, 2): null
    //                                                            -> (3, 3): true
    //                                                            -> (3, 4): "role" -> (4, 4): "admin"
    //                                      -> (2, 5): "active"   -> (3, 5): false
    //                                      -> (2, 6): {} (EmptyObj)
    //                                      -> (2, 7): 1
    //                                      -> (2, 8): 2
    // (0, 9): "trailing"      -> (1, 9): "ok"
    // ========================================================================

    // 1. Shallow keys and values (c=0)
    assert_eq!(engine(0, 0).2, AxiValue::Str("api_version"));
    assert_eq!(engine(1, 0).2, AxiValue::Num("2"));

    // 2. Deep nesting progression (c=1)
    assert_eq!(engine(0, 1).2, AxiValue::Str("payload"));
    assert_eq!(engine(1, 1).2, AxiValue::Str("users"));
    assert_eq!(engine(2, 1).2, AxiValue::Str("id"));
    assert_eq!(engine(3, 1).2, AxiValue::Str("u_123"));

    // 3. Null and boolean inside an array (c=2, c=3)
    assert_eq!(engine(2, 2).2, AxiValue::Str("metadata"));
    assert_eq!(engine(3, 2).2, AxiValue::Null);
    assert_eq!(engine(3, 3).2, AxiValue::Bool(true));

    // 4. Object inside an array (c=4)
    let (k_role, _, v_role) = engine(3, 4);
    assert_eq!(k_role, 1); // 1 = Object Key
    assert_eq!(v_role, AxiValue::Str("role"));
    assert_eq!(engine(4, 4).2, AxiValue::Str("admin")); // Depth r=4

    // 5. Continuation of the original object (c=5)
    assert_eq!(engine(2, 5).2, AxiValue::Str("active"));
    assert_eq!(engine(3, 5).2, AxiValue::Bool(false));

    // 6. Empty object inside an array (c=6)
    let (k_empty, _, v_empty) = engine(2, 6);
    assert_eq!(k_empty, 4); // 4 = EmptyObj
    assert_eq!(v_empty, AxiValue::Null);

    // 7. Primitives inside a nested array (c=7, c=8)
    // Note: Array items do not increase r (depth), they align horizontally (c)
    let (k_1, _, v_1) = engine(2, 7);
    assert_eq!(k_1, 3); // 3 = Array Item
    assert_eq!(v_1, AxiValue::Num("1"));

    let (k_2, _, v_2) = engine(2, 8);
    assert_eq!(k_2, 3);
    assert_eq!(v_2, AxiValue::Num("2"));

    // 8. Trailing element at the root level (c=9)
    assert_eq!(engine(0, 9).2, AxiValue::Str("trailing"));
    assert_eq!(engine(1, 9).2, AxiValue::Str("ok"));
}
