using Theriapolis.Tools.Commands;

if (args.Length == 0)
{
    PrintHelp();
    return 0;
}

return args[0].ToLowerInvariant() switch
{
    "hello"              => Hello(),
    "worldgen-dump"      => WorldgenDump.Run(args[1..]),
    "worldgen-hash"      => WorldgenHash.Run(args[1..]),
    "settlement-report"  => SettlementReport.Run(args[1..]),
    "tile-inspect"       => TileInspect.Run(args[1..]),
    "tactical-dump"      => TacticalDump.Run(args[1..]),
    "tile-analyze"       => TileAnalyze.Run(args[1..]),
    "content-validate"   => ContentValidate.Run(args[1..]),
    "character-roll"     => CharacterRoll.Run(args[1..]),
    "combat-duel"        => CombatDuel.Run(args[1..]),
    "settlement-render"  => SettlementRender.Run(args[1..]),
    "dialogue-validate"  => DialogueValidate.Run(args[1..]),
    "quest-validate"     => QuestValidate.Run(args[1..]),
    "dungeon-render"     => DungeonRender.Run(args[1..]),
    _                    => Unknown(args[0]),
};

static int Hello()
{
    var asm    = typeof(Theriapolis.Core.C).Assembly;
    var ver    = asm.GetName().Version?.ToString() ?? "0.0.0.0";
    Console.WriteLine($"Theriapolis Tools — Core version {ver}");
    return 0;
}

static int Unknown(string cmd)
{
    Console.Error.WriteLine($"Unknown command: {cmd}");
    PrintHelp();
    return 1;
}

static void PrintHelp()
{
    Console.WriteLine("Theriapolis Tools");
    Console.WriteLine();
    Console.WriteLine("Commands:");
    Console.WriteLine("  hello                             Print version.");
    Console.WriteLine("  worldgen-dump --seed <n>          Run full pipeline and export biome+feature map PNG.");
    Console.WriteLine("                --out <file.png>");
    Console.WriteLine("                [--data-dir <dir>]");
    Console.WriteLine("                [--show-violations]");
    Console.WriteLine("  worldgen-hash --seed <n>          Run full pipeline and print FNV-1a hashes (elevation,");
    Console.WriteLine("                [--data-dir <dir>]  moisture, temperature, biomes, settlements, polylines)");
    Console.WriteLine("                                    plus per-stage hashes. Determinism oracle for the");
    Console.WriteLine("                                    Godot port (compare to Godot host's smoke-test output).");
    Console.WriteLine("  settlement-report --seed <n>      Run full pipeline and print settlement report.");
    Console.WriteLine("                [--data-dir <dir>]");
    Console.WriteLine("  tile-inspect --seed <n>           Print polylines/bridges near a reported tile.");
    Console.WriteLine("                --tile X,Y          (for debugging bug reports from the in-game overlay)");
    Console.WriteLine("                [--radius N]        (default 3)");
    Console.WriteLine("                [--data-dir <dir>]");
    Console.WriteLine("  tactical-dump --seed <n>          Run full pipeline and export a tactical chunk PNG.");
    Console.WriteLine("                --chunk cx,cy");
    Console.WriteLine("                [--grid N]          (NxN chunks centered on cx,cy; default 1)");
    Console.WriteLine("                [--out <file.png>]");
    Console.WriteLine("                [--data-dir <dir>]");
    Console.WriteLine("  tile-analyze --dir <path>         Vet a directory of 32x32 tile PNGs (typically");
    Console.WriteLine("                                    a Pixellab tiles_pro download). Reports per-tile");
    Console.WriteLine("                [--sheet <out.png>] border edges, opaque %, and shadow gradient scores.");
    Console.WriteLine("                [--shadow-threshold N]  Optionally writes a labeled contact sheet.");
    Console.WriteLine("                [--upscale N]       See theriapolis-tile-generation-handoff.md.");
    Console.WriteLine("  content-validate                  Load every Phase 5 JSON content file and run");
    Console.WriteLine("                [--data-dir <dir>]  per-file + cross-file referential checks. CI gate.");
    Console.WriteLine("  character-roll                    Build a sample character via CharacterBuilder and");
    Console.WriteLine("                [--seed N]          dump the resulting stat block. Useful for verifying");
    Console.WriteLine("                [--clade ID]        determinism, balance sweeps, and content edits.");
    Console.WriteLine("                [--species ID]      Defaults: canidae / wolf / fangsworn / pack_raised.");
    Console.WriteLine("                [--class ID]");
    Console.WriteLine("                [--background ID]");
    Console.WriteLine("                [--name STR]");
    Console.WriteLine("                [--roll]            Use 4d6-drop-lowest (otherwise Standard Array).");
    Console.WriteLine("                [--ms-override N]   Pin the roll seed for reproducibility.");
    Console.WriteLine("  combat-duel                       Run a deterministic scripted duel between two");
    Console.WriteLine("                [--seed N]          combatants and print the encounter log. Combatants");
    Console.WriteLine("                [--a SPEC]          may be NPC template ids (\"brigand_footpad\", \"wolf\")");
    Console.WriteLine("                [--b SPEC]          or character specs (\"char:canidae:wolf:fangsworn:");
    Console.WriteLine("                [--rounds N]        pack_raised\"). Defaults brigand_footpad vs wolf at");
    Console.WriteLine("                                    seed 42 with a 20-round cap.");
    Console.WriteLine("  settlement-render                 Phase 6 M0 — stamp a settlement and export PNG.");
    Console.WriteLine("                [--seed N]          Defaults seed=12345, first Tier-1 anchor.");
    Console.WriteLine("                [--settlement S]    Anchor name (\"millhaven\") or numeric id.");
    Console.WriteLine("                [--pad N]           Extra chunks of context around the settlement.");
    Console.WriteLine("                [--out file.png]    Output path.");
    Console.WriteLine("                [--data-dir <dir>]");
    Console.WriteLine("  dialogue-validate                 Phase 6 M3 — load every dialogues/*.json and run");
    Console.WriteLine("                [--data-dir <dir>]  structural + reachability checks. CI gate.");
    Console.WriteLine("  quest-validate                    Phase 6 M4 — load every quests/*.json and run");
    Console.WriteLine("                [--data-dir <dir>]  structural + reachability checks. CI gate.");
    Console.WriteLine("  dungeon-render                    Phase 7 M0 — render a single room template's");
    Console.WriteLine("                --template ID       ASCII grid to PNG. M1 will add full procedural");
    Console.WriteLine("                --out file.png      --seed/--poi assembly mode.");
    Console.WriteLine("                [--cell N]          (px per tactical tile; default 16)");
    Console.WriteLine("                [--data-dir <dir>]");
}
