const std = @import("std");
const ResolvedTarget = std.Build.ResolvedTarget;
const OptimizeMode = std.Build.OptimizeMode;
const Module = std.Build.Module;

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

    var api: *Module = undefined;
    {
        const s2s_dep = b.dependency("s2s", .{
            .target = target,
            .optimize = optimize,
        });

        api = b.createModule(.{
            .root_source_file = b.path("api/root.zig"),
            .target = target,
            .optimize = optimize,
            .link_libc = true,
        });
        api.addIncludePath(b.path("api/date_time"));
        api.addCSourceFile(.{ .file = b.path("api/date_time/timegm.c") });

        api.addIncludePath(b.path("api/stb_image"));
        api.addCSourceFile(.{ .file = b.path("api/stb_image/stb_image.c") });

        api.addImport("s2s", s2s_dep.module("s2s"));
    }

    var lib: *Module = undefined;
    {
        lib = b.createModule(.{
            .root_source_file = b.path("lib/root.zig"),
            .target = target,
            .optimize = optimize,
        });
        lib.addImport("artifacts-api", api);

        const unit_tests = b.addTest(.{
            .root_source_file = b.path("lib/root.zig"),
            .target = target,
            .optimize = optimize,
        });

        const run_unit_tests = b.addRunArtifact(unit_tests);
        const test_step = b.step("test-lib", "Run lib unit tests");
        test_step.dependOn(&run_unit_tests.step);
    }

    {
        const cli = b.addExecutable(.{
            .name = "artificer",
            .root_source_file = b.path("cli/main.zig"),
            .target = target,
            .optimize = optimize,
        });
        cli.root_module.addImport("artificer", lib);
        cli.root_module.addImport("artifacts-api", api);
        b.installArtifact(cli);

        const run_cmd = b.addRunArtifact(cli);
        run_cmd.step.dependOn(b.getInstallStep());

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

        const run_step = b.step("run-cli", "Run the CLI");
        run_step.dependOn(&run_cmd.step);

        const build_step = b.step("build-cli", "Build the CLI");
        build_step.dependOn(&b.addInstallArtifact(cli, .{}).step);
    }

    {
        const raylib_dep = b.dependency("raylib-zig", .{
            .target = target,
            .optimize = optimize,
        });

        const gui = b.addExecutable(.{
            .name = "artificer-gui",
            .root_source_file = b.path("gui/main.zig"),
            .target = target,
            .optimize = optimize,
        });
        gui.linkLibrary(raylib_dep.artifact("raylib"));

        gui.root_module.addImport("artifacts-api", api);
        gui.root_module.addImport("artificer", lib);
        gui.root_module.addImport("raylib", raylib_dep.module("raylib"));

        const run_cmd = b.addRunArtifact(gui);
        if (b.args) |args| {
            run_cmd.addArgs(args);
        }

        const build_step = b.step("build-gui", "Build the GUI");
        build_step.dependOn(&b.addInstallArtifact(gui, .{}).step);

        const run_step = b.step("run-gui", "Run the GUI");
        run_step.dependOn(&run_cmd.step);
    }
}
