"""
Secure Database Credentials - AUTO-GENERATED
This file is auto-generated by embed_credentials.py
DO NOT EDIT MANUALLY - Run embed_credentials.py to update credentials

WARNING: This file contains obfuscated credentials.
Only distribute the COMPILED version (.exe), never this source file.
"""

import base64
import hashlib

# Encrypted credentials (XOR encoded with app signature)
_CREDENTIALS = {'host': 'BFtXSwYNUxlTAwhLUl5U', 'port': 'AFZXVw==', 'name': 'RgEWAFJbBkU=', 'user': 'RgEWAFJbBkU+R0oAEQ==', 'password': 'BlUQB1JNM18UQUpdBwIzBld3WCkrBgF3', 'sslmode': 'RwcVEF5HBg=='}

# App signature for decryption
_APP_SIGNATURE = "Sentinel_StockScreener_2024"

def _get_decryption_key() -> str:
    """Get decryption key from app signature"""
    return hashlib.sha256(_APP_SIGNATURE.encode()).hexdigest()

def _decode_value(encoded_value: str) -> str:
    """Decode an XOR-encoded value"""
    key = _get_decryption_key()
    key_bytes = key.encode('utf-8')
    encoded_bytes = base64.b64decode(encoded_value)
    
    decoded = bytearray()
    for i, byte in enumerate(encoded_bytes):
        decoded.append(byte ^ key_bytes[i % len(key_bytes)])
    
    return decoded.decode('utf-8')

def get_db_config() -> dict:
    """
    Get database configuration with decrypted credentials.
    This function should only be called from within the compiled application.
    """
    return {
        'host': _decode_value(_CREDENTIALS['host']),
        'port': int(_decode_value(_CREDENTIALS['port'])),
        'database': _decode_value(_CREDENTIALS['name']),
        'user': _decode_value(_CREDENTIALS['user']),
        'password': _decode_value(_CREDENTIALS['password']),
        'sslmode': _decode_value(_CREDENTIALS['sslmode']),
    }

def get_connection_string() -> str:
    """Get PostgreSQL connection string"""
    config = get_db_config()
    return f"postgresql://{config['user']}:{config['password']}@{config['host']}:{config['port']}/{config['database']}"

# Clear sensitive data from module namespace after import
def _cleanup():
    """Remove sensitive data from module namespace"""
    global _CREDENTIALS
    # Note: We keep _CREDENTIALS as it's needed for get_db_config()
    # In a compiled app, this provides basic protection against casual inspection
    pass

_cleanup()
del _cleanup
