const std = @import("std");
const cli = @import("../../main.zig");
const native_hash = @import("../../native/hash.zig");
const ui = @import("../../ui/ui.zig");
const colors = @import("../../ui/colors.zig");

pub const name = "dataset hash";
pub const description = "Hash a dataset directory using native SHA256 library";

pub fn run(allocator: std.mem.Allocator, args: []const []const u8) !void {
    // Parse arguments
    if (args.len < 1) {
        try ui.printHelp(name, description, &.{
            .{ "<path>", "Path to dataset directory" },
        });
        return;
    }

    const path = args[0];

    // Check if native library is available
    if (!native_hash.hasSimdSha256()) {
        colors.printWarning("SIMD SHA256 not available, using generic implementation\n", .{});
    } else {
        const impl_name = native_hash.getSimdImplName();
        colors.printInfo("Using {s} SHA256 implementation\n", .{impl_name});
    }

    // Hash the directory
    colors.printInfo("Hashing dataset at: {s}\n", .{path});
    
    const hash = native_hash.hashDirectory(allocator, path) catch |err| {
        switch (err) {
            error.ContextInitFailed => {
                colors.printError("Failed to initialize native hash context\n", .{});
            },
            error.HashFailed => {
                colors.printError("Hash computation failed\n", .{});
            },
            error.InvalidPath => {
                colors.printError("Invalid path: {s}\n", .{path});
            },
            error.OutOfMemory => {
                colors.printError("Out of memory\n", .{});
            },
        }
        return err;
    };
    defer allocator.free(hash);

    // Print result
    colors.printSuccess("Dataset hash: {s}\n", .{hash});
}
