#!/bin/bash
# ==========================================
# AEA SERVER MODULE: IDENTITY MANAGER
# VERSION: 1.00 (Unified User Database)
# ==========================================
AEA_VERSION="1.00"
MODULE_DATE="2026-03-21"

DB_FILE="/etc/aea_identity.json"

# 1. Version check for Android app
if [ "$1" == "--version" ] || [ "$1" == "-v" ]; then
    echo "$AEA_VERSION"
    exit 0
fi

# 2. Initialize DB if missing
if [ ! -f "$DB_FILE" ]; then
    echo '{"users": {}}' > "$DB_FILE"
    chmod 600 "$DB_FILE"
fi

# 3. CLI MODE (API for Android App)
case "$1" in
    --list | --info)
        cat "$DB_FILE"
        exit 0
        ;;
    --add-user)
        # $2 = user_id, $3 = name, $4 = auth_token
        if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
            echo "[AEA] Error: Missing arguments. Usage: --add-user <id> <name> <token>"
            exit 1
        fi
        jq --arg id "$2" --arg name "$3" --arg token "$4" \
           '.users[$id] = {name: $name, token: $token, status: "active", protocols: {}}' "$DB_FILE" > "${DB_FILE}.tmp" && mv "${DB_FILE}.tmp" "$DB_FILE"
        echo "[AEA] User $3 ($2) successfully created in database."
        exit 0
        ;;
    --remove-user)
        # $2 = user_id
        if [ -z "$2" ]; then exit 1; fi
        jq --arg id "$2" 'del(.users[$id])' "$DB_FILE" > "${DB_FILE}.tmp" && mv "${DB_FILE}.tmp" "$DB_FILE"
        echo "[AEA] User $2 successfully removed from database."
        exit 0
        ;;
    --link)
        # $2 = user_id, $3 = protocol (e.g., outline), $4 = key_id (e.g., 15)
        if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then exit 1; fi
        jq --arg id "$2" --arg proto "$3" --arg key "$4" \
           '.users[$id].protocols[$proto] = $key' "$DB_FILE" > "${DB_FILE}.tmp" && mv "${DB_FILE}.tmp" "$DB_FILE"
        echo "[AEA] Linked $3 key $4 to user $2."
        exit 0
        ;;
    --unlink)
        # $2 = user_id, $3 = protocol
        if [ -z "$2" ] || [ -z "$3" ]; then exit 1; fi
        jq --arg id "$2" --arg proto "$3" \
           'del(.users[$id].protocols[$proto])' "$DB_FILE" > "${DB_FILE}.tmp" && mv "${DB_FILE}.tmp" "$DB_FILE"
        echo "[AEA] Unlinked $3 from user $2."
        exit 0
        ;;
    --set-status)
        # $2 = user_id, $3 = status (active/suspended)
        if [ -z "$2" ] || [ -z "$3" ]; then exit 1; fi
        jq --arg id "$2" --arg stat "$3" \
           '.users[$id].status = $stat' "$DB_FILE" > "${DB_FILE}.tmp" && mv "${DB_FILE}.tmp" "$DB_FILE"
        echo "[AEA] User $2 status set to $3."
        exit 0
        ;;
esac

# 4. INSTALLATION MODE
if [ "$1" != "--list" ] && [ -z "$2" ]; then
    echo "[AEA] Installing Identity Manager module (v$AEA_VERSION)..."
    
    # Ensure jq is installed in the system environment
    if ! command -v jq >/dev/null 2>&1; then
        echo "[AEA] Setting up system environment for module..."
        export DEBIAN_FRONTEND=noninteractive
        apt-get update -yq >/dev/null 2>&1
        apt-get install -yq jq >/dev/null 2>&1
    fi

    # Copy itself to become a global system command
    MODULE_PATH="/usr/local/bin/aea_user_manager"
    cat "$0" > "$MODULE_PATH"
    chmod +x "$MODULE_PATH"

    echo "[AEA] Module successfully installed! Database initialized at $DB_FILE."
    echo "[AEA] Available commands: --list, --add-user, --remove-user, --link, --unlink, --set-status"
    exit 0
fi
