const std = @import("std");
const pi = @import("program_index.zig");
const ProgramIndex = pi.ProgramIndex;
const ast = @import("../ast.zig");
const types = @import("types.zig");
const inst = @import("inst.zig");

test "ProgramIndex.init starts empty with unset borrowed views" {
    var idx = ProgramIndex.init(std.testing.allocator);
    defer idx.deinit();
    try std.testing.expectEqual(@as(u32, 0), idx.import_flags.count());
    try std.testing.expect(idx.module_scopes == null);
    try std.testing.expect(idx.import_graph == null);
}

test "ProgramIndex.import_flags round-trips imported vs local" {
    var idx = ProgramIndex.init(std.testing.allocator);
    defer idx.deinit();
    try idx.import_flags.put("printf", true);
    try idx.import_flags.put("main", false);
    try std.testing.expectEqual(@as(?bool, true), idx.import_flags.get("printf"));
    try std.testing.expectEqual(@as(?bool, false), idx.import_flags.get("main"));
    try std.testing.expect(idx.import_flags.get("absent") == null);
}

test "ProgramIndex borrows module_scopes / import_graph without owning them" {
    const ScopeSet = std.StringHashMap(std.StringHashMap(void));
    var scopes = ScopeSet.init(std.testing.allocator);
    defer scopes.deinit();
    var graph = ScopeSet.init(std.testing.allocator);
    defer graph.deinit();

    var idx = ProgramIndex.init(std.testing.allocator);
    defer idx.deinit();
    idx.module_scopes = &scopes;
    idx.import_graph = &graph;

    // Reads go through the borrowed pointer; the backing stays caller-owned,
    // so idx.deinit() must not free it (testing.allocator would flag a
    // double-free / leak otherwise).
    try std.testing.expect(idx.module_scopes.? == &scopes);
    try std.testing.expect(idx.import_graph.? == &graph);
    try std.testing.expectEqual(@as(u32, 0), idx.module_scopes.?.count());
}

test "ProgramIndex declaration maps round-trip (A1.1b)" {
    var idx = ProgramIndex.init(std.testing.allocator);
    defer idx.deinit();

    // Minimal AST node reused wherever a *Node is required.
    var blk = ast.Node{ .span = .{ .start = 0, .end = 0 }, .data = .{ .block = .{ .stmts = &.{} } } };

    // fn_ast_map: function name → AST decl.
    const fd = ast.FnDecl{ .name = "main", .params = &.{}, .return_type = null, .body = &blk };
    try idx.fn_ast_map.put("main", &fd);
    try std.testing.expect(idx.fn_ast_map.get("main").? == &fd);

    // type_alias_map: alias name → target TypeId.
    try idx.type_alias_map.put("ShaderHandle", .s64);
    try std.testing.expectEqual(@as(?types.TypeId, .s64), idx.type_alias_map.get("ShaderHandle"));

    // global_names: #run global name → GlobalInfo.
    try idx.global_names.put("g", .{ .id = inst.GlobalId.fromIndex(0), .ty = .s64 });
    try std.testing.expect(idx.global_names.get("g").?.id == inst.GlobalId.fromIndex(0));

    // module_const_map: const name → ModuleConstInfo.
    try idx.module_const_map.put("AF_INET", .{ .value = &blk, .ty = .s32 });
    try std.testing.expect(idx.module_const_map.get("AF_INET").?.value == &blk);

    // foreign_class_map: sx alias → ForeignClassDecl.
    const fcd = ast.ForeignClassDecl{
        .name = "NSString",
        .foreign_path = "NSString",
        .runtime = .objc_class,
        .members = &.{},
        .is_foreign = true,
        .is_main = false,
    };
    try idx.foreign_class_map.put("NSString", &fcd);
    try std.testing.expect(idx.foreign_class_map.get("NSString").? == &fcd);

    // protocol_decl_map: protocol name → ProtocolDeclInfo.
    try idx.protocol_decl_map.put("Show", .{ .name = "Show", .is_inline = false, .methods = &.{} });
    try std.testing.expectEqualStrings("Show", idx.protocol_decl_map.get("Show").?.name);

    // protocol_ast_map: protocol name → AST decl.
    const pd = ast.ProtocolDecl{ .name = "Show", .methods = &.{} };
    try idx.protocol_ast_map.put("Show", &pd);
    try std.testing.expect(idx.protocol_ast_map.get("Show").? == &pd);

    // struct_template_map: generic struct name → template.
    try idx.struct_template_map.put("List", .{ .name = "List", .type_params = &.{}, .field_names = &.{}, .field_type_nodes = &.{} });
    try std.testing.expectEqualStrings("List", idx.struct_template_map.get("List").?.name);

    // ufcs_alias_map: alias name → target function name.
    try idx.ufcs_alias_map.put("len", "list_len");
    try std.testing.expectEqualStrings("list_len", idx.ufcs_alias_map.get("len").?);
}
