#!/usr/bin/env lua

-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
--
-- SPDX-License-Identifier: GPL-3.0-or-later

if _VERSION < "Lua 5.2" then
	io.stderr:write("error: wt requires Lua 5.2 or later\n")
	os.exit(1)
end

local exit = require("wt.exit")
local shell = require("wt.shell")
local path_mod = require("wt.path")
local git_mod = require("wt.git")
local config_mod = require("wt.config")
local hooks_mod = require("wt.hooks")
local help_mod = require("wt.help")
local fetch_mod = require("wt.cmd.fetch")
local list_mod = require("wt.cmd.list")
local remove_mod = require("wt.cmd.remove")
local add_mod = require("wt.cmd.add")
local new_mod = require("wt.cmd.new")
local clone_mod = require("wt.cmd.clone")
local init_mod = require("wt.cmd.init")

-- Main entry point

local function main()
	local command = arg[1]

	if not command or command == "help" or command == "--help" or command == "-h" then
		help_mod.print_usage()
		os.exit(exit.EXIT_SUCCESS)
	end

	---@cast command string

	-- Collect remaining args
	local subargs = {}
	for i = 2, #arg do
		table.insert(subargs, arg[i])
	end

	-- Check for --help on any command
	if subargs[1] == "--help" or subargs[1] == "-h" then
		help_mod.show_command_help(command)
	end

	if command == "c" then
		clone_mod.cmd_clone(subargs)
	elseif command == "n" then
		new_mod.cmd_new(subargs)
	elseif command == "a" then
		add_mod.cmd_add(subargs)
	elseif command == "r" then
		remove_mod.cmd_remove(subargs)
	elseif command == "l" then
		list_mod.cmd_list()
	elseif command == "f" then
		fetch_mod.cmd_fetch()
	elseif command == "init" then
		init_mod.cmd_init(subargs)
	else
		shell.die("unknown command: " .. command)
	end
end

-- Export for testing when required as module
if pcall(debug.getlocal, 4, 1) then
	---@diagnostic disable: duplicate-set-field
	return {
		-- URL/project parsing
		extract_project_name = config_mod.extract_project_name,
		resolve_url_template = config_mod.resolve_url_template,
		-- Path manipulation
		branch_to_path = path_mod.branch_to_path,
		split_path = path_mod.split_path,
		relative_path = path_mod.relative_path,
		path_inside = path_mod.path_inside,
		-- Config loading
		load_global_config = config_mod.load_global_config,
		load_project_config = config_mod.load_project_config,
		-- Git output parsing (testable without git)
		parse_branch_remotes = git_mod.parse_branch_remotes,
		parse_worktree_list = git_mod.parse_worktree_list,
		escape_pattern = path_mod.escape_pattern,
		-- Hook helpers (re-exported from wt.hooks)
		summarize_hooks = hooks_mod.summarize_hooks,
		load_hook_permissions = hooks_mod.load_hook_permissions,
		save_hook_permissions = hooks_mod.save_hook_permissions,
		run_hooks = hooks_mod.run_hooks,
		-- Project root detection (re-exported from wt.git)
		find_project_root = git_mod.find_project_root,
		detect_source_worktree = git_mod.detect_source_worktree,
		-- Command execution (for integration tests)
		run_cmd = shell.run_cmd,
		run_cmd_silent = shell.run_cmd_silent,
		-- Exit codes (re-exported from wt.exit)
		EXIT_SUCCESS = exit.EXIT_SUCCESS,
		EXIT_USER_ERROR = exit.EXIT_USER_ERROR,
		EXIT_SYSTEM_ERROR = exit.EXIT_SYSTEM_ERROR,
	}
end

main()
