-- helpers_demo.lua: demonstrates senl.* host helpers
local sen  = require("lua_sen")
local json = require("senl.json")
local fs   = require("senl.fs")
local exec = require("senl.exec")
local env  = require("senl.env")
local path = require("senl.path")

local function json_demo(ctx)
    local data = { name = "senl", version = "0.2.0", features = {"json", "fs", "exec"} }
    local encoded = json.encode_pretty(data)
    local decoded = json.parse(encoded)
    return sen.ok(string.format(
        "encode → parse roundtrip OK\nname=%s, features=%d items",
        decoded.name, #decoded.features
    ))
end

local function fs_demo(ctx)
    local dir = ctx.args.dir or "."
    local files = fs.walk(dir)
    local lines = {}
    for i, f in ipairs(files) do
        lines[#lines + 1] = string.format("  %s", f)
        if i >= 10 then
            lines[#lines + 1] = string.format("  ... and %d more", #files - 10)
            break
        end
    end
    return sen.ok(string.format("%d files in %s:\n%s", #files, dir, table.concat(lines, "\n")))
end

local function exec_demo(ctx)
    local result = exec.run("echo", {"hello from exec"})
    local uname = exec.capture("uname", {"-s"})
    return sen.ok(string.format(
        "exec.run:     stdout=%s (code=%d)\nexec.capture:  uname=%s",
        result.stdout:gsub("\n", ""), result.code, uname
    ))
end

local function env_demo(ctx)
    local home = env.home()
    local user = env.get("USER") or env.get("USERNAME") or "unknown"
    local p = path.join(home, ".claude", "settings.json")
    local ext = path.ext(p)
    local stem = path.stem(p)
    return sen.ok(string.format(
        "HOME=%s\nUSER=%s\npath.join=%s\npath.ext=%s  path.stem=%s",
        home, user, p, ext, stem
    ))
end

local app = sen.app("helpers-demo", "Demonstrate senl.* host helpers")
    :version("0.2.0")
    :command("json", "JSON roundtrip demo"):done()
    :command("fs", "File system walk demo")
        :arg("dir", "Directory to walk", { required = false })
        :done()
    :command("exec", "Subprocess demo"):done()
    :command("env", "Environment & path demo"):done()

app:route("json", json_demo)
app:route("fs", fs_demo)
app:route("exec", exec_demo)
app:route("env", env_demo)

return app:build()
