const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // The core is a module so tests import it by name instead of reaching across
    // directory boundaries.
    const core = b.createModule(.{
        .root_source_file = b.path("src/usage.zig"),
        .target = target,
        .optimize = optimize,
    });

    // Shared library for native hosts (Swift imports include/usage.h).
    const lib = b.addLibrary(.{
        .name = "usage",
        .linkage = .dynamic,
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/c_api.zig"),
            .target = target,
            .optimize = optimize,
            .link_libc = true,
        }),
    });
    lib.installHeader(b.path("include/usage.h"), "usage.h");
    b.installArtifact(lib);

    const static_lib = b.addLibrary(.{
        .name = "usage",
        .linkage = .static,
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/c_api.zig"),
            .target = target,
            .optimize = optimize,
            .link_libc = true,
        }),
    });
    const install_static = b.addInstallArtifact(static_lib, .{});
    b.getInstallStep().dependOn(&install_static.step);

    if (target.result.os.tag == .macos) {
        const ranlib_cmd = b.addSystemCommand(&[_][]const u8{"ranlib"});
        ranlib_cmd.addFileArg(b.getInstallStep().owner.path("zig-out/lib/libusage.a"));
        ranlib_cmd.step.dependOn(&install_static.step);
        b.getInstallStep().dependOn(&ranlib_cmd.step);
    }

    const exe = b.addExecutable(.{
        .name = "usage",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });
    b.installArtifact(exe);

    const run = b.addRunArtifact(exe);
    if (b.args) |args| run.addArgs(args);
    b.step("run", "Run the usage CLI").dependOn(&run.step);

    const test_module = b.createModule(.{
        .root_source_file = b.path("tests/usage_test.zig"),
        .target = target,
        .optimize = optimize,
    });
    test_module.addImport("usage", core);
    const tests = b.addTest(.{ .root_module = test_module });
    const run_tests = b.addRunArtifact(tests);

    // The ABI is the part native hosts consume, so it is exercised the way they call it:
    // a C program compiled with this toolchain, linked against the installed static
    // library, exercising multi-round state and recorded provider bodies.
    const smoke_module = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    smoke_module.addCSourceFile(.{ .file = b.path("tests/c_smoke.c"), .flags = &.{ "-Wall", "-Wextra" } });
    smoke_module.addIncludePath(b.path("include"));
    smoke_module.linkLibrary(static_lib);
    const smoke = b.addExecutable(.{ .name = "c_smoke", .root_module = smoke_module });
    const run_smoke = b.addRunArtifact(smoke);
    run_smoke.addFileArg(b.path("tests/fixtures/codex-usage-live.json"));

    b.step("smoke", "Compile and run the C ABI smoke test").dependOn(&run_smoke.step);
    const test_step = b.step("test", "Run core tests and the C ABI smoke test");
    test_step.dependOn(&run_tests.step);
    test_step.dependOn(&run_smoke.step);
}
