#!/usr/bin/env python3
import os
import sys
import mysql.connector
import security_i
from config_i import config
from database_i import get_connection

def get_wallet_credentials(user_id: str, TESTING = False) -> tuple[str, str]:
    """
    Returns the wallet path and password for the given user_id.

    :param user_id: The user ID (Atavism account ID)
    :return: (wallet_address, wallet_password)
    :raises: Runtime error if the user is not found
    """
    #print (f"get_wallet_credentials.py: Getting wallet credentials for user {user_id}...", file=sys.stderr)
    conn = get_connection (config.db.tr1)
    cursor = conn.cursor()
    cursor.execute("SELECT wallet_a, wallet_p FROM users WHERE user_id = %s", (user_id,))
    result = cursor.fetchone()
    conn.close()

    if not result:
        raise RuntimeError(f"No wallet found for user {user_id}")

    return result[0], security_i.decrypt(result[1])

# Optional CLI usage for debugging
if __name__ == "__main__":
    import sys
    if len(sys.argv) < 2:
        print("Usage: get_wallet_credentials.py <user_id> [test]")
        print (get_wallet_credentials(20))
        exit(1)
    try:
        address, password = get_wallet_credentials(sys.argv[1])
        print(f"Path: {address}")
        print(f"Encrypted Password: {password}")
    except RuntimeError as e:
        print(e)
        exit(1)
