const std = @import("std");
const llvm = @import("../../llvm_api.zig");
const c = llvm.c;
const errors = @import("../../errors.zig");
const emit = @import("../../ir/emit_llvm.zig");
const ir_inst = @import("../../ir/inst.zig");
const ir_types = @import("../../ir/types.zig");

const LLVMEmitter = emit.LLVMEmitter;
const Inst = ir_inst.Inst;
const TypeId = ir_types.TypeId;
const StringId = ir_types.StringId;

/// Reflection metadata + trace-frame emission (architecture phase A7.2),
/// extracted from `LLVMEmitter`. A backend `*LLVMEmitter` facade (field `e`):
/// the type/field/tag reflection NAME-ARRAY builders (memoized into
/// `type_name_array`/`field_name_arrays`/`tag_name_array` on `LLVMEmitter`) and
/// the error-trace `Frame` builders. Reads cached LLVM handles / the IR type
/// table / the module via `self.e.*`; the memoizing composite getters
/// (`getStringStructType`/`getFrameStructType`) + `emitFieldValueGet` stay on
/// `LLVMEmitter`. Entry points are reached via `self.reflection()`.
pub const Reflection = struct {
    e: *LLVMEmitter,

    /// Lazy global `[N x string]` indexed by `TypeId.index()`, holding each
    /// type's display name. Built on the first dynamic `type_name(t)` call site.
    pub fn getOrBuildTypeNameArray(self: Reflection) c.LLVMValueRef {
        if (self.e.type_name_array) |g| return g;

        const n: u32 = @intCast(self.e.ir_mod.types.infos.items.len);
        const string_ty = self.e.getStringStructType();

        var field_vals = std.ArrayList(c.LLVMValueRef).empty;
        defer field_vals.deinit(self.e.alloc);
        var i: u32 = 0;
        while (i < n) : (i += 1) {
            const tid = TypeId.fromIndex(i);
            const name_str = self.e.ir_mod.types.formatTypeName(self.e.alloc, tid);
            const str_z = self.e.alloc.dupeZ(u8, name_str) catch unreachable;
            defer self.e.alloc.free(str_z);
            const global_str = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(name_str.len + 1)), "tn.str");
            c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(name_str.len + 1), 1));
            c.LLVMSetGlobalConstant(global_str, 1);
            c.LLVMSetLinkage(global_str, c.LLVMPrivateLinkage);
            const len_val = c.LLVMConstInt(self.e.cached_i64, name_str.len, 0);
            var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
            const const_struct = c.LLVMConstStructInContext(self.e.context, &struct_fields, 2, 0);
            field_vals.append(self.e.alloc, const_struct) catch unreachable;
        }

        const arr_ty = c.LLVMArrayType(string_ty, n);
        const arr_init = c.LLVMConstArray(string_ty, field_vals.items.ptr, n);
        const global = c.LLVMAddGlobal(self.e.llvm_module, arr_ty, "__sx_type_names");
        c.LLVMSetInitializer(global, arr_init);
        c.LLVMSetGlobalConstant(global, 1);
        c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);

        self.e.type_name_array = global;
        self.e.type_name_array_len = n;
        return global;
    }

    /// Lazy global `[N x i1]` indexed by `TypeId.index()`: 1 where the type is
    /// an unsigned integer. Built on the first dynamic `type_is_unsigned(t)`
    /// call site; the runtime arm GEPs in at the boxed TypeId and loads the bit.
    /// Derives every entry from `TypeTable.isUnsignedInt` — the single
    /// signedness source-of-truth, so no per-index magic lives in the emitter.
    pub fn getOrBuildTypeIsUnsignedArray(self: Reflection) c.LLVMValueRef {
        if (self.e.type_is_unsigned_array) |g| return g;

        const n: u32 = @intCast(self.e.ir_mod.types.infos.items.len);
        var field_vals = std.ArrayList(c.LLVMValueRef).empty;
        defer field_vals.deinit(self.e.alloc);
        var i: u32 = 0;
        while (i < n) : (i += 1) {
            const tid = TypeId.fromIndex(i);
            const bit: u64 = if (self.e.ir_mod.types.isUnsignedInt(tid)) 1 else 0;
            field_vals.append(self.e.alloc, c.LLVMConstInt(self.e.cached_i1, bit, 0)) catch unreachable;
        }

        const arr_ty = c.LLVMArrayType(self.e.cached_i1, n);
        const arr_init = c.LLVMConstArray(self.e.cached_i1, field_vals.items.ptr, n);
        const global = c.LLVMAddGlobal(self.e.llvm_module, arr_ty, "__sx_type_is_unsigned");
        c.LLVMSetInitializer(global, arr_init);
        c.LLVMSetGlobalConstant(global, 1);
        c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);

        self.e.type_is_unsigned_array = global;
        self.e.type_is_unsigned_array_len = n;
        return global;
    }

    /// Build (or return cached) a global constant array of {ptr, i64} string values
    /// for the field names of a struct type.
    pub fn getOrBuildFieldNameArray(self: Reflection, struct_type: TypeId) c.LLVMValueRef {
        if (self.e.field_name_arrays.get(struct_type.index())) |g| return g;

        const info = self.e.ir_mod.types.get(struct_type);

        // Collect name StringIds from struct fields, union fields, or enum variants
        var name_ids = std.ArrayList(StringId).empty;
        defer name_ids.deinit(self.e.alloc);
        switch (info) {
            .@"struct" => |s| {
                for (s.fields) |f| name_ids.append(self.e.alloc, f.name) catch unreachable;
            },
            .@"union" => |u| {
                for (u.fields) |f| name_ids.append(self.e.alloc, f.name) catch unreachable;
            },
            .tagged_union => |u| {
                for (u.fields) |f| name_ids.append(self.e.alloc, f.name) catch unreachable;
            },
            .@"enum" => |e| {
                for (e.variants) |v| name_ids.append(self.e.alloc, v) catch unreachable;
            },
            else => {},
        }

        const string_ty = self.e.getStringStructType();
        const n: u32 = @intCast(name_ids.items.len);

        // Build constant initializer: [N x {ptr, i64}]
        var field_vals = std.ArrayList(c.LLVMValueRef).empty;
        defer field_vals.deinit(self.e.alloc);
        for (name_ids.items) |name_id| {
            const name_str = self.e.ir_mod.types.getString(name_id);
            const str_z = self.e.alloc.dupeZ(u8, name_str) catch unreachable;
            defer self.e.alloc.free(str_z);
            const global_str = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(name_str.len + 1)), "fld.str");
            c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(name_str.len + 1), 1));
            c.LLVMSetGlobalConstant(global_str, 1);
            c.LLVMSetLinkage(global_str, c.LLVMPrivateLinkage);
            // Build fat pointer {ptr, len} as constant struct
            const len_val = c.LLVMConstInt(self.e.cached_i64, name_str.len, 0);
            var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
            const const_struct = c.LLVMConstStructInContext(self.e.context, &struct_fields, 2, 0);
            field_vals.append(self.e.alloc, const_struct) catch unreachable;
        }

        // Create global array [N x {ptr, i64}]
        const array_ty = c.LLVMArrayType(string_ty, n);
        const array_init = c.LLVMConstArray(string_ty, field_vals.items.ptr, n);
        const global = c.LLVMAddGlobal(self.e.llvm_module, array_ty, "field_names");
        c.LLVMSetInitializer(global, array_init);
        c.LLVMSetGlobalConstant(global, 1);
        c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);

        self.e.field_name_arrays.put(struct_type.index(), global) catch unreachable;
        return global;
    }

    /// The always-linked tag-name table: a `[N x {ptr, i64}]` global of tag
    /// names indexed by global tag id (the `TagRegistry` namespace; slot 0 is
    /// the reserved "" no-error name). `error_tag_name_get` GEPs into it at the
    /// runtime tag id. Built once per module. Always emitted (not trace-gated)
    /// so `{}` interpolation of an error tag works even in release builds.
    pub fn getOrBuildTagNameArray(self: Reflection) c.LLVMValueRef {
        if (self.e.tag_name_array) |g| return g;

        const string_ty = self.e.getStringStructType();
        const names = self.e.ir_mod.types.tags.names.items;

        var field_vals = std.ArrayList(c.LLVMValueRef).empty;
        defer field_vals.deinit(self.e.alloc);
        for (names) |name_str| {
            const str_z = self.e.alloc.dupeZ(u8, name_str) catch unreachable;
            defer self.e.alloc.free(str_z);
            const global_str = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(name_str.len + 1)), "tag.str");
            c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(name_str.len + 1), 1));
            c.LLVMSetGlobalConstant(global_str, 1);
            c.LLVMSetLinkage(global_str, c.LLVMPrivateLinkage);
            const len_val = c.LLVMConstInt(self.e.cached_i64, name_str.len, 0);
            var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
            const const_struct = c.LLVMConstStructInContext(self.e.context, &struct_fields, 2, 0);
            field_vals.append(self.e.alloc, const_struct) catch unreachable;
        }

        const n: u32 = @intCast(names.len);
        const array_ty = c.LLVMArrayType(string_ty, n);
        const array_init = c.LLVMConstArray(string_ty, field_vals.items.ptr, n);
        const global = c.LLVMAddGlobal(self.e.llvm_module, array_ty, "tag_names");
        c.LLVMSetInitializer(global, array_init);
        c.LLVMSetGlobalConstant(global, 1);
        c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);

        self.e.tag_name_array = global;
        return global;
    }

    /// An interned constant sx `string` (`{ ptr, i64 }`) of the cached string
    /// struct type, backed by a private NUL-terminated data global. Cached by
    /// content so a path/name shared by many push sites is emitted once.
    fn buildStringConst(self: Reflection, s: []const u8) c.LLVMValueRef {
        if (self.e.frame_str_cache.get(s)) |v| return v;
        const str_z = self.e.alloc.dupeZ(u8, s) catch unreachable;
        defer self.e.alloc.free(str_z);
        const data = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(s.len + 1)), "frame.str");
        c.LLVMSetInitializer(data, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(s.len + 1), 1));
        c.LLVMSetGlobalConstant(data, 1);
        c.LLVMSetLinkage(data, c.LLVMPrivateLinkage);
        c.LLVMSetUnnamedAddress(data, c.LLVMGlobalUnnamedAddr);
        var fields = [_]c.LLVMValueRef{ data, c.LLVMConstInt(self.e.cached_i64, s.len, 0) };
        const str_const = c.LLVMConstNamedStruct(self.e.getStringStructType(), &fields, 2);
        const key = self.e.alloc.dupe(u8, s) catch return str_const;
        self.e.frame_str_cache.put(key, str_const) catch self.e.alloc.free(key);
        return str_const;
    }

    /// Build the interned `Frame` global for a `.trace_frame` push site and
    /// return its address as `i64` (the value `sx_trace_push` stores). Resolves
    /// the instruction's span + current function to `{file,line,col,func}`. The
    /// file is shown as its basename so trace output is machine-independent
    /// (the harness passes absolute paths); full paths live in DWARF.
    pub fn emitTraceFrame(self: Reflection, instruction: *const Inst) c.LLVMValueRef {
        const file = std.fs.path.basename(self.e.current_func_file);
        const src = self.e.sourceForFile(self.e.current_func_file);
        const loc = errors.SourceLoc.compute(src, instruction.span.start);
        const func_name = self.e.ir_mod.types.getString(self.e.ir_mod.functions.items[self.e.current_func_idx].name);

        var fields = [_]c.LLVMValueRef{
            self.buildStringConst(file),
            c.LLVMConstInt(self.e.cached_i32, loc.line, 0),
            c.LLVMConstInt(self.e.cached_i32, loc.col, 0),
            self.buildStringConst(func_name),
            self.buildStringConst(errors.lineAt(src, instruction.span.start)),
        };
        const frame_ty = self.e.getFrameStructType();
        const frame_const = c.LLVMConstNamedStruct(frame_ty, &fields, 5);
        const g = c.LLVMAddGlobal(self.e.llvm_module, frame_ty, "trace.frame");
        c.LLVMSetInitializer(g, frame_const);
        c.LLVMSetGlobalConstant(g, 1);
        c.LLVMSetLinkage(g, c.LLVMPrivateLinkage);
        return c.LLVMConstPtrToInt(g, self.e.cached_i64);
    }
};
