// test_mircall_phase2_module.hako — MirCall Phase 2 tests (ModuleFunction)
// Expected: ModuleFunction calls (StringHelpers.int_to_str, etc.) work correctly

using selfhost.vm.hakorune-vm.hakorune_vm_core as HakoruneVmCore
using selfhost.shared.common.string_helpers as StringHelpers

static box Main {
  main() {
    print("=== MirCall Phase 2 Tests (ModuleFunction) ===")

    // Test 1: StringHelpers.int_to_str(42) → "42"
    // Block 0:
    //   const 42 → v1
    //   mir_call StringHelpers.int_to_str(v1) → v2
    //   ret v2
    // Note: Expected return is string length (2 chars: "42")
    local mir1 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"const","dst":1,"value":{"type":"i64","value":42}},{"op":"mir_call","dst":2,"mir_call":{"callee":{"type":"ModuleFunction","name":"StringHelpers.int_to_str"},"args":[1],"effects":[],"flags":{}}},{"op":"boxcall","dst":3,"box":2,"method":"size","args":[]},{"op":"ret","value":3}],"terminator":{"op":"ret","value":3}}]}]}"#

    print("[Test 1] StringHelpers.int_to_str(42) → '42' (length 2)")
    local result1 = HakoruneVmCore.run(mir1)
    print("Test 1 result: " + StringHelpers.int_to_str(result1))
    if result1 != 2 {
      print("[FAIL] Test 1: expected 2 (length of '42'), got " + StringHelpers.int_to_str(result1))
      return 1
    }
    print("[PASS] Test 1: StringHelpers.int_to_str works")

    // Test 2: StringHelpers.int_to_str(0) → "0"
    // Block 0:
    //   const 0 → v1
    //   mir_call StringHelpers.int_to_str(v1) → v2
    //   boxcall v2.length() → v3
    //   ret v3
    local mir2 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"const","dst":1,"value":{"type":"i64","value":0}},{"op":"mir_call","dst":2,"mir_call":{"callee":{"type":"ModuleFunction","name":"StringHelpers.int_to_str"},"args":[1],"effects":[],"flags":{}}},{"op":"boxcall","dst":3,"box":2,"method":"size","args":[]},{"op":"ret","value":3}],"terminator":{"op":"ret","value":3}}]}]}"#

    print("[Test 2] StringHelpers.int_to_str(0) → '0' (length 1)")
    local result2 = HakoruneVmCore.run(mir2)
    print("Test 2 result: " + StringHelpers.int_to_str(result2))
    if result2 != 1 {
      print("[FAIL] Test 2: expected 1 (length of '0'), got " + StringHelpers.int_to_str(result2))
      return 1
    }
    print("[PASS] Test 2: StringHelpers.int_to_str(0) works")

    // Test 3: StringHelpers.int_to_str(100) → "100"
    // Block 0:
    //   const 100 → v1
    //   mir_call StringHelpers.int_to_str(v1) → v2
    //   boxcall v2.length() → v3
    //   ret v3
    local mir3 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"const","dst":1,"value":{"type":"i64","value":100}},{"op":"mir_call","dst":2,"mir_call":{"callee":{"type":"ModuleFunction","name":"StringHelpers.int_to_str"},"args":[1],"effects":[],"flags":{}}},{"op":"boxcall","dst":3,"box":2,"method":"size","args":[]},{"op":"ret","value":3}],"terminator":{"op":"ret","value":3}}]}]}"#

    print("[Test 3] StringHelpers.int_to_str(100) → '100' (length 3)")
    local result3 = HakoruneVmCore.run(mir3)
    print("Test 3 result: " + StringHelpers.int_to_str(result3))
    if result3 != 3 {
      print("[FAIL] Test 3: expected 3 (length of '100'), got " + StringHelpers.int_to_str(result3))
      return 1
    }
    print("[PASS] Test 3: StringHelpers.int_to_str(100) works")

    // Test 4: Chain - int_to_str + print
    // Block 0:
    //   const 42 → v1
    //   mir_call StringHelpers.int_to_str(v1) → v2
    //   mir_call print(v2) → null
    //   const 0 → v3
    //   ret v3
    local mir4 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"const","dst":1,"value":{"type":"i64","value":42}},{"op":"mir_call","dst":2,"mir_call":{"callee":{"type":"ModuleFunction","name":"StringHelpers.int_to_str"},"args":[1],"effects":[],"flags":{}}},{"op":"mir_call","dst":null,"mir_call":{"callee":{"type":"Global","name":"print"},"args":[2],"effects":["IO"],"flags":{}}},{"op":"const","dst":3,"value":{"type":"i64","value":0}},{"op":"ret","value":3}],"terminator":{"op":"ret","value":3}}]}]}"#

    print("[Test 4] Chain: StringHelpers.int_to_str(42) + print (should print '42')")
    local result4 = HakoruneVmCore.run(mir4)
    print("Test 4 result: " + StringHelpers.int_to_str(result4))
    if result4 != 0 {
      print("[FAIL] Test 4: expected 0, got " + StringHelpers.int_to_str(result4))
      return 1
    }
    print("[PASS] Test 4: ModuleFunction + Global call chain works")

    print("=== All MirCall Phase 2 (ModuleFunction) tests PASSED! (4/4) ===")
    return 0
  }
}
