#!/usr/bin/env bash
set -Eeuo pipefail

POWERUSER_DIR="/usr/share/artix-poweruser"

GUM_TITLE_COLOR="${GUM_TITLE_COLOR:-212}"
GUM_ACCENT_COLOR="${GUM_ACCENT_COLOR:-34}"

[[ -f /etc/anvil-theme.conf ]] && source /etc/anvil-theme.conf

RECIPES_REPO="https://raw.githubusercontent.com/realvolk/ArtixForge-recipes/main"
LIST_URL="${RECIPES_REPO}/.LIST"
LOCAL_LIST="${POWERUSER_DIR}/.recipes.list"
SECTION_CONFIG="${POWERUSER_DIR}/recipe-sections.conf"
DEFAULT_SECTIONS="OFFICIAL/Base"

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/anvil_common.bash"
source "${SCRIPT_DIR}/anvil_tui.bash"
source "${SCRIPT_DIR}/anvil_cli.bash"
source "${SCRIPT_DIR}/anvil_recovery.bash"

usage() {
    cat <<EOF
Usage: anvil [--tui] <command> [args]

Commands:
  list              List installed source packages
  list-recipes      List all available recipes
  info <pkg>        Show build details
  rebuild <pkg>     Rebuild a specific package
  new <name>        Create a new recipe from template
  edit <name>       Edit an existing recipe
  config            Edit the running kernel's .config
  menuconfig        Launch make menuconfig (needs kernel source)
  fetch-recipe <name>  Download a single recipe from the community repo
  fetch-all         Download all recipe sources for offline use
  lint <name>       Validate a recipe
  checksum <recipe> Download sources and print SHA256 checksums
  upgrade           Backup recipes and update from remote
  cache-clean       Remove obsolete cached packages
  recovery [pkg]    Check or repair source‑built packages (default: kernel)
  sync              Update .LIST and recipes from community repo
  sections          Manage which recipe sections are enabled
  --tui             Launch interactive TUI
EOF
}

main() {
    load_sections

    if [[ "${1:-}" == "--tui" || "${1:-}" == "-tui" ]]; then
        tui_main
        exit 0
    fi

    case "${1:-}" in
        list)          list_packages ;;
        list-recipes)  list_recipes ;;
        info)          [[ -n "${2:-}" ]] || { usage; exit 1; }; info_package "${2}" ;;
        rebuild)       [[ -n "${2:-}" ]] || { usage; exit 1; }; rebuild_package "${2}" ;;
        new)           [[ -n "${2:-}" ]] || { usage; exit 1; }; new_recipe "${2}" ;;
        edit)          [[ -n "${2:-}" ]] || { usage; exit 1; }; edit_recipe "${2}" ;;
        config)        edit_config ;;
        menuconfig)    launch_menuconfig ;;
        fetch-source)  [[ -n "${2:-}" ]] || { usage; exit 1; }; fetch_source "${2}" ;;
        fetch-recipe)  [[ -n "${2:-}" ]] || { usage; exit 1; }; fetch_recipe "${2}" ;;
        fetch-all)     fetch_all_sources ;;
        lint)          [[ -n "${2:-}" ]] || { usage; exit 1; }; lint_recipe "${2}" ;;
        checksum)      [[ -n "${2:-}" ]] || { usage; exit 1; }; checksum_recipe "${2}" ;;
        upgrade)       upgrade_anvil ;;
        cache-clean)   cache_clean ;;
        recovery)
            require_root
            if [[ -z "${2:-}" ]]; then
                anvil_recovery_status
                if ! tui_yesno "Repair?" "Repair detected issues?"; then
                    exit 0
                fi
                anvil_recovery_repair
            else
                anvil_recovery_repair "${2}"
            fi
            ;;
        sync)          sync_recipes ;;
        sections)      tui_manage_sections ;;
        *)             usage ;;
    esac
}

main "$@"