const std = @import("std");

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

    const assets = b.addInstallDirectory(.{
        .source_dir = .{ .src_path = .{ .owner = b, .sub_path = "assets" } },
        .install_dir = .bin,
        .install_subdir = "assets",
    });

    // const sdl3 = b.dependency("sdl3", .{
    //     .target = target,
    //     .optimize = .Debug,

    //     // Lib options.
    //     // .callbacks = false,
    //     // .ext_image = false,
    //     // .ext_net = false,
    //     // .ext_ttf = false,
    //     // .log_message_stack_size = 1024,
    //     // .main = false,
    //     // .renderer_debug_text_stack_size = 1024,

    //     // Options passed directly to https://github.com/castholm/SDL (SDL3 C Bindings):
    //     // .c_sdl_preferred_linkage = .static,
    //     // .c_sdl_strip = false,
    //     // .c_sdl_sanitize_c = .off,
    //     // .c_sdl_lto = .none,
    //     // .c_sdl_emscripten_pthreads = false,
    //     // .c_sdl_install_build_config_h = false,

    //     // Options if `ext_image` is enabled:
    //     // .image_enable_bmp = true,
    //     // .image_enable_gif = true,
    //     // .image_enable_jpg = true,
    //     // .image_enable_lbm = true,
    //     // .image_enable_pcx = true,
    //     // .image_enable_png = true,
    //     // .image_enable_pnm = true,
    //     // .image_enable_qoi = true,
    //     // .image_enable_svg = true,
    //     // .image_enable_tga = true,
    //     // .image_enable_xcf = true,
    //     // .image_enable_xpm = true,
    //     // .image_enable_xv = true,
    // });

    const shared = b.addModule("shared", .{
        .root_source_file = b.path("src/shared/shared.zig"),
        .target = target,
    });

    const client = b.addExecutable(.{
        .name = "client",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/client/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "shared", .module = shared },
            },
        }),
    });

    const server = b.addExecutable(.{
        .name = "server",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/server/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "shared", .module = shared },
            },
        }),
    });

    const zmath = b.dependency("zmath", .{});
    client.root_module.addImport("zmath", zmath.module("root"));
    server.root_module.addImport("zmath", zmath.module("root"));
    shared.addImport("zmath", zmath.module("root"));

    const znet_dep = b.dependency("znet", .{
        .target = target,
        .optimize = optimize,
    });
    const znet_mod = znet_dep.module("znet");
    const znet_artifact = znet_dep.artifact("znet");
    client.root_module.addImport("znet", znet_mod);
    client.linkLibrary(znet_artifact);
    server.root_module.addImport("znet", znet_mod);
    server.linkLibrary(znet_artifact);

    const bufzilla = b.dependency("bufzilla", .{});
    client.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));
    server.root_module.addImport("bufzilla", bufzilla.module("bufzilla"));

    const raylib_dep = b.dependency("raylib_zig", .{
        .target = target,
        .optimize = optimize,
    });

    const raylib = raylib_dep.module("raylib"); // main raylib module
    const raygui = raylib_dep.module("raygui"); // raygui module
    const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library

    client.linkLibrary(raylib_artifact);
    client.root_module.addImport("raylib", raylib);
    client.root_module.addImport("raygui", raygui);

    b.installArtifact(client);
    b.installArtifact(server);

    b.getInstallStep().dependOn(&assets.step);

    const run_step = b.step("run", "Run the app");

    const run_cmd_client = b.addRunArtifact(client);
    const run_cmd_server = b.addRunArtifact(server);
    run_step.dependOn(&run_cmd_client.step);
    run_step.dependOn(&run_cmd_server.step);

    run_cmd_client.step.dependOn(b.getInstallStep());
    run_cmd_server.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd_client.addArgs(args);
    }

    // Creates an executable that will run `test` blocks from the provided module.
    // Here `mod` needs to define a target, which is why earlier we made sure to
    // set the releative field.
    // const mod_tests = b.addTest(.{
    //     .root_module = mod,
    // });

    // A run step that will run the test executable.
    // const run_mod_tests = b.addRunArtifact(mod_tests);

    // Creates an executable that will run `test` blocks from the executable's
    // root module. Note that test executables only test one module at a time,
    // hence why we have to create two separate ones.
    // const exe_tests = b.addTest(.{
    //     .root_module = exe.root_module,
    // });

    // A run step that will run the second test executable.
    // const run_exe_tests = b.addRunArtifact(exe_tests);

    // A top level step for running all tests. dependOn can be called multiple
    // times and since the two run steps do not depend on one another, this will
    // make the two of them run in parallel.
    // const test_step = b.step("test", "Run tests");
    // test_step.dependOn(&run_mod_tests.step);
    // test_step.dependOn(&run_exe_tests.step);

    // Just like flags, top level steps are also listed in the `--help` menu.
    //
    // The Zig build system is entirely implemented in userland, which means
    // that it cannot hook into private compiler APIs. All compilation work
    // orchestrated by the build system will result in other Zig compiler
    // subcommands being invoked with the right flags defined. You can observe
    // these invocations when one fails (or you pass a flag to increase
    // verbosity) to validate assumptions and diagnose problems.
    //
    // Lastly, the Zig build system is relatively simple and self-contained,
    // and reading its source code will allow you to master it.
}
