require "./check"
require "./doctor"
require "./fmt"
require "./init"
require "./clear_cache"
require "./check_update"
require "./stats"
require "./list_symbols"

module Lattice
  module CLI
    HELP_TEXT = <<-HELP
      Usage: lattice <command> [arguments]

      Commands:
        check <path> [...]              Run offline diagnostics on Crystal source files or directories
        doctor                          Check environment prerequisites (Crystal, ameba, shards)
        fmt [path]                      Format Crystal source via crystal tool format; CI-safe exit codes
        init [dir]                      Scaffold a .lattice.yml config in the current or given directory
        clear-cache                     Delete the .lattice-cache/ persistent index directory
        check-update                    Compare local version against the latest Codeberg release
        stats [path]                    Print symbol-kind counts for the workspace index
        list-symbols [--kind=K] [path]  Dump all indexed symbols as JSON or plain text

      Options (LSP server mode):
        --strict             Enable stricter type checking
        --version, -v        Print version and exit
        --help, -h           Show this help message
      HELP

    def self.dispatch(argv : Array(String)) : Nil
      command = argv.first? || ""

      case command
      when "check"
        Check.run(argv[1..])
      when "doctor"
        Doctor.run
      when "init"
        Init.run(argv[1]? || ".")
      when "clear-cache"
        ClearCache.run
      when "check-update"
        CheckUpdate.run
      when "fmt"
        Fmt.run(argv[1..])
      when "stats"
        Stats.run(argv[1..])
      when "list-symbols"
        ListSymbols.run(argv[1..])
      when "--version", "-v"
        puts "v#{Lattice::VERSION}"
        exit 0
      when "--help", "-h", "help", ""
        puts HELP_TEXT
        exit 0
      else
        STDERR.puts "lattice: unknown command '#{command}'"
        STDERR.puts "Run 'lattice --help' for usage."
        exit 1
      end
    end
  end
end
