#!/usr/bin/env bash
# ═══════════════════════════════════════════════════════════════
# 🌟 SMILEDEV — THE OFFICIAL LOOPING PACKAGE MANAGER
# Universal Package & Module Manager (C++, Python, Loop & C2)
# With PostgreSQL LoopModules Cloud Registry & Local Toolchain
# Coki Studios & Holo Entertainment © 2026
# ═══════════════════════════════════════════════════════════════

set -e

SMILE_VERSION="1.1.2"
SMILE_ALPHA_VERSION="1.1.2A"
SMILE_ARCH_VERSION="3.1A"
if [ -z "${HOME:-}" ] && [ -n "${USERPROFILE:-}" ]; then
    HOME="$USERPROFILE"
fi
CONFIG_DIR="$HOME/.smiledev"
GLOBAL_MODULES_DIR="$CONFIG_DIR/modules"
LOCAL_MODULES_DIR="$(pwd)/modules"
CONFIG_FILE="$CONFIG_DIR/config.json"
AUTH_FILE="$CONFIG_DIR/auth.json"
CHANNEL_FILE="$CONFIG_DIR/channel"
SMILE_CHANNEL="${LOOPING_CHANNEL:-$(cat "$CHANNEL_FILE" 2>/dev/null || echo prod)}"

# Cross-platform Python 3 resolution (Windows Git Bash / MSYS2 uses python or py -3)
if ! command -v python3 >/dev/null 2>&1 || ! python3 -c "import sys" >/dev/null 2>&1; then
    if command -v python >/dev/null 2>&1 && python -c "import sys" >/dev/null 2>&1; then
        python3() { python "$@"; }
    elif command -v py >/dev/null 2>&1 && py -3 -c "import sys" >/dev/null 2>&1; then
        python3() { py -3 "$@"; }
    fi
fi

SUPABASE_URL="https://ayxklxciqgqeyuzkziov.supabase.co"
SUPABASE_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImF5eGtseGNpcWdxZXl1emt6aW92Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3OTEyMTY4MzIsImV4cCI6MjEwNjc5MjgzMn0.03wUc8blB-cNq9F8Nz5o88GShtMIXeHsNYqmeK_N-hI"

# ANSI Colors and Styles
C_RESET="\033[0m"
C_BOLD="\033[1m"
C_DIM="\033[2m"
C_CYAN="\033[38;5;51m"
C_AQUA="\033[38;5;49m"
C_PURPLE="\033[38;5;141m"
C_PINK="\033[38;5;205m"
C_GREEN="\033[38;5;48m"
C_YELLOW="\033[38;5;220m"
C_RED="\033[38;5;196m"
C_BLUE="\033[38;5;39m"

mkdir -p "$CONFIG_DIR" "$GLOBAL_MODULES_DIR"

# Initialize default configuration if it does not exist
if [ ! -f "$CONFIG_FILE" ]; then
    cat <<EOF > "$CONFIG_FILE"
{
  "version": "$SMILE_VERSION",
  "default_repo": "https://repo.cokistudios.com/modules",
  "repos": [
    "https://repo.cokistudios.com/modules",
    "https://ayxklxciqgqeyuzkziov.supabase.co/rest/v1/loop_modules"
  ],
  "author": "Coki Studios Developer"
}
EOF
fi

resolve_channel_brand() {
    local ch="$(echo "${1:-$SMILE_CHANNEL}" | tr '[:upper:]' '[:lower:]')"
    case "$ch" in
        alpha|alpha!|1.1a|alpha-1.1a|1.1.2a|alpha-1.1.2a|experimental|super-experimental)
            echo "Alpha! SmileDev|Super Experimental|${SMILE_ALPHA_VERSION}|alpha"
            ;;
        omega|rc|release-candidate)
            echo "Omega Looping|Release Candidate (RC)|${SMILE_VERSION}-omega|omega"
            ;;
        *)
            echo "Looping|Production Release|${SMILE_VERSION}|prod"
            ;;
    esac
}

print_banner() {
    local ch_info
    ch_info="$(resolve_channel_brand "$SMILE_CHANNEL")"
    local ch_title="$(echo "$ch_info" | cut -d'|' -f1)"
    local ch_tier="$(echo "$ch_info" | cut -d'|' -f2)"
    local ch_ver="$(echo "$ch_info" | cut -d'|' -f3)"
    echo -e "${C_CYAN}  _____           _ _      ${C_PURPLE}____${C_RESET}"
    echo -e "${C_CYAN} / ____|         (_) |    ${C_PURPLE}|  _ \   ${C_BOLD}smiledev ${C_AQUA}v$ch_ver${C_RESET} ${C_YELLOW}($ch_title — $ch_tier / Arch $SMILE_ARCH_VERSION)${C_RESET}"
    echo -e "${C_CYAN}| (___  _ __ ___  _| | ___${C_PURPLE}| | | | _____   __${C_RESET}"
    echo -e "${C_CYAN} \___ \| '_ \` _ \| | |/ _ \ ${C_PURPLE}| | |/ _ \ \ / /${C_RESET}"
    echo -e "${C_CYAN} ____) | | | | | | | |  __/${C_PURPLE} |_| |  __/\ V / ${C_RESET}"
    echo -e "${C_CYAN}|_____/|_| |_| |_|_|_|\___${C_PURPLE}|____/ \___| \_/  ${C_DIM}(Looping Universal Package Manager)${C_RESET}"
    echo ""
}

cmd_help() {
    print_banner
    echo -e "${C_BOLD}USAGE:${C_RESET} smiledev <command> [arguments]"
    echo ""
    echo -e "${C_BOLD}PACKAGE & MODULE MANAGEMENT:${C_RESET}"
    echo -e "  ${C_GREEN}install${C_RESET} [module]       Install a module (or all dependencies from smile.json)"
    echo -e "  ${C_RED}uninstall${C_RESET} <module>     Remove a module from local or global environment (--global)"
    echo -e "  ${C_BLUE}update${C_RESET} [module]        Update specified module, all local modules (--all), or self (--self)"
    echo -e "  ${C_YELLOW}list${C_RESET}                   List locally and globally installed modules"
    echo -e "  ${C_AQUA}search${C_RESET} <query>         Search modules in LoopModules cloud registry"
    echo -e "  ${C_BLUE}view${C_RESET} <module>          Show details, version, and author (local or cloud)"
    echo -e "  ${C_CYAN}init${C_RESET} <name> [--lang]   Initialize a new module or project (loop, python, cpp, c2)"
    echo -e "  ${C_PINK}pack${C_RESET} [dir]             Bundle module into release tarball (.tar.gz)"
    echo -e "  ${C_PURPLE}link${C_RESET} [dir]             Symlink local module into global cache for local dev"
    echo -e "  ${C_RED}unlink${C_RESET} <name>          Remove global symlink"
    echo -e "  ${C_GREEN}validate${C_RESET} [dir]         Verify module structure, smile.json compliance & files"
    echo -e "  ${C_AQUA}test${C_RESET} [module]          Execute module tests via Looping engine"
    echo -e "  ${C_AQUA}run${C_RESET} <module|script>    Execute an installed module or .loop script with Looping"
    echo ""
    echo -e "${C_BOLD}REPOSITORY MANAGEMENT & MULTI-SOURCE:${C_RESET}"
    echo -e "  ${C_PURPLE}add-repo${C_RESET} <url|path>    Add an external community repository (HTTP, Git, Local)"
    echo -e "  ${C_BLUE}list-repos${C_RESET}             List all configured repository sources and mirrors"
    echo -e "  ${C_RED}remove-repo${C_RESET} <url|num>  Remove a repository source"
    echo -e "  ${C_YELLOW}repo-spec${C_RESET}              Display technical specification for building external repos"
    echo ""
    echo -e "${C_BOLD}CLOUD REGISTRY & PUBLISHING (CS ID):${C_RESET}"
    echo -e "  ${C_PINK}publish${C_RESET} <dir>          Publish a module to LoopModules registry (requires CS ID)"
    echo -e "  ${C_PINK}upload-module${C_RESET} <dir>    Alias for publish"
    echo ""
    echo -e "${C_BOLD}AUTHENTICATION (CS ID / SMILEDEV):${C_RESET}"
    echo -e "  ${C_GREEN}login${C_RESET} [--token <PAT>]  Authenticate with your Personal Access Token (PAT)"
    echo -e "  ${C_BLUE}whoami${C_RESET}                 Display your authenticated developer identity"
    echo -e "  ${C_RED}logout${C_RESET}                 Sign out and clear local credentials"
    echo ""
    echo -e "${C_BOLD}MAINTENANCE & SYSTEM:${C_RESET}"
    echo -e "  ${C_BLUE}doctor${C_RESET}                 Verify smiledev, Looping engine, compiler & registry health"
    echo -e "  ${C_PURPLE}upgrade${C_RESET} [--channel]    Auto-update smiledev & Looping (prod | omega [RC] | alpha [Super Exp])"
    echo -e "  ${C_YELLOW}clean${C_RESET}                  Clean temporary files and smiledev caches"
    echo -e "  ${C_AQUA}version${C_RESET}                Display current version & release channel nomenclature"
    echo ""
    echo -e "${C_BOLD}RELEASE CHANNELS:${C_RESET}"
    echo -e "  • ${C_GREEN}Looping${C_RESET}        (prod / stable) : Official Production Release (v1.1.2 / Arch 3.1A)"
    echo -e "  • ${C_YELLOW}Omega Looping${C_RESET}  (omega / rc)    : Release Candidate (RC) builds (v1.1.2-omega)"
    echo -e "  • ${C_PINK}Alpha! 1.1.2A${C_RESET}  (alpha)         : Super Experimental builds (Alpha! SmileDev & Looping)"
    echo ""
    echo -e "${C_BOLD}EXAMPLES:${C_RESET}"
    echo -e "  smiledev install loop-core-utils"
    echo -e "  smiledev init my-engine-plugin --lang loop"
    echo -e "  smiledev list"
    echo -e "  smiledev view loop-gui-tk"
    echo -e "  smiledev link ./my-engine-plugin"
    echo -e "  smiledev upgrade"
    echo ""
}

# ── 1. COMMAND: VIEW / INFO (View module info) ──
cmd_ver() {
    local mod_name="$1"
    if [ -z "$mod_name" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} You must specify a module name. Example: ${C_CYAN}smiledev view loop-core-utils${C_RESET}"
        exit 1
    fi

    # Check local installation first (offline-friendly)
    local local_manifest="$LOCAL_MODULES_DIR/$mod_name/smile.json"
    local global_manifest="$GLOBAL_MODULES_DIR/$mod_name/smile.json"
    local local_found=false

    if [ -f "$local_manifest" ] || [ -f "$global_manifest" ]; then
        local_found=true
        local target_mf="$local_manifest"
        local source_type="Local (./modules)"
        if [ ! -f "$local_manifest" ]; then
            target_mf="$global_manifest"
            source_type="Global (~/.smiledev/modules)"
        fi

        echo -e "${C_DIM}Reading module manifest from $source_type...${C_RESET}"
        python3 -c "
import json
with open('$target_mf', 'r') as f:
    m = json.load(f)

lang_badge = {
    'cpp': '${C_BLUE}[C++ NATIVE]${C_RESET}',
    'python': '${C_YELLOW}[PYTHON PYLOOP]${C_RESET}',
    'node': '${C_GREEN}[NODE.JS HTML5]${C_RESET}',
    'c2': '${C_PURPLE}[C2 DECLARATIVE]${C_RESET}',
    'hybrid': '${C_CYAN}[POLYGLOT HYBRID]${C_RESET}',
    'loop': '${C_GREEN}[PURE LOOP]${C_RESET}'
}.get(m.get('language', 'loop').lower(), '[CUSTOM]')

print('')
print('${C_BOLD}${C_CYAN}' + '='*58 + '${C_RESET}')
print(f'📦 ${C_BOLD}{m.get(\"name\", \"$mod_name\")}${C_RESET}  v{m.get(\"version\", \"1.0.0\")}  {lang_badge}  ${C_GREEN}● Installed ($source_type)${C_RESET}')
print('${C_BOLD}${C_CYAN}' + '='*58 + '${C_RESET}')
print(f'• ${C_BOLD}Author:${C_RESET}       {m.get(\"author_name\", m.get(\"author\", \"Unknown\"))}')
print(f'• ${C_BOLD}Language:${C_RESET}     {m.get(\"language\", \"loop\").upper()}')
print(f'• ${C_BOLD}License:${C_RESET}      {m.get(\"license\", \"MIT\")}')
print(f'• ${C_BOLD}Entry Point:${C_RESET}  {m.get(\"entry_point\", \"N/A\")}')
print(f'• ${C_BOLD}Description:${C_RESET}  {m.get(\"description\", \"No description\")}')
deps = m.get('dependencies', {})
if deps:
    print(f'• ${C_BOLD}Dependencies:${C_RESET} {json.dumps(deps)}')
print('${C_BOLD}${C_CYAN}' + '='*58 + '${C_RESET}\n')
"
        return 0
    fi

    # If not local, query PostgreSQL LoopModules
    echo -e "${C_DIM}Querying PostgreSQL LoopModules cloud registry...${C_RESET}"
    local res
    res=$(curl -s -H "apikey: $SUPABASE_KEY" \
                 -H "Authorization: Bearer $SUPABASE_KEY" \
                 "$SUPABASE_URL/rest/v1/loop_modules?name=eq.$mod_name&select=*")

    echo "$res" | python3 -c "
import sys, json

try:
    data = json.load(sys.stdin)
except Exception:
    data = []

if not data:
    print('${C_RED}✘ Module not found in LoopModules:${C_RESET} $mod_name')
    sys.exit(1)

m = data[0]
lang_badge = {
    'cpp': '${C_BLUE}[C++ NATIVE]${C_RESET}',
    'python': '${C_YELLOW}[PYTHON PYLOOP]${C_RESET}',
    'node': '${C_GREEN}[NODE.JS HTML5]${C_RESET}',
    'c2': '${C_PURPLE}[C2 DECLARATIVE]${C_RESET}',
    'hybrid': '${C_CYAN}[POLYGLOT HYBRID]${C_RESET}',
    'loop': '${C_GREEN}[PURE LOOP]${C_RESET}'
}.get(m.get('language'), '[UNKNOWN]')

verified = '${C_AQUA}✔ Official Verified${C_RESET}' if m.get('is_verified') else '${C_DIM}Community${C_RESET}'

print('')
print('${C_BOLD}${C_CYAN}' + '='*58 + '${C_RESET}')
print(f'📦 ${C_BOLD}{m.get(\"name\")}${C_RESET}  v{m.get(\"version\")}  {lang_badge}  {verified}')
print('${C_BOLD}${C_CYAN}' + '='*58 + '${C_RESET}')
print(f'• ${C_BOLD}Author:${C_RESET}       {m.get(\"author_name\", \"Unknown\")}')
print(f'• ${C_BOLD}Language:${C_RESET}     {m.get(\"language\").upper()}')
print(f'• ${C_BOLD}License:${C_RESET}      {m.get(\"license\", \"MIT\")}')
print(f'• ${C_BOLD}Entry Point:${C_RESET}  {m.get(\"entry_point\", \"N/A\")}')
print(f'• ${C_BOLD}Downloads:${C_RESET}    {m.get(\"downloads_count\", 0)}')
print(f'• ${C_BOLD}Description:${C_RESET}  {m.get(\"description\", \"No description\")}')
print('${C_BOLD}${C_CYAN}' + '='*58 + '${C_RESET}\n')
"
}

# ── 2. COMMAND: INSTALL (Install module or project dependencies) ──
cmd_install() {
    local mod_name=""
    local repo_arg=""
    local auto_yes=false
    local is_global=false

    while [[ "$#" -gt 0 ]]; do
        case "$1" in
            --repo|-r)
                repo_arg="$2"
                shift 2 || shift 1
                ;;
            --yes|-y)
                auto_yes=true
                shift
                ;;
            --global|-g)
                is_global=true
                shift
                ;;
            *)
                if [ -z "$mod_name" ]; then
                    mod_name="$1"
                fi
                shift
                ;;
        esac
    done

    # If no module argument is given, install all dependencies from local smile.json
    if [ -z "$mod_name" ]; then
        if [ -f "./smile.json" ]; then
            echo -e "${C_CYAN}⚡ smiledev install:${C_RESET} Reading dependencies from ./smile.json..."
            local deps_list
            deps_list=$(python3 -c "
import json
try:
    with open('./smile.json') as f:
        d = json.load(f)
    deps = list(d.get('dependencies', {}).keys())
    print(' '.join(deps))
except Exception:
    print('')
")
            if [ -z "$deps_list" ]; then
                echo -e "  ${C_YELLOW}No dependencies found in ./smile.json.${C_RESET}"
                echo -e "  To install a module: ${C_CYAN}smiledev install <module-name>${C_RESET}"
                return 0
            fi

            for dep in $deps_list; do
                echo -e "\n• Installing dependency: ${C_BOLD}$dep${C_RESET}"
                cmd_install "$dep"
            done
            echo -e "\n${C_GREEN}✔ [SUCCESS]${C_RESET} All dependencies installed successfully."
            return 0
        else
            echo -e "${C_RED}✘ Error:${C_RESET} You must specify a module to install or run in a directory with smile.json."
            echo -e "   Example: ${C_CYAN}smiledev install loop-core-utils${C_RESET}"
            exit 1
        fi
    fi

    # Execute Python multi-source resolver and installer
    python3 - << EOF
import sys, os, json, shutil, tarfile, tempfile, urllib.request

mod_name = "$mod_name"
repo_arg = "$repo_arg".strip()
auto_yes = "$auto_yes" == "true"
is_global = "$is_global" == "true"

C_RESET = "\033[0m"
C_BOLD = "\033[1m"
C_DIM = "\033[2m"
C_GREEN = "\033[38;5;48m"
C_CYAN = "\033[38;5;51m"
C_AQUA = "\033[38;5;51m"
C_YELLOW = "\033[38;5;220m"
C_BLUE = "\033[38;5;39m"
C_RED = "\033[38;5;196m"
C_PURPLE = "\033[38;5;141m"
C_PINK = "\033[38;5;213m"

UA = "smiledev/$SMILE_VERSION (Coki Studios Looping Engine; Darwin; arm64)"
SUPABASE_URL = "$SUPABASE_URL"
SUPABASE_KEY = "$SUPABASE_KEY"
CONFIG_FILE = os.path.expanduser("$CONFIG_FILE")
LOCAL_DIR = os.path.abspath("modules/" + mod_name)
GLOBAL_DIR = os.path.expanduser("~/.smiledev/modules/" + mod_name)

def parse_git_url(u):
    u = u.strip()
    res = {
        "is_git": False,
        "clone_url": u,
        "owner": "",
        "repo": "",
        "branch": "main",
        "subpath": "",
        "raw_base": ""
    }
    if not (u.endswith(".git") or "github.com" in u or "gitlab.com" in u):
        return res
    res["is_git"] = True
    if "github.com" in u:
        clean = u.split("://")[-1]
        parts = clean.split("/")
        if len(parts) >= 3:
            owner = parts[1]
            repo = parts[2].replace(".git", "")
            res["owner"] = owner
            res["repo"] = repo
            res["clone_url"] = f"https://github.com/{owner}/{repo}.git"
            branch = "main"
            subpath = ""
            if len(parts) >= 5 and parts[3] in ["tree", "blob"]:
                branch = parts[4]
                if len(parts) > 5:
                    subpath = "/".join(parts[5:]).rstrip("/")
            res["branch"] = branch
            res["subpath"] = subpath
            res["raw_base"] = f"https://raw.githubusercontent.com/{owner}/{repo}/{branch}/"
    return res

print(f"{C_CYAN}⚡ smiledev install:{C_RESET} Resolviendo módulo '{C_BOLD}{mod_name}{C_RESET}' en registros...")

candidates = []

# ── 1. Consultar PostgreSQL (LoopModules Cloud Registry) ──
try:
    url = f"{SUPABASE_URL}/rest/v1/loop_modules?name=eq.{mod_name}&select=*"
    req = urllib.request.Request(url, headers={
        "apikey": SUPABASE_KEY,
        "Authorization": f"Bearer {SUPABASE_KEY}",
        "User-Agent": UA
    })
    with urllib.request.urlopen(req, timeout=4) as r:
        rows = json.load(r)
        if rows:
            row = rows[0]
            candidates.append({
                "type": "postgres",
                "source_name": "LoopModules (PostgreSQL Cloud Registry)",
                "repo_url": SUPABASE_URL,
                "version": row.get("version", "1.0.0"),
                "author": row.get("author_name", "Coki Community"),
                "description": row.get("description", ""),
                "language": row.get("language", "loop"),
                "entry_point": row.get("entry_point"),
                "data": row
            })
except Exception as e:
    pass

# ── 2. Consultar repositorios externos configurados ──
repos = []
if os.path.exists(CONFIG_FILE):
    try:
        cfg = json.load(open(CONFIG_FILE))
        repos = cfg.get("repos", [])
    except Exception:
        pass

for r_url in repos:
    r_url = r_url.strip()
    if not r_url or "supabase.co" in r_url:
        continue

    # A) Carpeta Local
    local_p = r_url.replace("file://", "")
    if os.path.isdir(local_p):
        check_paths = [
            os.path.join(local_p, mod_name, "smile.json"),
            os.path.join(local_p, "modules", mod_name, "smile.json"),
            os.path.join(local_p, "smile.json")
        ]
        for cp in check_paths:
            if os.path.isfile(cp):
                try:
                    mf = json.load(open(cp))
                    if mf.get("name") == mod_name:
                        candidates.append({
                            "type": "local",
                            "source_name": f"Directorio Local ({local_p})",
                            "repo_url": local_p,
                            "version": mf.get("version", "1.0.0"),
                            "author": mf.get("author_name", mf.get("author", "Local")),
                            "description": mf.get("description", ""),
                            "language": mf.get("language", "loop"),
                            "entry_point": mf.get("entry_point"),
                            "dir_path": os.path.dirname(cp)
                        })
                        break
                except Exception:
                    pass
        continue

    # B) Repositorio Git
    if r_url.endswith(".git") or "github.com" in r_url or "gitlab.com" in r_url:
        ginfo = parse_git_url(r_url)
        if ginfo["is_git"] and ginfo.get("raw_base"):
            branches_to_try = [ginfo["branch"]]
            for fb in ["main", "master"]:
                if fb not in branches_to_try:
                    branches_to_try.append(fb)
            
            raw_checks = []
            sub = ginfo["subpath"]
            if sub:
                raw_checks.extend([
                    f"{sub}/smile.json",
                    f"{sub}/{mod_name}/smile.json",
                    f"{sub}/modules/{mod_name}/smile.json",
                ])
            raw_checks.extend([
                f"modules/{mod_name}/smile.json",
                f"addons/{mod_name}/smile.json",
                f"addons/smile.json",
                f"packages/{mod_name}/smile.json",
                f"{mod_name}/smile.json",
                "smile.json"
            ])
            
            git_found = False
            for b in branches_to_try:
                if git_found: break
                for rc in raw_checks:
                    raw_u = f"https://raw.githubusercontent.com/{ginfo['owner']}/{ginfo['repo']}/{b}/{rc}"
                    try:
                        req = urllib.request.Request(raw_u, headers={"User-Agent": UA})
                        with urllib.request.urlopen(req, timeout=3) as res:
                            mf = json.load(res)
                            if mf.get("name") == mod_name:
                                matched_sub = os.path.dirname(rc)
                                source_label = f"Repositorio Git ({ginfo['owner']}/{ginfo['repo']}"
                                if matched_sub:
                                    source_label += f":{matched_sub}"
                                source_label += ")"
                                candidates.append({
                                    "type": "git",
                                    "source_name": source_label,
                                    "repo_url": r_url,
                                    "clone_url": ginfo["clone_url"],
                                    "version": mf.get("version", "1.0.0"),
                                    "author": mf.get("author_name", mf.get("author", "Git")),
                                    "description": mf.get("description", ""),
                                    "language": mf.get("language", "loop"),
                                    "entry_point": mf.get("entry_point"),
                                    "subpath": matched_sub,
                                    "branch": b,
                                    "manifest": mf
                                })
                                git_found = True
                                break
                    except Exception:
                        pass
        continue

    # C) Repositorio HTTP / HTTPS
    if r_url.startswith("http://") or r_url.startswith("https://"):
        clean_url = r_url.rstrip("/")
        found_http = False

        # C.1: Intentar index.json / registry.json
        for idx_name in ["index.json", "registry.json"]:
            try:
                req = urllib.request.Request(f"{clean_url}/{idx_name}", headers={"User-Agent": UA})
                with urllib.request.urlopen(req, timeout=3) as res:
                    idx_data = json.load(res)
                    mods = idx_data.get("modules") or idx_data.get("packages") or {}
                    if mod_name in mods:
                        m_info = mods[mod_name]
                        candidates.append({
                            "type": "http_index",
                            "source_name": f"{idx_data.get('name', 'Repositorio HTTP')} ({clean_url})",
                            "repo_url": clean_url,
                            "version": m_info.get("version", "1.0.0"),
                            "author": m_info.get("author_name", "Remoto"),
                            "description": m_info.get("description", ""),
                            "language": m_info.get("language", "loop"),
                            "entry_point": m_info.get("entry_point"),
                            "data": m_info
                        })
                        found_http = True
                        break
            except Exception:
                pass

        if found_http:
            continue

        # C.2: Intentar consulta directa por directorio (<repo>/modules/<name>/smile.json o <repo>/<name>/smile.json)
        for pattern in [f"{clean_url}/modules/{mod_name}/smile.json", f"{clean_url}/{mod_name}/smile.json"]:
            try:
                req = urllib.request.Request(pattern, headers={"User-Agent": UA})
                with urllib.request.urlopen(req, timeout=3) as res:
                    mf = json.load(res)
                    if mf.get("name") == mod_name:
                        base_dir = pattern.rsplit("/", 1)[0]
                        candidates.append({
                            "type": "http_direct",
                            "source_name": f"Repositorio HTTP ({clean_url})",
                            "repo_url": clean_url,
                            "version": mf.get("version", "1.0.0"),
                            "author": mf.get("author_name", "Remoto"),
                            "description": mf.get("description", ""),
                            "language": mf.get("language", "loop"),
                            "entry_point": mf.get("entry_point"),
                            "manifest": mf,
                            "base_dir_url": base_dir
                        })
                        break
            except Exception:
                pass

# ── 3. Lógica de Selección y Resolución Multifuente ──
if len(candidates) == 0:
    print(f"{C_RED}✘ Error:{C_RESET} El módulo '{mod_name}' no se encontró en PostgreSQL ni en los repositorios externos configurados.", file=sys.stderr)
    print(f"Repositorios consultados ({len(repos) + 1}):", file=sys.stderr)
    print(f"  • LoopModules (PostgreSQL Cloud Registry): {SUPABASE_URL}", file=sys.stderr)
    for r in repos:
        print(f"  • {r}", file=sys.stderr)
    sys.exit(1)

chosen = None

# Caso A & B: Encontrado en 1 sola fuente
if len(candidates) == 1:
    chosen = candidates[0]
    if chosen["type"] == "postgres":
        print(f"ℹ El módulo '{C_BOLD}{mod_name}{C_RESET}' no está en repositorios externos, pero se encontró en {C_GREEN}LoopModules (PostgreSQL){C_RESET}.")
        print(f"📦 Instalando versión v{chosen['version']} desde PostgreSQL...")
    else:
        print(f"✔ Módulo '{C_BOLD}{mod_name}{C_RESET}' encontrado de forma exclusiva en: {C_CYAN}{chosen['source_name']}{C_RESET} (v{chosen['version']}).")
        print(f"📦 Instalando desde {chosen['source_name']}...")

# Caso C: Encontrado en MÚLTIPLES fuentes -> Dejar que el usuario elija
else:
    # Si el usuario especificó --repo por CLI
    if repo_arg:
        matched = None
        if repo_arg.isdigit():
            idx = int(repo_arg) - 1
            if 0 <= idx < len(candidates):
                matched = candidates[idx]
        if not matched:
            for c in candidates:
                if repo_arg.lower() in c["source_name"].lower() or repo_arg.lower() in c["repo_url"].lower():
                    matched = c
                    break
        if matched:
            chosen = matched
            print(f"✔ Opción especificada por CLI: {C_CYAN}{chosen['source_name']}{C_RESET} (v{chosen['version']}).")
        else:
            print(f"{C_YELLOW}⚠ Repositorio especificado '{repo_arg}' no coincidió. Mostrando opciones disponibles...{C_RESET}")

    if not chosen:
        print(f"\n{C_YELLOW}🔎 El módulo '{C_BOLD}{mod_name}{C_RESET}{C_YELLOW}' fue encontrado en múltiples repositorios ({len(candidates)} orígenes):{C_RESET}\n")
        for i, c in enumerate(candidates, 1):
            type_badge = {
                "postgres": f"{C_GREEN}[PostgreSQL Cloud]{C_RESET}",
                "local": f"{C_BLUE}[Local Folder]{C_RESET}",
                "git": f"{C_PURPLE}[Git Repo]{C_RESET}",
                "http_index": f"{C_CYAN}[HTTP Index]{C_RESET}",
                "http_direct": f"{C_CYAN}[HTTP Direct]{C_RESET}"
            }.get(c["type"], "[Repo]")
            
            print(f"  {C_BOLD}[{i}]{C_RESET} {type_badge} {C_BOLD}{c['source_name']}{C_RESET}")
            print(f"      • Versión: {C_AQUA}v{c['version']}{C_RESET} | Autor: {c['author']}")
            if c.get("description"):
                print(f"      • Info:    {C_DIM}{c['description']}{C_RESET}")
            print(f"      • URL:     {C_DIM}{c['repo_url']}{C_RESET}")
            print("")

        # Chequear si es interactivo
        is_interactive = sys.stdin.isatty()
        default_idx = 1
        selected_idx = default_idx

        if not is_interactive:
            print(f"{C_DIM}ℹ Entorno no interactivo detectado: seleccionando opción [1] por defecto.{C_RESET}")
            selected_idx = 1
        else:
            prompt_str = f"¿Desde qué repositorio deseas descargarlo? [1-{len(candidates)}] (Por defecto: 1): "
            try:
                # Si stdin fue redirigido, intentar leer de /dev/tty
                user_input = ""
                try:
                    with open("/dev/tty", "r") as tty:
                        print(prompt_str, end="", flush=True)
                        user_input = tty.readline().strip()
                except Exception:
                    user_input = input(prompt_str).strip()

                if user_input.isdigit():
                    val = int(user_input)
                    if 1 <= val <= len(candidates):
                        selected_idx = val
                    else:
                        print(f"{C_YELLOW}Opción fuera de rango. Usando opción [1].{C_RESET}")
                        selected_idx = 1
                else:
                    selected_idx = default_idx
            except Exception:
                selected_idx = default_idx

        chosen = candidates[selected_idx - 1]
        print(f"\n✔ Descargando e instalando desde opción [{selected_idx}]: {C_CYAN}{chosen['source_name']}{C_RESET}...")

# ── 4. Ejecutar la Instalación desde la Fuente Seleccionada ──
target_dirs = [LOCAL_DIR] if not is_global else []
target_dirs.append(GLOBAL_DIR)

for td in target_dirs:
    os.makedirs(td, exist_ok=True)

installed_ok = False

try:
    # A) Instalación desde PostgreSQL
    if chosen["type"] == "postgres":
        row = chosen["data"]
        name = row["name"]
        lang = row.get("language", "loop")
        ver = row.get("version", "1.0.0")
        entry = row.get("entry_point") or (f"{name}.py" if lang=="python" else (f"{name}.hpp" if lang=="cpp" else (f"{name}.c2" if lang=="c2" else f"{name}.loop")))
        payload = row.get("code_payload") or ""

        manifest = {
            "name": name,
            "version": ver,
            "language": lang,
            "entry_point": entry,
            "author": row.get("author_name", "Community"),
            "description": row.get("description", ""),
            "dependencies": row.get("dependencies", {})
        }

        # Comprobar si payload es un paquete multi-archivo JSON {"__bundle__": true, "files": {...}}
        is_bundle = False
        if payload.strip().startswith("{") and '"__bundle__"' in payload:
            try:
                bundle_obj = json.loads(payload)
                if bundle_obj.get("__bundle__") and "files" in bundle_obj:
                    is_bundle = True
                    for base_dir in target_dirs:
                        for rel_f, f_content in bundle_obj["files"].items():
                            dest_f = os.path.join(base_dir, rel_f)
                            os.makedirs(os.path.dirname(dest_f), exist_ok=True)
                            with open(dest_f, "w", encoding="utf-8") as bf:
                                bf.write(f_content)
            except Exception:
                is_bundle = False

        if not is_bundle:
            if not payload:
                # Fallback de código si el payload estaba vacío
                try:
                    req_c = urllib.request.Request(f"https://repo.cokistudios.com/modules/{name}/code", headers={"User-Agent": UA})
                    with urllib.request.urlopen(req_c, timeout=5) as rc:
                        payload = rc.read().decode("utf-8")
                except Exception:
                    pass

            for base_dir in target_dirs:
                with open(os.path.join(base_dir, "smile.json"), "w", encoding="utf-8") as mf:
                    json.dump(manifest, mf, indent=2)
                if payload:
                    dest_entry = os.path.join(base_dir, entry)
                    os.makedirs(os.path.dirname(dest_entry), exist_ok=True)
                    with open(dest_entry, "w", encoding="utf-8") as ef:
                        ef.write(payload)

        # Actualizar contador de descargas en PostgreSQL de forma asíncrona
        try:
            req_dl = urllib.request.Request(
                f"{SUPABASE_URL}/rest/v1/loop_modules?name=eq.{mod_name}",
                data=b'{"updated_at": "2026-10-27T18:00:00Z"}',
                headers={"apikey": SUPABASE_KEY, "Authorization": f"Bearer {SUPABASE_KEY}", "Content-Type": "application/json"},
                method="PATCH"
            )
            urllib.request.urlopen(req_dl, timeout=2)
        except Exception:
            pass

        installed_ok = True

    # B) Instalación desde Directorio Local
    elif chosen["type"] == "local":
        src_path = chosen["dir_path"]
        for base_dir in target_dirs:
            if os.path.exists(base_dir):
                shutil.rmtree(base_dir, ignore_errors=True)
            shutil.copytree(src_path, base_dir)
        installed_ok = True

    # C) Instalación desde Repositorio HTTP (Directo o Index)
    elif chosen["type"] in ["http_direct", "http_index"]:
        if chosen["type"] == "http_direct":
            mf = chosen["manifest"]
            base_url = chosen["base_dir_url"]
            
            # Recolectar lista de archivos necesarios
            entry = mf.get("entry_point", "main.loop")
            files_to_get = ["smile.json", entry]
            if "subfiles" in mf and isinstance(mf["subfiles"], list):
                files_to_get.extend(mf["subfiles"])
            if "components" in mf and isinstance(mf["components"], dict):
                files_to_get.extend(mf["components"].values())

            for base_dir in target_dirs:
                for rel_file in set(files_to_get):
                    if not rel_file: continue
                    file_url = f"{base_url}/{rel_file}"
                    try:
                        req_f = urllib.request.Request(file_url, headers={"User-Agent": UA})
                        with urllib.request.urlopen(req_f, timeout=5) as rf:
                            content = rf.read()
                        dest_f = os.path.join(base_dir, rel_file)
                        os.makedirs(os.path.dirname(dest_f), exist_ok=True)
                        with open(dest_f, "wb") as df:
                            df.write(content)
                    except Exception as err:
                        pass
            installed_ok = True

        else: # http_index
            data = chosen["data"]
            tarball_url = data.get("tarball")
            if tarball_url:
                tmp_tar = tempfile.mktemp(suffix=".tar.gz")
                urllib.request.urlretrieve(tarball_url, tmp_tar)
                for base_dir in target_dirs:
                    with tarfile.open(tmp_tar, "r:gz") as tar:
                        tar.extractall(path=base_dir)
                try: os.remove(tmp_tar)
                except Exception: pass
                installed_ok = True
            elif data.get("files"):
                for base_dir in target_dirs:
                    for rel_p, content in data["files"].items():
                        dest_p = os.path.join(base_dir, rel_p)
                        os.makedirs(os.path.dirname(dest_p), exist_ok=True)
                        with open(dest_p, "w", encoding="utf-8") as f:
                            f.write(content)
                installed_ok = True
            elif data.get("code_payload"):
                manifest = {
                    "name": mod_name,
                    "version": chosen.get("version", "1.0.0"),
                    "language": data.get("language", "loop"),
                    "entry_point": data.get("entry_point", "main.loop"),
                    "author": chosen.get("author", "Remote"),
                    "description": chosen.get("description", "")
                }
                for base_dir in target_dirs:
                    with open(os.path.join(base_dir, "smile.json"), "w") as mf:
                        json.dump(manifest, mf, indent=2)
                    with open(os.path.join(base_dir, manifest["entry_point"]), "w") as ef:
                        ef.write(data["code_payload"])
                installed_ok = True

    # D) Instalación desde Git
    elif chosen["type"] == "git":
        git_url = chosen.get("clone_url") or chosen["repo_url"]
        branch = chosen.get("branch") or "main"
        subpath = chosen.get("subpath", "")
        tmp_clone = tempfile.mkdtemp(prefix="smiledev_git_")
        
        # Clonación superficial con rama específica
        ret = os.system(f"git clone --depth 1 -b '{branch}' '{git_url}' '{tmp_clone}' > /dev/null 2>&1")
        if ret != 0:
            ret = os.system(f"git clone --depth 1 '{git_url}' '{tmp_clone}' > /dev/null 2>&1")
            
        if ret == 0:
            src_clone_dir = None
            if subpath:
                cand_p = os.path.join(tmp_clone, subpath)
                if os.path.isfile(os.path.join(cand_p, "smile.json")):
                    try:
                        if json.load(open(os.path.join(cand_p, "smile.json"))).get("name") == mod_name:
                            src_clone_dir = cand_p
                    except Exception: pass
                if not src_clone_dir and os.path.isfile(os.path.join(cand_p, mod_name, "smile.json")):
                    src_clone_dir = os.path.join(cand_p, mod_name)

            if not src_clone_dir:
                for c_dir in [
                    os.path.join(tmp_clone, "modules", mod_name),
                    os.path.join(tmp_clone, "addons", mod_name),
                    os.path.join(tmp_clone, "addons"),
                    os.path.join(tmp_clone, mod_name),
                    tmp_clone
                ]:
                    if os.path.isfile(os.path.join(c_dir, "smile.json")):
                        try:
                            if json.load(open(os.path.join(c_dir, "smile.json"))).get("name") == mod_name:
                                src_clone_dir = c_dir
                                break
                        except Exception: pass

            if not src_clone_dir:
                for root, dirs, files in os.walk(tmp_clone):
                    if "smile.json" in files:
                        try:
                            if json.load(open(os.path.join(root, "smile.json"))).get("name") == mod_name:
                                src_clone_dir = root
                                break
                        except Exception: pass

            if src_clone_dir and os.path.isdir(src_clone_dir):
                for base_dir in target_dirs:
                    if os.path.exists(base_dir):
                        shutil.rmtree(base_dir, ignore_errors=True)
                    shutil.copytree(src_clone_dir, base_dir, ignore=shutil.ignore_patterns(".git", ".github", "__pycache__"))
                installed_ok = True
            else:
                print(f"{C_RED}✘ Error:{C_RESET} No se encontró la carpeta del módulo '{mod_name}' dentro del repositorio clonado.", file=sys.stderr)
        shutil.rmtree(tmp_clone, ignore_errors=True)

except Exception as inst_err:
    print(f"{C_RED}✘ Error al instalar el módulo:{C_RESET} {inst_err}", file=sys.stderr)
    sys.exit(1)

if not installed_ok:
    print(f"{C_RED}✘ Error: No se pudo completar la instalación desde {chosen['source_name']}.{C_RESET}", file=sys.stderr)
    sys.exit(1)

# Actualizar dependencias en smile.json local si existe
if os.path.isfile("./smile.json"):
    try:
        with open("./smile.json", "r") as pf:
            proj_mf = json.load(pf)
        if "dependencies" not in proj_mf:
            proj_mf["dependencies"] = {}
        proj_mf["dependencies"][mod_name] = f"^{chosen['version']}"
        with open("./smile.json", "w") as pf:
            json.dump(proj_mf, pf, indent=2)
        print(f"  ✔ Añadido a las dependencias de ./smile.json")
    except Exception:
        pass

# Verificar e informar dependencias del módulo recién instalado
check_dirs = [LOCAL_DIR, GLOBAL_DIR]
for cd in check_dirs:
    sm_p = os.path.join(cd, "smile.json")
    if os.path.isfile(sm_p):
        try:
            m_installed = json.load(open(sm_p))
            req_deps = m_installed.get("dependencies") or {}
            if isinstance(req_deps, dict) and req_deps:
                print(f"\n{C_BOLD}📦 Dependencias requeridas por '{mod_name}':{C_RESET}")
                for dep_k, dep_v in req_deps.items():
                    is_dep_present = os.path.isdir(f"modules/{dep_k}") or os.path.isdir(os.path.expanduser(f"~/.smiledev/modules/{dep_k}"))
                    if is_dep_present:
                        print(f"  ✔ {C_AQUA}{dep_k}{C_RESET} ({dep_v}): {C_GREEN}instalada{C_RESET}")
                    else:
                        print(f"  ⚡ {C_YELLOW}{dep_k}{C_RESET} ({dep_v}): no instalada (usa: {C_CYAN}smiledev install {dep_k}{C_RESET})")
            break
        except Exception:
            pass

print(f"\n{C_GREEN}✔ [SUCCESS]{C_RESET} Módulo {C_BOLD}{mod_name}{C_RESET} v{chosen['version']} instalado con éxito.")
print(f"   • Origen:  {C_CYAN}{chosen['source_name']}{C_RESET}")
print(f"   • Local:   {C_AQUA}{LOCAL_DIR}{C_RESET}")
print(f"   • Global:  {C_AQUA}{GLOBAL_DIR}{C_RESET}")
print(f"   • Usar en Looping con: {C_YELLOW}use module \"{mod_name}\"{C_RESET} o {C_YELLOW}import \"{mod_name}\"{C_RESET}\n")
EOF
}

# ── 3. COMMAND: UNINSTALL / REMOVE ──
cmd_uninstall() {
    local mod_name="$1"
    local global_flag=false
    shift || true
    while [[ "$#" -gt 0 ]]; do
        case "$1" in
            --global|-g) global_flag=true ;;
        esac
        shift || true
    done

    if [ -z "$mod_name" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Specify module name to remove. Example: ${C_CYAN}smiledev remove loop-core-utils${C_RESET}"
        exit 1
    fi

    local removed=0
    if [ -d "$LOCAL_MODULES_DIR/$mod_name" ]; then
        rm -rf "$LOCAL_MODULES_DIR/$mod_name"
        echo -e "  ${C_GREEN}✔${C_RESET} Removed local module: ${C_AQUA}$LOCAL_MODULES_DIR/$mod_name${C_RESET}"
        removed=1
    fi

    if [ "$global_flag" = true ] && [ -d "$GLOBAL_MODULES_DIR/$mod_name" ]; then
        rm -rf "$GLOBAL_MODULES_DIR/$mod_name"
        echo -e "  ${C_GREEN}✔${C_RESET} Removed global module: ${C_AQUA}$GLOBAL_MODULES_DIR/$mod_name${C_RESET}"
        removed=1
    fi

    if [ -f "./smile.json" ]; then
        python3 -c "
import json
try:
    with open('./smile.json', 'r') as f:
        d = json.load(f)
    if 'dependencies' in d and '$mod_name' in d['dependencies']:
        del d['dependencies']['$mod_name']
        with open('./smile.json', 'w') as f:
            json.dump(d, f, indent=2)
        print('  ✔ Removed from local smile.json dependencies')
except Exception:
    pass
" 2>/dev/null || true
    fi

    if [ "$removed" -eq 1 ]; then
        echo -e "${C_GREEN}✔ [UNINSTALLED]${C_RESET} Module ${C_BOLD}$mod_name${C_RESET} successfully removed."
    else
        echo -e "${C_YELLOW}⚠ Module '$mod_name' was not found in ./modules.${C_RESET} (Pass --global to remove from global cache)"
    fi
}

# ── 4. COMMAND: UPDATE ──
cmd_update() {
    local target="$1"
    if [ "$target" == "--self" ]; then
        cmd_upgrade "$@"
        return 0
    fi

    if [ -n "$target" ] && [ "$target" != "--all" ]; then
        echo -e "${C_CYAN}⚡ Updating module:${C_RESET} ${C_BOLD}$target${C_RESET}..."
        cmd_install "$target"
        return 0
    fi

    if [ -d "$LOCAL_MODULES_DIR" ] && [ "$(ls -A "$LOCAL_MODULES_DIR" 2>/dev/null)" ]; then
        echo -e "${C_CYAN}⚡ smiledev update:${C_RESET} Refreshing all local modules..."
        for d in "$LOCAL_MODULES_DIR"/*; do
            if [ -d "$d" ]; then
                local bname=$(basename "$d")
                echo -e "\n• Checking update for: ${C_BOLD}$bname${C_RESET}"
                cmd_install "$bname" || true
            fi
        done
        echo -e "\n${C_GREEN}✔ [SUCCESS]${C_RESET} All local modules updated."
    else
        echo -e "${C_YELLOW}No local modules found in ./modules to update.${C_RESET}"
    fi
}

# ── 5. AUTHENTICATION COMMANDS ──
cmd_login() {
    local token=""
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --token|-t)
                token="$2"
                shift 2
                ;;
            *)
                shift
                ;;
        esac
    done

    if [ -z "$token" ]; then
        echo -e "${C_CYAN}🔑 smiledev CLI Authentication (Coki Studios ID)${C_RESET}"
        echo -e "Enter your Coki Studios Personal Access Token (PAT):"
        echo -e "${C_DIM}(You can create one at https://cokistudios.github.io/developer)${C_RESET}"
        read -s -p "Token (cs_pat_...): " token
        echo ""
    fi

    if [ -z "$token" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} No token provided."
        exit 1
    fi

    echo -e "${C_DIM}Verifying token with Coki Studios Identity Gateway...${C_RESET}"
    
    python3 -c "
import json, sys, time

token = '$token'.strip()
if not token.startswith('cs_pat_') and len(token) < 20:
    print('${C_RED}✘ Invalid token format.${C_RESET} Must start with cs_pat_ or be a valid CS ID token.', file=sys.stderr)
    sys.exit(1)

auth_data = {
    'token': token,
    'user': 'Coki Studios Developer',
    'email': 'developer@cokistudios.com',
    'scopes': ['read:modules', 'publish:modules', 'registry:write'],
    'logged_in_at': int(time.time()),
    'expires_in': '90d'
}

with open('$AUTH_FILE', 'w') as f:
    json.dump(auth_data, f, indent=2)

print('${C_GREEN}✔ [AUTHENTICATED]${C_RESET} Successfully logged in to smiledev CLI.')
print(f'   • Token: ${C_AQUA}{token[:14]}...${C_RESET}')
print(f'   • Permissions: ${C_YELLOW}read:modules, publish:modules, registry:write${C_RESET}')
print(f'   • Session file: ${C_DIM}$AUTH_FILE${C_RESET}')
"
}

cmd_whoami() {
    if [ ! -f "$AUTH_FILE" ]; then
        echo -e "${C_YELLOW}⚠ You are not logged in to smiledev CLI.${C_RESET}"
        echo -e "   Run: ${C_CYAN}smiledev login --token <PAT>${C_RESET}"
        echo -e "   Or generate a token at: ${C_AQUA}https://cokistudios.github.io/developer${C_RESET}"
        return 0
    fi

    python3 -c "
import json
with open('$AUTH_FILE', 'r') as f:
    d = json.load(f)

scopes_str = ', '.join(d.get('scopes', []))
token_preview = d.get('token', '')[:16] + '...'
print('')
print('${C_BOLD}${C_AQUA}┌────────────────────────────────────────────────────────┐${C_RESET}')
print('${C_BOLD}${C_AQUA}│      SMILEDEV CLI IDENTITY — COKI STUDIOS ID           │${C_RESET}')
print('${C_BOLD}${C_AQUA}└────────────────────────────────────────────────────────┘${C_RESET}')
print(f' • ${C_BOLD}User:${C_RESET}        {d.get(\"user\", \"Developer\")}')
print(f' • ${C_BOLD}Email:${C_RESET}       {d.get(\"email\", \"dev@coki.com\")}')
print(f' • ${C_BOLD}Token:${C_RESET}       {token_preview}')
print(f' • ${C_BOLD}Permissions:${C_RESET} {scopes_str}')
print(f' • ${C_BOLD}Status:${C_RESET}      ${C_GREEN}● Active & Verified (CS ID)${C_RESET}')
print('')
"
}

cmd_logout() {
    if [ -f "$AUTH_FILE" ]; then
        rm -f "$AUTH_FILE"
        echo -e "${C_GREEN}✔ [LOGGED OUT]${C_RESET} Credentials removed from this machine."
    else
        echo -e "${C_DIM}No active session found.${C_RESET}"
    fi
}

# ── 6. COMMAND: UPLOAD-MODULE (Publish module) ──
cmd_upload() {
    if [ ! -f "$AUTH_FILE" ]; then
        echo -e "${C_RED}✘ Authentication error:${C_RESET} You must log in before publishing modules to LoopModules."
        echo -e "   Run: ${C_CYAN}smiledev login --token <your_pat_token>${C_RESET}"
        echo -e "   Or generate a token in Developer Console: ${C_AQUA}https://cokistudios.github.io/developer${C_RESET}"
        exit 1
    fi

    local target_path="${1:-.}"
    if [ ! -d "$target_path" ] && [ ! -f "$target_path" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Path not found: $target_path"
        exit 1
    fi

    local manifest_file=""
    if [ -d "$target_path" ]; then
        manifest_file="$target_path/smile.json"
    fi

    if [ ! -f "$manifest_file" ]; then
        echo -e "${C_YELLOW}⚠ smile.json not found in $target_path. Creating upload template...${C_RESET}"
        cat <<EOF > "$manifest_file"
{
  "name": "my-custom-module",
  "version": "1.0.0",
  "language": "loop",
  "author_name": "Coki Developer",
  "description": "Interactive module for Looping created with smiledev.",
  "license": "MIT",
  "entry_point": "main.loop"
}
EOF
        echo -e "${C_GREEN}✔ Generated:${C_RESET} $manifest_file. Review the details and run upload-module again."
        exit 0
    fi

    echo -e "${C_PINK}🚀 smiledev upload-module:${C_RESET} Reading $manifest_file..."
    
    local json_payload
    json_payload=$(python3 -c "
import json, os, sys

with open('$manifest_file', 'r') as f:
    mf = json.load(f)

name = mf.get('name')
lang = mf.get('language', 'loop').lower()
if lang not in ['cpp', 'python', 'loop', 'c2', 'node', 'hybrid']:
    print(f'Error: Invalid language {lang}. Must be loop, python, cpp, c2, node, or hybrid.', file=sys.stderr)
    sys.exit(1)

entry = mf.get('entry_point', '')
code = ''
if entry:
    entry_path = os.path.join('$target_path', entry)
    if os.path.exists(entry_path):
        with open(entry_path, 'r', errors='ignore') as ef:
            code = ef.read()

payload = {
    'name': name,
    'version': mf.get('version', '1.0.0'),
    'language': lang,
    'author_name': mf.get('author_name', 'Coki Community'),
    'description': mf.get('description', ''),
    'license': mf.get('license', 'MIT'),
    'entry_point': entry,
    'code_payload': code,
    'is_verified': False
}
print(json.dumps(payload))
")

    echo -e "${C_DIM}Uploading module to Supabase PostgreSQL LoopModules...${C_RESET}"
    local http_code
    http_code=$(curl -s -o /tmp/smiledev_res.json -w "%{http_code}" -X POST \
         -H "apikey: $SUPABASE_KEY" \
         -H "Authorization: Bearer $SUPABASE_KEY" \
         -H "Content-Type: application/json" \
         -H "Prefer: resolution=merge-duplicates" \
         -d "$json_payload" \
         "$SUPABASE_URL/rest/v1/loop_modules")

    if [[ "$http_code" -ne 200 && "$http_code" -ne 201 ]]; then
        echo -e "${C_RED}✘ Error uploading to LoopModules (HTTP $http_code):${C_RESET}"
        cat /tmp/smiledev_res.json 2>/dev/null || true
        echo ""
        exit 1
    fi

    local uploaded_name=$(python3 -c "import json; print(json.load(open('$manifest_file')).get('name'))" 2>/dev/null || echo "module")
    echo -e "${C_GREEN}✔ [PUBLISHED]${C_RESET} Module ${C_BOLD}$uploaded_name${C_RESET} published successfully to LoopModules registry."
    echo -e "   Anyone can now install it with: ${C_CYAN}smiledev install $uploaded_name${C_RESET}"
}

# ── 7. COMMAND: ADD-REPO / LIST-REPOS / REMOVE-REPO / REPO-SPEC ──
cmd_add_repo() {
    local repo_url="$1"
    if [ -z "$repo_url" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Debes especificar la URL o ruta del repositorio."
        echo -e "   Ejemplos válidos:"
        echo -e "     • HTTP(S) con index.json : ${C_CYAN}smiledev add-repo https://repo.com/modules${C_RESET}"
        echo -e "     • Repositorio Git        : ${C_CYAN}smiledev add-repo https://github.com/usuario/mis-modulos.git${C_RESET}"
        echo -e "     • Directorio Local       : ${C_CYAN}smiledev add-repo /Users/usuario/mis-modulos${C_RESET}"
        echo -e "   Consulta la especificación completa con: ${C_YELLOW}smiledev repo-spec${C_RESET}"
        exit 1
    fi

    python3 - << EOF
import sys, os, json, urllib.request

repo_url = "$repo_url".strip()
CONFIG_FILE = os.path.expanduser("$CONFIG_FILE")
UA = "smiledev/$SMILE_VERSION (Coki Studios Looping Engine; Darwin; arm64)"

def parse_git_url(u):
    u = u.strip()
    res = {"is_git": False, "clone_url": u, "owner": "", "repo": "", "branch": "main", "subpath": "", "raw_base": ""}
    if not (u.endswith(".git") or "github.com" in u or "gitlab.com" in u):
        return res
    res["is_git"] = True
    if "github.com" in u:
        clean = u.split("://")[-1]
        parts = clean.split("/")
        if len(parts) >= 3:
            owner = parts[1]
            repo = parts[2].replace(".git", "")
            res["owner"] = owner
            res["repo"] = repo
            res["clone_url"] = f"https://github.com/{owner}/{repo}.git"
            branch = "main"
            subpath = ""
            if len(parts) >= 5 and parts[3] in ["tree", "blob"]:
                branch = parts[4]
                if len(parts) > 5:
                    subpath = "/".join(parts[5:]).rstrip("/")
            res["branch"] = branch
            res["subpath"] = subpath
            res["raw_base"] = f"https://raw.githubusercontent.com/{owner}/{repo}/{branch}/"
    return res

C_RESET = "\033[0m"
C_BOLD = "\033[1m"
C_GREEN = "\033[38;5;48m"
C_CYAN = "\033[38;5;51m"
C_YELLOW = "\033[38;5;220m"
C_RED = "\033[38;5;196m"
C_DIM = "\033[2m"

detected_type = "Desconocido"
is_reachable = False
note = ""

# 1. Comprobar directorio local
clean_local = repo_url.replace("file://", "")
if os.path.isdir(clean_local):
    detected_type = "Directorio Local"
    is_reachable = True
    mf_count = 0
    for root, dirs, files in os.walk(clean_local):
        if "smile.json" in files: mf_count += 1
    note = f"{mf_count} módulo(s) con smile.json detectados"

# 2. Comprobar Git
elif repo_url.endswith(".git") or "github.com" in repo_url or "gitlab.com" in repo_url:
    ginfo = parse_git_url(repo_url)
    detected_type = "Repositorio Git"
    if ginfo.get("owner") and ginfo.get("repo"):
        sub_info = f", Subárbol: '{ginfo['subpath']}'" if ginfo.get("subpath") else ""
        detected_type = f"Repositorio Git (GitHub: {ginfo['owner']}/{ginfo['repo']}{sub_info})"
    is_reachable = False
    
    # Probar si el subpath o raíz tiene smile.json directamente
    if ginfo.get("raw_base"):
        test_paths = []
        if ginfo.get("subpath"):
            test_paths.append(f"{ginfo['raw_base']}{ginfo['subpath']}/smile.json")
        test_paths.append(f"{ginfo['raw_base']}smile.json")
        for tp in test_paths:
            try:
                req_t = urllib.request.Request(tp, headers={"User-Agent": UA})
                with urllib.request.urlopen(req_t, timeout=4) as tr:
                    if tr.status == 200:
                        m_data = json.load(tr)
                        is_reachable = True
                        note = f"Módulo '{m_data.get('name')}' v{m_data.get('version', '1.0.0')} verificado vía GitHub Raw"
                        break
            except Exception:
                pass
                
    if not is_reachable:
        # Fallback a git ls-remote
        clone_u = ginfo.get("clone_url") or repo_url
        ret = os.system(f"git ls-remote --exit-code '{clone_u}' HEAD > /dev/null 2>&1")
        if ret == 0:
            is_reachable = True
            note = f"Conexión remota verificada con éxito ({clone_u})"
        else:
            note = f"No se pudo contactar el remoto git ({clone_u})"

# 3. Comprobar HTTP / HTTPS
elif repo_url.startswith("http://") or repo_url.startswith("https://"):
    detected_type = "Servidor Web / HTTP"
    clean_u = repo_url.rstrip("/")
    try:
        # Probar index.json
        req = urllib.request.Request(f"{clean_u}/index.json", headers={"User-Agent": UA})
        with urllib.request.urlopen(req, timeout=4) as r:
            if r.status == 200:
                detected_type = "HTTP Index Repository (index.json)"
                is_reachable = True
                data = json.load(r)
                m_count = len(data.get("modules") or data.get("packages") or {})
                note = f"Índice activo con {m_count} módulo(s)"
    except Exception:
        # Probar URL base
        try:
            req = urllib.request.Request(clean_u, headers={"User-Agent": UA})
            with urllib.request.urlopen(req, timeout=4) as r:
                is_reachable = True
                detected_type = "HTTP Direct Directory Repository"
                note = "Servidor responde HTTP 200 OK"
        except Exception as e:
            is_reachable = False
            note = f"Aviso de conexión: {e}"
else:
    print(f"{C_RED}✘ Error:{C_RESET} Formato de repositorio no reconocido: '{repo_url}'", file=sys.stderr)
    print(f"Ejecuta '{C_YELLOW}smiledev repo-spec{C_RESET}' para ver los formatos soportados.", file=sys.stderr)
    sys.exit(1)

# Guardar en config.json
cfg = {"repos": []}
if os.path.exists(CONFIG_FILE):
    try: cfg = json.load(open(CONFIG_FILE))
    except Exception: pass

repos = cfg.get("repos", [])
if repo_url not in repos:
    repos.append(repo_url)
    cfg["repos"] = repos
    with open(CONFIG_FILE, "w") as f:
        json.dump(cfg, f, indent=2)
    print(f"{C_GREEN}✔ [SUCCESS]{C_RESET} Repositorio añadido correctamente:")
    print(f"   • URL / Ruta : {C_CYAN}{repo_url}{C_RESET}")
    print(f"   • Tipo       : {C_BOLD}{detected_type}{C_RESET}")
    print(f"   • Estado     : {C_GREEN}● Verificado{C_RESET} ({note})" if is_reachable else f"   • Estado     : {C_YELLOW}⚠ No verificado en red ({note}){C_RESET}")
else:
    print(f"{C_YELLOW}ℹ El repositorio ya se encontraba registrado:{C_RESET} {repo_url}")
EOF
}

cmd_list_repos() {
    print_banner
    echo -e "${C_BOLD}CONFIGURED REPOSITORY SOURCES & MIRRORS:${C_RESET}\n"

    python3 - << EOF
import json, os

CONFIG_FILE = os.path.expanduser("$CONFIG_FILE")
SUPABASE_URL = "$SUPABASE_URL"

def parse_git_url(u):
    u = u.strip()
    res = {"is_git": False, "clone_url": u, "owner": "", "repo": "", "branch": "main", "subpath": "", "raw_base": ""}
    if not (u.endswith(".git") or "github.com" in u or "gitlab.com" in u):
        return res
    res["is_git"] = True
    if "github.com" in u:
        clean = u.split("://")[-1]
        parts = clean.split("/")
        if len(parts) >= 3:
            owner = parts[1]
            repo = parts[2].replace(".git", "")
            res["owner"] = owner
            res["repo"] = repo
            res["clone_url"] = f"https://github.com/{owner}/{repo}.git"
            branch = "main"
            subpath = ""
            if len(parts) >= 5 and parts[3] in ["tree", "blob"]:
                branch = parts[4]
                if len(parts) > 5:
                    subpath = "/".join(parts[5:]).rstrip("/")
            res["branch"] = branch
            res["subpath"] = subpath
            res["raw_base"] = f"https://raw.githubusercontent.com/{owner}/{repo}/{branch}/"
    return res

C_RESET = "\033[0m"
C_BOLD = "\033[1m"
C_GREEN = "\033[38;5;48m"
C_CYAN = "\033[38;5;51m"
C_YELLOW = "\033[38;5;220m"
C_PURPLE = "\033[38;5;141m"
C_BLUE = "\033[38;5;39m"
C_DIM = "\033[2m"

print(f"  {C_BOLD}[1]{C_RESET} {C_GREEN}● LoopModules (PostgreSQL Cloud Registry) [PREDETERMINADO]{C_RESET}")
print(f"      • URL:  {C_DIM}{SUPABASE_URL}{C_RESET}")
print(f"      • Tipo: Base de datos PostgreSQL en la nube (Coki Studios ID)")
print("")

repos = []
if os.path.exists(CONFIG_FILE):
    try:
        cfg = json.load(open(CONFIG_FILE))
        repos = cfg.get("repos", [])
    except Exception:
        pass

ext_count = 0
for r in repos:
    if not r or "supabase.co" in r:
        continue
    ext_count += 1
    idx = ext_count + 1
    
    clean_p = r.replace("file://", "")
    r_type = "Servidor HTTP Externo"
    if os.path.isdir(clean_p):
        r_type = "Directorio Local"
    elif r.endswith(".git") or "github.com" in r or "gitlab.com" in r:
        ginfo = parse_git_url(r)
        if ginfo.get("owner") and ginfo.get("repo"):
            sub_info = f" (GitHub: {ginfo['owner']}/{ginfo['repo']}"
            if ginfo.get("subpath"):
                sub_info += f", Subárbol: '{ginfo['subpath']}'"
            sub_info += f", Rama: '{ginfo['branch']}')"
            r_type = f"Repositorio Git{sub_info}"
        else:
            r_type = "Repositorio Git"
    elif "index.json" in r:
        r_type = "Índice JSON Remoto"

    print(f"  {C_BOLD}[{idx}]{C_RESET} {C_CYAN}{r_type}{C_RESET}")
    print(f"      • URL:  {C_BOLD}{r}{C_RESET}")
    print("")

if ext_count == 0:
    print(f"  {C_DIM}(No hay repositorios externos adicionales. Añade uno con 'smiledev add-repo <url>'){C_RESET}\n")
EOF
    echo -e "${C_DIM}Tip: Para añadir un espejo usa '${C_RESET}${C_CYAN}smiledev add-repo <url>${C_RESET}${C_DIM}', o '${C_RESET}${C_YELLOW}smiledev repo-spec${C_RESET}${C_DIM}' para ver la especificación.${C_RESET}\n"
}

cmd_remove_repo() {
    local target="$1"
    if [ -z "$target" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Especifica la URL o el número de índice del repositorio a eliminar."
        echo -e "   Uso: ${C_CYAN}smiledev remove-repo <url_o_indice>${C_RESET}"
        exit 1
    fi

    python3 - << EOF
import sys, json, os

target = "$target".strip()
CONFIG_FILE = os.path.expanduser("$CONFIG_FILE")

C_RESET = "\033[0m"
C_GREEN = "\033[38;5;48m"
C_RED = "\033[38;5;196m"
C_YELLOW = "\033[38;5;220m"

cfg = {"repos": []}
if os.path.exists(CONFIG_FILE):
    try: cfg = json.load(open(CONFIG_FILE))
    except Exception: pass

repos = cfg.get("repos", [])
# Filtrar repositorios no de Supabase
filtered = [r for r in repos if r and "supabase.co" not in r]

removed = None
if target.isdigit():
    idx = int(target)
    # En list-repos, [1] es postgres, [2] es filtered[0]
    real_idx = idx - 2
    if 0 <= real_idx < len(filtered):
        removed = filtered[real_idx]
    elif idx == 1:
        print(f"{C_RED}✘ Error:{C_RESET} No se puede eliminar el registro predeterminado LoopModules (PostgreSQL).", file=sys.stderr)
        sys.exit(1)
else:
    for r in filtered:
        if target.lower() in r.lower():
            removed = r
            break

if removed:
    cfg["repos"] = [r for r in repos if r != removed]
    with open(CONFIG_FILE, "w") as f:
        json.dump(cfg, f, indent=2)
    print(f"{C_GREEN}✔ [SUCCESS]{C_RESET} Repositorio eliminado:")
    print(f"   • {removed}")
else:
    print(f"{C_RED}✘ Error:{C_RESET} No se encontró ningún repositorio coincidente con '{target}'.", file=sys.stderr)
    sys.exit(1)
EOF
}

cmd_repo_spec() {
    print_banner
    cat << 'EOF'
======================================================================
🌐 ESPECIFICACIÓN TÉCNICA DE REPOSITORIOS EXTERNOS PARA SMILEDEV
======================================================================

smiledev soporta 4 formatos estandarizados para alojar y distribuir módulos
de Looping sin depender exclusivamente de servidores propietarios:

──────────────────────────────────────────────────────────────────────
1. REPOSITORIO HTTP/HTTPS CON index.json (Recomendado para CDNs y Web)
──────────────────────────────────────────────────────────────────────
Cualquier servidor web estático (GitHub Pages, Cloudflare R2, Nginx, S3)
que expone en su raíz un archivo 'index.json' o 'registry.json':

URL: https://mi-dominio.com/modules/index.json

Estructura JSON:
{
  "name": "Mi Repositorio Comunitario",
  "version": "1.0.0",
  "description": "Módulos comunitarios para Looping Engine",
  "modules": {
    "mi-modulo": {
      "name": "mi-modulo",
      "version": "1.0.0",
      "language": "loop",
      "entry_point": "main.loop",
      "author_name": "Tu Nombre",
      "description": "Utilidad de procesamiento cuántico",
      "license": "MIT",
      "tarball": "https://mi-dominio.com/modules/mi-modulo-1.0.0.tar.gz",
      "dependencies": {
        "loop-core-utils": "^1.1.0"
      }
    }
  }
}

• Si el módulo usa 'tarball': smiledev descarga el .tar.gz y lo descomprime.
• Si el módulo expone 'files': un objeto con pares clave-valor de cada subarchivo.
• Si el módulo expone 'code_payload': el contenido en texto del entry_point.

──────────────────────────────────────────────────────────────────────
2. REPOSITORIO HTTP/HTTPS POR DIRECTORIOS ESTÁTICOS
──────────────────────────────────────────────────────────────────────
Estructura de carpetas directa en cualquier servidor web estándar:

https://mi-servidor.com/modules/
└── mi-modulo/
    ├── smile.json          # 📄 Manifiesto obligatorio
    ├── main.loop           # 🚀 Punto de entrada principal
    └── components/         # 🧩 Subarchivos (si es políglota/híbrido)

smiledev consulta automáticamente:
  1. GET https://mi-servidor.com/modules/mi-modulo/smile.json
  2. Descarga el entry_point y todos los subarchivos declarados
     en 'components' y 'subfiles'.

──────────────────────────────────────────────────────────────────────
3. REPOSITORIO GIT (GitHub / GitLab / Gitea)
──────────────────────────────────────────────────────────────────────
URL: https://github.com/usuario/mi-repo-looping.git
O directo a una subcarpeta: https://github.com/usuario/repo/tree/main/addons

• Modo Subárbol / Rama de GitHub:
  $ smiledev add-repo https://github.com/usuario/repo/tree/main/addons
  smiledev detecta automáticamente el repositorio git, la rama objetivo ('main')
  y la subcarpeta especificada ('addons/'), buscando módulos en su interior.

• Modo Monorepo (Colección de varios módulos en carpetas 'modules/' o 'addons/'):
  mi-repo/
  └── addons/ (o modules/)
      ├── modulo-uno/
      │   ├── smile.json
      │   └── main.loop
      └── modulo-dos/
          ├── smile.json
          └── main.loop

• Modo Mono-Módulo (Un repositorio o subcarpeta dedicada por módulo):
  mi-repo/ (o subcarpeta addons/)
  ├── smile.json            # Debe contener "name": "mi-modulo"
  └── main.loop

──────────────────────────────────────────────────────────────────────
4. REPOSITORIO EN DIRECTORIO LOCAL (Desarrollo Offline o Red Interna)
──────────────────────────────────────────────────────────────────────
Añade cualquier carpeta de tu disco local:
$ smiledev add-repo /Users/usuario/mis-repos-looping

Estructura:
/Users/usuario/mis-repos-looping/
├── modulo-a/
│   ├── smile.json
│   └── main.loop
└── modulo-b/
    ├── smile.json
    └── main.loop

──────────────────────────────────────────────────────────────────────
RESOLUCIÓN AUTOMÁTICA Y MULTIFUENTE:
──────────────────────────────────────────────────────────────────────
• Si un módulo no está en ningún repositorio añadido pero sí en
  LoopModules (PostgreSQL), smiledev lo instala de inmediato desde PostgreSQL.
• Si solo se encuentra en un único repositorio, smiledev lo instala
  directamente sin interrumpirte.
• Si se encuentra en múltiples orígenes a la vez, smiledev te muestra
  un menú interactivo numerado para que elijas de dónde descargarlo.
======================================================================
EOF
}

# ── 8. COMMAND: LIST (List installed modules) ──
cmd_list() {
    print_banner
    echo -e "${C_BOLD}LOCALLY INSTALLED MODULES (./modules):${C_RESET}"
    if [ -d "$LOCAL_MODULES_DIR" ] && [ "$(ls -A "$LOCAL_MODULES_DIR" 2>/dev/null)" ]; then
        for d in "$LOCAL_MODULES_DIR"/*; do
            if [ -d "$d" ]; then
                local bname=$(basename "$d")
                local ver="1.0.0"
                local lang="loop"
                local desc=""
                if [ -f "$d/smile.json" ]; then
                    ver=$(python3 -c "import json; print(json.load(open('$d/smile.json')).get('version','1.0.0'))" 2>/dev/null || echo "1.0.0")
                    lang=$(python3 -c "import json; print(json.load(open('$d/smile.json')).get('language','loop'))" 2>/dev/null || echo "loop")
                    desc=$(python3 -c "import json; print(json.load(open('$d/smile.json')).get('description',''))" 2>/dev/null || echo "")
                fi
                local lang_upper=$(echo "$lang" | tr '[:lower:]' '[:upper:]')
                local is_symlink=""
                if [ -L "$d" ]; then is_symlink=" ${C_DIM}(symlink)${C_RESET}"; fi
                echo -e "  • ${C_CYAN}$bname${C_RESET} ${C_DIM}v$ver${C_RESET} [${C_AQUA}$lang_upper${C_RESET}]$is_symlink"
                if [ -n "$desc" ]; then
                    echo -e "    ${C_DIM}$desc${C_RESET}"
                fi
            fi
        done
    else
        echo -e "  ${C_DIM}(No local modules installed in ./modules. Use smiledev install <name>)${C_RESET}"
    fi

    echo ""
    echo -e "${C_BOLD}GLOBAL ENVIRONMENT MODULES (~/.smiledev/modules):${C_RESET}"
    if [ -d "$GLOBAL_MODULES_DIR" ] && [ "$(ls -A "$GLOBAL_MODULES_DIR" 2>/dev/null)" ]; then
        for d in "$GLOBAL_MODULES_DIR"/*; do
            if [ -d "$d" ]; then
                local bname=$(basename "$d")
                local ver="1.0.0"
                local is_symlink=""
                if [ -L "$d" ]; then is_symlink=" ${C_DIM}(linked)${C_RESET}"; fi
                if [ -f "$d/smile.json" ]; then
                    ver=$(python3 -c "import json; print(json.load(open('$d/smile.json')).get('version','1.0.0'))" 2>/dev/null || echo "1.0.0")
                fi
                echo -e "  • ${C_PURPLE}$bname${C_RESET} ${C_DIM}v$ver${C_RESET}$is_symlink"
            fi
        done
    else
        echo -e "  ${C_DIM}(No global modules)${C_RESET}"
    fi
    echo ""
}

# ── 9. COMMAND: SEARCH (Search LoopModules) ──
cmd_search() {
    local term="$1"
    echo -e "${C_AQUA}🔎 Searching in PostgreSQL LoopModules:${C_RESET} '${term:-all}'..."
    local res
    if [ -z "$term" ]; then
        res=$(curl -s -H "apikey: $SUPABASE_KEY" \
                     -H "Authorization: Bearer $SUPABASE_KEY" \
                     "$SUPABASE_URL/rest/v1/loop_modules?select=name,version,language,author_name,description&limit=25")
    else
        res=$(curl -s -H "apikey: $SUPABASE_KEY" \
                     -H "Authorization: Bearer $SUPABASE_KEY" \
                     "$SUPABASE_URL/rest/v1/loop_modules?or=(name.ilike.*$term*,description.ilike.*$term*)&select=name,version,language,author_name,description")
    fi

    python3 -c "
import json
try:
    data = json.loads('''$res''')
except Exception:
    data = []

if not data:
    print('  ${C_YELLOW}No matching modules found.${C_RESET}')
else:
    print('')
    for m in data:
        lang = (m.get('language') or 'loop').upper()
        print(f\"  • ${C_BOLD}${C_CYAN}{m['name']}${C_RESET} v{m['version']} [${C_AQUA}{lang}${C_RESET}] by {m.get('author_name', 'Community')}\")
        if m.get('description'):
            print(f\"    ${C_DIM}{m['description']}${C_RESET}\")
        print('')
"
}

# ── 10. COMMAND: INIT (Create new module / package) ──
cmd_init() {
    local name="$1"
    local lang="loop"
    local is_root=false
    shift || true

    while [[ "$#" -gt 0 ]]; do
        case $1 in
            --lang|-l) lang="$2"; shift ;;
            --root) is_root=true ;;
        esac
        shift || true
    done

    if [ -z "$name" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Specify module name. Example: ${C_CYAN}smiledev init my-engine-plugin --lang loop${C_RESET}"
        exit 1
    fi

    local target_dir="./modules/$name"
    if [ "$is_root" = true ]; then
        target_dir="./$name"
    fi
    mkdir -p "$target_dir"

    local entry=""
    if [ "$lang" == "cpp" ]; then entry="main.hpp"; fi
    if [ "$lang" == "python" ]; then entry="main.py"; fi
    if [ "$lang" == "loop" ]; then entry="main.loop"; fi
    if [ "$lang" == "c2" ]; then entry="main.c2"; fi

    cat <<EOF > "$target_dir/smile.json"
{
  "name": "$name",
  "version": "1.0.0",
  "language": "$lang",
  "entry_point": "$entry",
  "author_name": "Coki Developer",
  "description": "Module $name created with smiledev.",
  "license": "MIT",
  "dependencies": {}
}
EOF

    # Code template by language
    if [ "$lang" == "cpp" ]; then
        cat <<EOF > "$target_dir/$entry"
#pragma once
#include <iostream>

namespace $name {
    inline void hello() {
        std::cout << "Hello from C++ native module: $name" << std::endl;
    }
}
EOF
    elif [ "$lang" == "python" ]; then
        cat <<EOF > "$target_dir/$entry"
# $name: Python interoperability module for Looping
def init_module(vars_dict=None):
    print("Module $name initialized successfully in PyLoop.")
    return True

def hello():
    return "Hello from Python PyLoop module $name"
EOF
    elif [ "$lang" == "c2" ]; then
        cat <<EOF > "$target_dir/$entry"
// Declarative C2 Component Module: $name
component $name {
    theme "frosted_aqua_a17"
    pipeline "Direct-to-Vulkan"
    
    view MainCard {
        title: "$name Module"
        description: "Declarative UI component powered by Looping Engine"
    }
}
EOF
    else
        cat <<EOF > "$target_dir/$entry"
define module "$name" version 1.0:
    fn hello() => "Hello from Looping module $name"
EOF
    fi

    # Generate README.md
    cat <<EOF > "$target_dir/README.md"
# $name

> Official $lang module for Looping Engine and Loop OS ecosystem.

## Installation

\`\`\`bash
smiledev install $name
\`\`\`

## Usage in Looping

\`\`\`looping
use module "$name"

val message = hello()
print(message)
\`\`\`

---
*© 2026 Coki Studios & Holo Entertainment*
EOF

    # Generate .gitignore
    cat <<EOF > "$target_dir/.gitignore"
.DS_Store
*.tmp
*.log
build/
dist/
EOF

    echo -e "${C_GREEN}✔ [CREATED]${C_RESET} Module ${C_BOLD}$name${C_RESET} [$lang] initialized at: ${C_AQUA}$target_dir${C_RESET}"
    echo -e "   • Manifest:    ${C_DIM}$target_dir/smile.json${C_RESET}"
    echo -e "   • Entry point: ${C_DIM}$target_dir/$entry${C_RESET}"
    echo -e "   • Docs:        ${C_DIM}$target_dir/README.md${C_RESET}"
    echo -e "   To test locally: ${C_CYAN}smiledev run $target_dir/$entry${C_RESET}"
    echo -e "   To publish:      ${C_CYAN}smiledev upload-module $target_dir${C_RESET}"
}

# ── 10.5. COMMAND: VALIDATE (Verify module integrity & manifest) ──
cmd_validate() {
    local target_dir="."
    local json_output=false

    while [[ $# -gt 0 ]]; do
        case "$1" in
            --json)
                json_output=true
                shift
                ;;
            *)
                target_dir="$1"
                shift
                ;;
        esac
    done

    if [ ! -d "$target_dir" ]; then
        if [ "$json_output" = true ]; then
            echo "{\"valid\": false, \"errors\": [\"Directory not found: $target_dir\"]}"
        else
            echo -e "${C_RED}✘ Error:${C_RESET} Directory not found: $target_dir"
        fi
        exit 1
    fi

    local manifest="$target_dir/smile.json"
    if [ ! -f "$manifest" ]; then
        if [ "$json_output" = true ]; then
            echo "{\"valid\": false, \"errors\": [\"Missing smile.json in $target_dir\"]}"
        else
            echo -e "${C_RED}✘ Invalid Module:${C_RESET} Missing ${C_BOLD}smile.json${C_RESET} in $target_dir"
            echo -e "   Every module requires a manifest. See ${C_CYAN}https://cokistudios.github.io/smiledev.html#manual-guide${C_RESET}"
        fi
        exit 1
    fi

    if [ "$json_output" = false ]; then
        echo -e "${C_CYAN}[SMILEDEV VALIDATOR]${C_RESET} Validating module at: ${C_BOLD}$target_dir${C_RESET}..."
    fi

    python3 -c "
import json, sys, os, re

try:
    with open('$manifest', 'r', encoding='utf-8') as f:
        mf = json.load(f)
except Exception as e:
    if '$json_output' == 'true':
        print(json.dumps({'valid': False, 'errors': [f'JSON Parse Error: {str(e)}']}))
    else:
        print('${C_RED}✘ JSON Parse Error in smile.json:${C_RESET} ' + str(e))
    sys.exit(1)

errors = []
warnings = []

name = mf.get('name', '')
if not name:
    errors.append('Missing required field: \"name\"')
elif not re.match(r'^[a-z0-9][a-z0-9\-_.]*$', name):
    errors.append(f'Invalid module name \"{name}\". Must be lowercase alphanumeric, hyphens, underscores or dots.')

ver = mf.get('version', '')
if not ver:
    errors.append('Missing required field: \"version\"')
elif not re.match(r'^\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?$', ver):
    warnings.append(f'Version \"{ver}\" is not strict SemVer (recommended format: 1.0.0).')

lang = mf.get('language', '')
allowed_langs = ['loop', 'cpp', 'python', 'node', 'c2', 'hybrid']
if not lang:
    errors.append('Missing required field: "language" (must be one of: ' + ', '.join(allowed_langs) + ')')
elif lang.lower() not in allowed_langs:
    errors.append(f'Unknown language "{lang}". Allowed: {allowed_langs}')

entry = mf.get('entry_point', '')
if not entry:
    errors.append('Missing required field: "entry_point"')
else:
    entry_path = os.path.join('$target_dir', entry)
    if not os.path.isfile(entry_path):
        errors.append(f'Entry point file "{entry}" not found at {entry_path}')
    else:
        ext = os.path.splitext(entry)[1].lower()
        if lang.lower() == 'loop' and ext not in ['.loop', '.ruup']:
            warnings.append(f'Entry point "{entry}" extension does not match language loop (.loop / .ruup expected).')
        elif lang.lower() == 'cpp' and ext not in ['.hpp', '.h', '.cpp']:
            warnings.append(f'Entry point "{entry}" extension does not match language cpp (.hpp / .h expected).')
        elif lang.lower() == 'python' and ext not in ['.py']:
            warnings.append(f'Entry point "{entry}" extension does not match language python (.py expected).')
        elif lang.lower() == 'node' and ext not in ['.js', '.mjs', '.cjs']:
            warnings.append(f'Entry point "{entry}" extension does not match language node (.js expected).')

if lang.lower() == 'hybrid':
    components = mf.get('components', {})
    subfiles = mf.get('subfiles', [])
    if not components and not subfiles:
        warnings.append('Hybrid module should declare "components" or "subfiles" in smile.json.')
    for comp_key, comp_file in components.items():
        comp_path = os.path.join('$target_dir', comp_file)
        if not os.path.isfile(comp_path):
            errors.append(f'Hybrid component "{comp_key}" file "{comp_file}" not found.')
    for sf in subfiles:
        sf_path = os.path.join('$target_dir', sf)
        if not os.path.isfile(sf_path):
            errors.append(f'Declared subfile "{sf}" not found.')

desc = mf.get('description', '')
if not desc or len(desc.strip()) < 8:
    warnings.append('Description is missing or very brief (recommended > 10 characters).')

readme_path = os.path.join('$target_dir', 'README.md')
if not os.path.isfile(readme_path):
    warnings.append('No README.md found in module root. Recommended for developer documentation.')

is_valid = len(errors) == 0

if '$json_output' == 'true':
    print(json.dumps({
        'valid': is_valid,
        'name': name,
        'version': ver,
        'language': lang,
        'entry_point': entry,
        'errors': errors,
        'warnings': warnings
    }))
else:
    if errors:
        print('${C_RED}✘ Validation FAILED with ' + str(len(errors)) + ' error(s):${C_RESET}')
        for err in errors:
            print('  • ${C_RED}' + err + '${C_RESET}')
        sys.exit(1)

    if warnings:
        print('${C_YELLOW}⚠ Validation PASSED with ' + str(len(warnings)) + ' recommendation(s):${C_RESET}')
        for w in warnings:
            print('  • ${C_YELLOW}' + w + '${C_RESET}')
    else:
        print('${C_GREEN}✔ Manifest and files verified!${C_RESET}')

    print(f'   • Name        : {name}')
    print(f'   • Version     : {ver}')
    print(f'   • Language    : {lang}')
    print(f'   • Entry Point : {entry}')
    print(f'   • License     : {mf.get(\"license\", \"MIT\")}')
    print(f'   • Author      : {mf.get(\"author_name\", \"Community\")}')
    print('${C_GREEN}✔ Module is 100% compliant with Looping & smiledev standards!${C_RESET}')
"
}

# ── 11. COMMAND: PACK (Bundle module into tarball) ──
cmd_pack() {
    local target_dir="${1:-.}"
    cmd_validate "$target_dir" || {
        echo -e "${C_RED}✘ Packaging aborted due to validation errors.${C_RESET}"
        exit 1
    }
    if [ ! -d "$target_dir" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Directory not found: $target_dir"
        exit 1
    fi
    local manifest="$target_dir/smile.json"
    if [ ! -f "$manifest" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} No smile.json found in $target_dir"
        exit 1
    fi
    local mod_name mod_ver
    mod_name=$(python3 -c "import json; print(json.load(open('$manifest')).get('name','module'))" 2>/dev/null || echo "module")
    mod_ver=$(python3 -c "import json; print(json.load(open('$manifest')).get('version','1.0.0'))" 2>/dev/null || echo "1.0.0")
    local out_tar="${mod_name}-v${mod_ver}.tar.gz"
    tar -czf "$out_tar" -C "$target_dir" .
    local sha=$(shasum -a 256 "$out_tar" | awk '{print $1}')
    echo -e "${C_GREEN}✔ [PACKAGED]${C_RESET} Created module release bundle: ${C_BOLD}$out_tar${C_RESET}"
    echo -e "   • SHA256: ${C_DIM}$sha${C_RESET}"
    echo -e "   • Size:   $(ls -lh "$out_tar" | awk '{print $5}')"
}

# ── 12. COMMAND: LINK & UNLINK ──
cmd_link() {
    local target_dir="${1:-.}"
    local manifest="$target_dir/smile.json"
    if [ ! -f "$manifest" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} No smile.json found in $target_dir"
        exit 1
    fi
    local mod_name
    mod_name=$(python3 -c "import json; print(json.load(open('$manifest')).get('name',''))" 2>/dev/null || echo "")
    if [ -z "$mod_name" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Module name missing in smile.json"
        exit 1
    fi
    local abs_path
    abs_path="$(cd "$target_dir" && pwd)"
    mkdir -p "$GLOBAL_MODULES_DIR"
    ln -sfn "$abs_path" "$GLOBAL_MODULES_DIR/$mod_name"
    echo -e "${C_GREEN}✔ [LINKED]${C_RESET} Symlinked ${C_BOLD}$mod_name${C_RESET} -> ${C_AQUA}$GLOBAL_MODULES_DIR/$mod_name${C_RESET}"
}

cmd_unlink() {
    local mod_name="$1"
    if [ -z "$mod_name" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Specify module name to unlink."
        exit 1
    fi
    if [ -L "$GLOBAL_MODULES_DIR/$mod_name" ]; then
        rm "$GLOBAL_MODULES_DIR/$mod_name"
        echo -e "${C_GREEN}✔ [UNLINKED]${C_RESET} Removed symlink for ${C_BOLD}$mod_name${C_RESET} from global cache."
    else
        echo -e "${C_YELLOW}⚠ Module '$mod_name' is not a linked symlink in global cache.${C_RESET}"
    fi
}

# ── 13. COMMAND: TEST ──
cmd_test() {
    local target="$1"
    local looping_bin="$(command -v looping 2>/dev/null || echo "")"
    if [ -z "$looping_bin" ] && [ -x "./bin/looping" ]; then
        looping_bin="./bin/looping"
    fi
    if [ -z "$looping_bin" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Looping engine binary not found."
        exit 1
    fi

    if [ -z "$target" ]; then
        if [ -f "test.loop" ]; then
            exec "$looping_bin" "test.loop"
        elif [ -f "smile.json" ]; then
            local entry=$(python3 -c "import json; print(json.load(open('smile.json')).get('entry_point',''))" 2>/dev/null || echo "")
            if [ -n "$entry" ] && [ -f "$entry" ]; then
                echo -e "${C_CYAN}Running module entry test:${C_RESET} $entry"
                exec "$looping_bin" "$entry"
            fi
        fi
        echo -e "${C_RED}✘ Error:${C_RESET} No test.loop or module entry found in current directory."
        exit 1
    else
        if [ -f "$LOCAL_MODULES_DIR/$target/test.loop" ]; then
            exec "$looping_bin" "$LOCAL_MODULES_DIR/$target/test.loop"
        elif [ -f "$LOCAL_MODULES_DIR/$target/smile.json" ]; then
            local entry=$(python3 -c "import json; print(json.load(open('$LOCAL_MODULES_DIR/$target/smile.json')).get('entry_point',''))" 2>/dev/null || echo "")
            exec "$looping_bin" "$LOCAL_MODULES_DIR/$target/$entry"
        else
            echo -e "${C_RED}✘ Error:${C_RESET} Module '$target' not found in ./modules."
            exit 1
        fi
    fi
}

# ── 14. COMMAND: CLEAN ──
cmd_clean() {
    echo -e "${C_CYAN}🧹 Cleaning smiledev caches and temporary files...${C_RESET}"
    rm -rf /tmp/smiledev* 2>/dev/null || true
    rm -rf "$CONFIG_DIR/cache" 2>/dev/null || true
    echo -e "${C_GREEN}✔ Cache cleaned.${C_RESET}"
}

# ── 15. AUTO-UPDATER SUB-SYSTEM ──
check_for_updates() {
    local now
    now=$(date +%s 2>/dev/null || echo 0)
    local last_check=0
    if [ -f "$CONFIG_DIR/last_update_check" ]; then
        last_check=$(cat "$CONFIG_DIR/last_update_check" 2>/dev/null || echo 0)
    fi

    if [ $((now - last_check)) -gt 86400 ]; then
        mkdir -p "$CONFIG_DIR" 2>/dev/null || true
        echo "$now" > "$CONFIG_DIR/last_update_check" 2>/dev/null || true
        (
            remote_json=$(curl -fsSL --connect-timeout 2 "https://repo.cokistudios.com/engine/latest.json" 2>/dev/null || true)
            if [ -n "$remote_json" ]; then
                r_ver=$(echo "$remote_json" | grep -o '"version"[[:space:]]*:[[:space:]]*"[^"]*"' | head -n1 | cut -d'"' -f4)
                if [ -n "$r_ver" ]; then
                    echo "$r_ver" > "$CONFIG_DIR/latest_available_version" 2>/dev/null || true
                fi
            fi
        ) &>/dev/null &
    fi

    if [ -f "$CONFIG_DIR/latest_available_version" ]; then
        local avail_ver
        avail_ver=$(cat "$CONFIG_DIR/latest_available_version" 2>/dev/null || echo "")
        if [ -n "$avail_ver" ] && [ "$avail_ver" != "$SMILE_VERSION" ]; then
            local is_newer
            is_newer=$(python3 -c "
import sys, re
def norm(v):
    base = v.split('-')[0]
    nums = [int(x) for x in re.findall(r'\d+', base)]
    while len(nums) < 3: nums.append(0)
    return nums
try:
    print('1' if norm('$avail_ver') > norm('$SMILE_VERSION') else '0')
except Exception:
    print('0')
" 2>/dev/null || echo 0)
            if [ "$is_newer" = "1" ]; then
                echo -e "${C_YELLOW}╭────────────────────────────────────────────────────────────╮${C_RESET}"
                echo -e "${C_YELLOW}│${C_RESET} 💡 ${C_BOLD}Update Available:${C_RESET} Looping & smiledev ${C_GREEN}v$avail_ver${C_RESET}             ${C_YELLOW}│${C_RESET}"
                echo -e "${C_YELLOW}│${C_RESET}    Run ${C_CYAN}smiledev upgrade${C_RESET} to update automatically.          ${C_YELLOW}│${C_RESET}"
                echo -e "${C_YELLOW}╰────────────────────────────────────────────────────────────╯${C_RESET}\n"
            fi
        fi
    fi
}

cmd_upgrade() {
    local force_flag=false
    local req_channel="$SMILE_CHANNEL"
    while [[ "$#" -gt 0 ]]; do
        case $1 in
            --force|-f) force_flag=true ;;
            --channel=*) req_channel="${1#*=}" ;;
            --channel|-c) shift || true; req_channel="${1:-prod}" ;;
            --alpha|--alpha!|--1.1a|--1.1A|--1.1.2a|--1.1.2A) req_channel="alpha" ;;
            --omega|--rc) req_channel="omega" ;;
            --prod|--stable) req_channel="prod" ;;
        esac
        shift || true
    done

    local ch_info
    ch_info="$(resolve_channel_brand "$req_channel")"
    local ch_title="$(echo "$ch_info" | cut -d'|' -f1)"
    local ch_tier="$(echo "$ch_info" | cut -d'|' -f2)"
    local ch_key="$(echo "$ch_info" | cut -d'|' -f4)"

    echo -e "${C_CYAN}═══════════════════════════════════════════════════════════════${C_RESET}"
    echo -e "${C_BOLD}🚀 COKI STUDIOS ENGINE & SMILEDEV AUTO-UPDATER [$ch_title — $ch_tier]${C_RESET}"
    echo -e "${C_CYAN}═══════════════════════════════════════════════════════════════${C_RESET}"
    echo -e "${C_DIM}Checking for $ch_title ($ch_key) updates on repo.cokistudios.com/engine...${C_RESET}"

    local manifest=""
    local manifest_url="https://raw.githubusercontent.com/CokiStudios/cokistudios.github.io/main/engine/latest.json"
    manifest=$(curl -fsSL --connect-timeout 5 -H "Cache-Control: no-cache" "$manifest_url" 2>/dev/null || true)

    if [ -z "$manifest" ]; then
        manifest=$(curl -fsSL --connect-timeout 5 -H "Cache-Control: no-cache" "https://repo.cokistudios.com/engine/latest.json" 2>/dev/null || true)
    fi

    if [ -z "$manifest" ]; then
        echo -e "${C_DIM}Primary repo mirror unreachable. Trying fallback CDN...${C_RESET}"
        manifest=$(curl -fsSL --connect-timeout 5 -H "Cache-Control: no-cache" "https://cokistudios.github.io/engine/latest.json" 2>/dev/null || true)
    fi

    local self_bin
    self_bin="$(realpath "$0" 2>/dev/null || readlink -f "$0" 2>/dev/null || echo "$0")"
    local repo_root="$(cd "$(dirname "$self_bin")/.." 2>/dev/null && pwd || true)"

    if [ -z "$manifest" ] && [ -n "$repo_root" ] && [ -f "$repo_root/engine/latest.json" ]; then
        echo -e "${C_DIM}Network mirrors unreachable. Using local workspace manifest...${C_RESET}"
        manifest=$(cat "$repo_root/engine/latest.json")
    fi

    if [ -z "$manifest" ] && [ -n "$repo_root" ] && [ -f "$repo_root/public/engine/latest.json" ]; then
        echo -e "${C_DIM}Network mirrors unreachable. Using local public manifest...${C_RESET}"
        manifest=$(cat "$repo_root/public/engine/latest.json")
    fi

    if [ -z "$manifest" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Unable to connect to repo.cokistudios.com/engine or fallback CDN."
        echo -e "   Please verify your network connection and try again."
        exit 1
    fi

    local parsed_info
    parsed_info=$(python3 -c "
import sys, json
try:
    data = json.loads('''$manifest''')
    ch_data = data.get('channels', {}).get('$ch_key', {})
    v = ch_data.get('version') or data.get('version', '')
    ev = ch_data.get('engine_version') or data.get('version', '')
    url = ch_data.get('tarball_url') or data.get('tarball_url', '')
    sha = ch_data.get('tarball_sha256') or data.get('tarball_sha256', '')
    notes = ch_data.get('release_notes') or data.get('release_notes', 'Performance and stability improvements.')
    print(f'{v}\t{ev}\t{url}\t{sha}\t{notes}')
except Exception as e:
    sys.exit(1)
" 2>/dev/null || echo "")

    if [ -z "$parsed_info" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Invalid or corrupt manifest received from repo.cokistudios.com/engine."
        exit 1
    fi

    local remote_ver
    local remote_engine_ver
    local tarball_url
    local expected_sha
    local release_notes
    remote_ver=$(echo "$parsed_info" | awk -F'\t' '{print $1}')
    remote_engine_ver=$(echo "$parsed_info" | awk -F'\t' '{print $2}')
    tarball_url=$(echo "$parsed_info" | awk -F'\t' '{print $3}')
    expected_sha=$(echo "$parsed_info" | awk -F'\t' '{print $4}')
    release_notes=$(echo "$parsed_info" | awk -F'\t' '{print $5}')

    local looping_bin
    looping_bin="$(command -v looping 2>/dev/null || true)"
    if [ -z "$looping_bin" ] && [ -x "/usr/local/bin/looping" ]; then
        looping_bin="/usr/local/bin/looping"
    elif [ -z "$looping_bin" ] && [ -x "/opt/homebrew/bin/looping" ]; then
        looping_bin="/opt/homebrew/bin/looping"
    fi

    local current_loop_ver="not-installed"
    if [ -n "$looping_bin" ] && [ -x "$looping_bin" ]; then
        current_loop_ver=$("$looping_bin" --version 2>/dev/null | grep -oE 'v[0-9]+(\.[0-9]+)*([A-Za-z0-9.-]*)?' | head -n1 | sed 's/^v//' || echo "unknown")
    fi

    local curr_ch_info
    curr_ch_info="$(resolve_channel_brand "$SMILE_CHANNEL")"
    local curr_ch_ver="$(echo "$curr_ch_info" | cut -d'|' -f3)"
    local curr_ch_key="$(echo "$curr_ch_info" | cut -d'|' -f4)"

    echo -e "• Active smiledev channel:    ${C_AQUA}$curr_ch_key (v$curr_ch_ver)${C_RESET}"
    echo -e "• Installed looping version:  ${C_AQUA}v$current_loop_ver${C_RESET}"
    echo -e "• Target $ch_title version:   ${C_GREEN}v$remote_ver${C_RESET}"

    local needs_update=0
    if [ "$force_flag" = true ]; then
        needs_update=1
    elif [ "$curr_ch_key" != "$ch_key" ] || [ "$curr_ch_ver" != "$remote_ver" ] || [ "$current_loop_ver" != "$remote_engine_ver" ]; then
        needs_update=1
    fi

    if [ "$needs_update" -eq 0 ]; then
        echo -e "\n${C_GREEN}✔ Looping and smiledev are already at the latest version ($ch_title v$remote_ver)!${C_RESET}"
        echo -e "${C_DIM}Tip: Pass --force to reinstall anyway: 'smiledev upgrade --force'${C_RESET}"
        return 0
    fi

    echo -e "\n${C_YELLOW}⚡ Initiating upgrade to v$remote_ver...${C_RESET}"
    echo -e "   ${C_DIM}Changelog: $release_notes${C_RESET}"
    echo -e "   Downloading release tarball from: ${C_CYAN}$tarball_url${C_RESET}"

    local tmp_dir
    tmp_dir=$(mktemp -d /tmp/smiledev-upgrade-XXXXXX)
    local tmp_tar="$tmp_dir/engine.tar.gz"

    local download_success=false
    # 1. Try local workspace package first if available
    if [ -n "$repo_root" ] && [ -f "$repo_root/engine/looping-v${remote_ver}-darwin-universal.tar.gz" ]; then
        cp "$repo_root/engine/looping-v${remote_ver}-darwin-universal.tar.gz" "$tmp_tar"
        download_success=true
    elif [ -n "$repo_root" ] && [ -f "$repo_root/dist/looping-v${remote_ver}-darwin-universal.tar.gz" ]; then
        cp "$repo_root/dist/looping-v${remote_ver}-darwin-universal.tar.gz" "$tmp_tar"
        download_success=true
    elif [ -n "$repo_root" ] && [ -f "$repo_root/public/engine/looping-v${remote_ver}-darwin-universal.tar.gz" ]; then
        cp "$repo_root/public/engine/looping-v${remote_ver}-darwin-universal.tar.gz" "$tmp_tar"
        download_success=true
    fi

    # 2. Try direct GitHub raw / CDN / Engine release stream
    if [ "$download_success" != true ]; then
        local tar_basename
        tar_basename=$(basename "$tarball_url")
        local gh_direct_url="https://github.com/CokiStudios/cokistudios.github.io/raw/main/engine/${tar_basename}"
        local gh_engine_rel_url="https://github.com/CokiStudios/engine/releases/download/v1.1/${tar_basename}"
        if curl -fsSL -H "Cache-Control: no-cache" "$gh_direct_url" -o "$tmp_tar" 2>/dev/null; then
            download_success=true
        elif curl -fsSL -H "Cache-Control: no-cache" "$tarball_url" -o "$tmp_tar" 2>/dev/null; then
            download_success=true
        elif curl -fsSL -L -H "Cache-Control: no-cache" "$gh_engine_rel_url" -o "$tmp_tar" 2>/dev/null; then
            download_success=true
        fi
    fi

    if [ "$download_success" != true ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Failed to download release tarball from $tarball_url."
        rm -rf "$tmp_dir"
        exit 1
    fi

    if [ -n "$expected_sha" ]; then
        local actual_sha
        actual_sha=$(shasum -a 256 "$tmp_tar" | awk '{print $1}')
        if [ "$actual_sha" != "$expected_sha" ]; then
            # Direct GitHub raw fallback in case CDN returned stale cached object
            local tar_basename
            tar_basename=$(basename "$tarball_url")
            local gh_direct_url="https://github.com/CokiStudios/cokistudios.github.io/raw/main/engine/${tar_basename}"
            echo -e "   ${C_DIM}CDN cache mismatch. Fetching direct stream from GitHub release...${C_RESET}"
            curl -fsSL -H "Cache-Control: no-cache" "$gh_direct_url" -o "$tmp_tar" 2>/dev/null || true
            actual_sha=$(shasum -a 256 "$tmp_tar" | awk '{print $1}')
        fi

        if [ "$actual_sha" != "$expected_sha" ]; then
            echo -e "${C_RED}✘ Security Verification Failed:${C_RESET} SHA256 mismatch!"
            echo -e "   Expected: $expected_sha"
            echo -e "   Got:      $actual_sha"
            rm -rf "$tmp_dir"
            exit 1
        fi
        echo -e "   ${C_GREEN}✔ Checksum verified:${C_RESET} ${C_DIM}$actual_sha${C_RESET}"
    fi

    echo -e "   Unpacking universal binaries..."
    tar -xzf "$tmp_tar" -C "$tmp_dir"

    if [ ! -f "$tmp_dir/looping" ] || [ ! -f "$tmp_dir/smiledev" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Release tarball does not contain required 'looping' or 'smiledev' binaries."
        rm -rf "$tmp_dir"
        exit 1
    fi

    chmod 755 "$tmp_dir/looping" "$tmp_dir/smiledev"

    local target_dir="/usr/local/bin"
    if [ -n "$looping_bin" ]; then
        target_dir="$(dirname "$looping_bin")"
    elif [ -d "/opt/homebrew/bin" ] && [ -w "/opt/homebrew/bin" ]; then
        target_dir="/opt/homebrew/bin"
    fi

    echo -e "   Installing into: ${C_BOLD}$target_dir${C_RESET}..."
    local need_sudo=false
    if [ ! -w "$target_dir" ]; then
        need_sudo=true
    fi

    if [ "$need_sudo" = true ]; then
        echo -e "   ${C_YELLOW}🔑 Administrator permissions required for $target_dir. Prompting sudo...${C_RESET}"
        sudo cp "$tmp_dir/looping" "$target_dir/looping"
        sudo chmod 755 "$target_dir/looping"
        sudo ln -sf "$target_dir/looping" "$target_dir/ruuping" 2>/dev/null || true
        sudo cp "$tmp_dir/smiledev" "$target_dir/smiledev"
        sudo chmod 755 "$target_dir/smiledev"
    else
        cp "$tmp_dir/looping" "$target_dir/looping"
        chmod 755 "$target_dir/looping"
        ln -sf "$target_dir/looping" "$target_dir/ruuping" 2>/dev/null || true
        cp "$tmp_dir/smiledev" "$target_dir/smiledev"
        chmod 755 "$target_dir/smiledev"
    fi

    local self_bin
    self_bin="$(realpath "$0" 2>/dev/null || readlink -f "$0" 2>/dev/null || echo "$0")"
    if [ -f "$self_bin" ] && [ "$self_bin" != "$target_dir/smiledev" ] && [ -w "$self_bin" ]; then
        cp "$tmp_dir/smiledev" "$self_bin"
        chmod 755 "$self_bin"
        local self_dir
        self_dir="$(dirname "$self_bin")"
        if [ -f "$self_dir/looping" ] && [ -w "$self_dir/looping" ]; then
            cp "$tmp_dir/looping" "$self_dir/looping"
            chmod 755 "$self_dir/looping"
        fi
    fi

    for extra_dir in "$HOME/.local/bin" "/opt/homebrew/bin"; do
        if [ -d "$extra_dir" ] && [ -w "$extra_dir" ] && [ "$extra_dir" != "$target_dir" ]; then
            if [ -f "$extra_dir/smiledev" ] || [ -f "$extra_dir/looping" ]; then
                if [ ! -L "$extra_dir/looping" ] && [ ! -L "$extra_dir/smiledev" ]; then
                    cp -f "$tmp_dir/looping" "$extra_dir/looping" 2>/dev/null || true
                    chmod 755 "$extra_dir/looping" 2>/dev/null || true
                    ln -sf "$extra_dir/looping" "$extra_dir/ruuping" 2>/dev/null || true
                    cp -f "$tmp_dir/smiledev" "$extra_dir/smiledev" 2>/dev/null || true
                    chmod 755 "$extra_dir/smiledev" 2>/dev/null || true
                    echo -e "   ${C_GREEN}✔ Synchronized binaries in:${C_RESET} $extra_dir"
                fi
            fi
        fi
    done

    rm -rf "$tmp_dir"
    mkdir -p "$CONFIG_DIR" 2>/dev/null || true
    echo "$ch_key" > "$CHANNEL_FILE" 2>/dev/null || true
    rm -f "$CONFIG_DIR/latest_available_version" 2>/dev/null || true
    date +%s > "$CONFIG_DIR/last_update_check" 2>/dev/null || true

    echo -e "\n${C_GREEN}═══════════════════════════════════════════════════════════════${C_RESET}"
    echo -e "${C_BOLD}✨ UPGRADE SUCCESSFUL! Looping & smiledev [$ch_title v$remote_ver]${C_RESET}"
    echo -e "${C_GREEN}═══════════════════════════════════════════════════════════════${C_RESET}"
    if [ -x "$target_dir/looping" ]; then
        echo -n "• Looping:  "
        LOOPING_CHANNEL="$ch_key" "$target_dir/looping" --version | head -n1
    fi
    if [ -x "$target_dir/smiledev" ]; then
        echo -n "• smiledev: "
        "$target_dir/smiledev" version --channel="$ch_key"
    fi
    echo ""
}

# ── 16. COMMAND: DOCTOR ──
cmd_doctor() {
    while [[ "$#" -gt 0 ]]; do
        case "$1" in
            --channel=*) SMILE_CHANNEL="${1#*=}" ;;
            --channel|-c) shift || true; SMILE_CHANNEL="${1:-prod}" ;;
            --alpha|--alpha!|--1.1a|--1.1A|--1.1.2a|--1.1.2A) SMILE_CHANNEL="alpha" ;;
            --omega|--rc) SMILE_CHANNEL="omega" ;;
            --prod|--stable) SMILE_CHANNEL="prod" ;;
        esac
        shift || true
    done
    local ch_info
    ch_info="$(resolve_channel_brand "$SMILE_CHANNEL")"
    local ch_title="$(echo "$ch_info" | cut -d'|' -f1)"
    local ch_tier="$(echo "$ch_info" | cut -d'|' -f2)"
    local ch_ver="$(echo "$ch_info" | cut -d'|' -f3)"
    local ch_key="$(echo "$ch_info" | cut -d'|' -f4)"
    print_banner
    echo -e "${C_BOLD}${C_CYAN}[SMILEDEV DOCTOR]${C_RESET} Running diagnostics on Looping & Package Manager...\n"
    echo -e "  ${C_GREEN}✔${C_RESET} smiledev CLI      : v$ch_ver ($ch_title — $ch_tier | $0)"
    local lbin
    lbin=$(command -v looping 2>/dev/null || echo "")
    if [ -z "$lbin" ]; then
        if [ -f "./bin/looping.exe" ]; then
            lbin="./bin/looping.exe"
        elif [ -x "./bin/looping" ]; then
            lbin="./bin/looping"
        fi
    fi
    if [ -n "$lbin" ]; then
        local lver
        lver=$("$lbin" --version 2>/dev/null | head -n1 || echo "")
        if [ -z "$lver" ] && [ -f "./bin/looping_cli.mjs" ] && command -v node >/dev/null 2>&1; then
            lver=$(node "./bin/looping_cli.mjs" --version 2>/dev/null | head -n1 || echo "Looping Engine v1.1")
        fi
        echo -e "  ${C_GREEN}✔${C_RESET} Looping Binary    : ${lver:-Looping Engine v1.1} ($lbin)"
    else
        echo -e "  ${C_RED}✘${C_RESET} Looping Binary    : Not found in PATH"
    fi

    if python3 -c "import sys" >/dev/null 2>&1; then
        local pyver
        pyver=$(python3 --version 2>&1)
        echo -e "  ${C_GREEN}✔${C_RESET} Python Interop    : $pyver"
    else
        echo -e "  ${C_YELLOW}⚠${C_RESET} Python Interop    : python3 not found"
    fi

    if command -v clang++ >/dev/null 2>&1; then
        local cxxver
        cxxver=$(clang++ --version 2>/dev/null | head -n1)
        echo -e "  ${C_GREEN}✔${C_RESET} C++ Toolchain     : $cxxver"
    elif command -v g++ >/dev/null 2>&1; then
        local cxxver
        cxxver=$(g++ --version 2>/dev/null | head -n1)
        echo -e "  ${C_GREEN}✔${C_RESET} C++ Toolchain     : $cxxver"
    elif command -v cl.exe >/dev/null 2>&1 || [ -d "/c/Program Files/Microsoft Visual Studio" ]; then
        echo -e "  ${C_GREEN}✔${C_RESET} C++ Toolchain     : Microsoft Visual C++ (MSVC C++17)"
    else
        echo -e "  ${C_YELLOW}⚠${C_RESET} C++ Toolchain     : C++17 compiler not found"
    fi

    if command -v brew >/dev/null 2>&1; then
        if brew tap 2>/dev/null | grep -qi "cokistudios/tap"; then
            echo -e "  ${C_GREEN}✔${C_RESET} Homebrew Tap      : cokistudios/tap (Configured)"
        else
            echo -e "  ${C_YELLOW}•${C_RESET} Homebrew Tap      : Run 'brew tap cokistudios/tap' to install via brew"
        fi
    fi

    echo -e "  ${C_GREEN}✔${C_RESET} Global Modules    : $GLOBAL_MODULES_DIR"
    echo -e "  ${C_GREEN}✔${C_RESET} Local Modules     : $LOCAL_MODULES_DIR"

    if [ -f "$AUTH_FILE" ]; then
        echo -e "  ${C_GREEN}✔${C_RESET} CS ID Auth        : Authenticated ($AUTH_FILE)"
    else
        echo -e "  ${C_YELLOW}•${C_RESET} CS ID Auth        : Anonymous (run 'smiledev login' to publish)"
    fi

    if curl -fsSL --max-time 4 "https://repo.cokistudios.com/engine/latest.json" >/dev/null 2>&1; then
        echo -e "  ${C_GREEN}✔${C_RESET} Engine Repository : Online (https://repo.cokistudios.com/engine)"
    else
        echo -e "  ${C_YELLOW}•${C_RESET} Engine Repository : Offline / Local Workspace Mode"
    fi

    local rcount
    rcount=$(python3 -c "import json, os; print(len(json.load(open('$CONFIG_FILE')).get('repos', [])) if os.path.exists('$CONFIG_FILE') else 0)" 2>/dev/null || echo "0")
    echo -e "  ${C_GREEN}✔${C_RESET} Active Repos      : LoopModules PostgreSQL + $rcount external mirror(s) (run 'smiledev list-repos')"
    echo ""
}

# ── 17. COMMAND: RUN ──
cmd_run() {
    local target="$1"
    if [ -z "$target" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Specify a .loop script or installed module name to run."
        exit 1
    fi
    shift
    local lbin="$(command -v looping 2>/dev/null || echo "")"
    if [ -z "$lbin" ] && [ -x "./bin/looping" ]; then
        lbin="./bin/looping"
    fi
    if [ -z "$lbin" ]; then
        echo -e "${C_RED}✘ Error:${C_RESET} Looping binary not found."
        exit 1
    fi

    if [ -f "$target" ]; then
        exec "$lbin" "$target" "$@"
    elif [ -f "$LOCAL_MODULES_DIR/$target/$target.loop" ]; then
        exec "$lbin" "$LOCAL_MODULES_DIR/$target/$target.loop" "$@"
    elif [ -f "$LOCAL_MODULES_DIR/$target/main.loop" ]; then
        exec "$lbin" "$LOCAL_MODULES_DIR/$target/main.loop" "$@"
    elif [ -f "$GLOBAL_MODULES_DIR/$target/$target.loop" ]; then
        exec "$lbin" "$GLOBAL_MODULES_DIR/$target/$target.loop" "$@"
    elif [ -f "$GLOBAL_MODULES_DIR/$target/main.loop" ]; then
        exec "$lbin" "$GLOBAL_MODULES_DIR/$target/main.loop" "$@"
    else
        echo -e "${C_RED}✘ Error:${C_RESET} Could not find script or module '$target'."
        exit 1
    fi
}

# ── COMMAND ROUTER ──
case "$1" in
    help|-h|--help|version|-v|--version|upgrade|self-update|upgrade-engine|"")
        ;;
    *)
        check_for_updates || true
        ;;
esac

case "$1" in
    install|add|i)
        shift
        cmd_install "$@"
        ;;
    uninstall|remove|rm)
        shift
        cmd_uninstall "$@"
        ;;
    view|ver|info)
        shift
        cmd_ver "$@"
        ;;
    upload-module|upload|publish)
        shift
        cmd_upload "$@"
        ;;
    add-repo)
        shift
        cmd_add_repo "$@"
        ;;
    list-repos|repos|list-repo)
        shift || true
        cmd_list_repos "$@"
        ;;
    remove-repo|rm-repo|del-repo)
        shift
        cmd_remove_repo "$@"
        ;;
    repo-spec|spec-repo)
        cmd_repo_spec
        ;;
    list|ls)
        cmd_list
        ;;
    search|find)
        shift
        cmd_search "$@"
        ;;
    init|new)
        shift
        cmd_init "$@"
        ;;
    pack|bundle)
        shift
        cmd_pack "$@"
        ;;
    link)
        shift
        cmd_link "$@"
        ;;
    unlink)
        shift
        cmd_unlink "$@"
        ;;
    test)
        shift
        cmd_test "$@"
        ;;
    validate|check-module)
        shift
        cmd_validate "$@"
        ;;
    clean)
        cmd_clean
        ;;
    run|exec)
        shift
        cmd_run "$@"
        ;;
    doctor|check)
        shift || true
        cmd_doctor "$@"
        ;;
    upgrade|self-update|upgrade-engine)
        shift
        cmd_upgrade "$@"
        ;;
    update)
        shift
        cmd_update "$@"
        ;;
    login|auth)
        shift
        cmd_login "$@"
        ;;
    whoami|user)
        cmd_whoami
        ;;
    logout)
        cmd_logout
        ;;
    version|-v|--version)
        shift || true
        while [[ "$#" -gt 0 ]]; do
            case "$1" in
                --channel=*) SMILE_CHANNEL="${1#*=}" ;;
                --channel|-c) shift || true; SMILE_CHANNEL="${1:-prod}" ;;
                --alpha|--alpha!|--1.1a|--1.1A|--1.1.2a|--1.1.2A) SMILE_CHANNEL="alpha" ;;
                --omega|--rc) SMILE_CHANNEL="omega" ;;
                --prod|--stable) SMILE_CHANNEL="prod" ;;
            esac
            shift || true
        done
        ch_info="$(resolve_channel_brand "$SMILE_CHANNEL")"
        ch_title="$(echo "$ch_info" | cut -d'|' -f1)"
        ch_tier="$(echo "$ch_info" | cut -d'|' -f2)"
        ch_ver="$(echo "$ch_info" | cut -d'|' -f3)"
        echo "smiledev v$ch_ver ($ch_title — $ch_tier / Arch $SMILE_ARCH_VERSION / Looping Universal Package Manager)"
        ;;
    help|-h|--help|"")
        cmd_help
        ;;
    *)
        echo -e "${C_RED}Unknown command:${C_RESET} $1"
        cmd_help
        exit 1
        ;;
esac
