import os
from databricks import sql
import toml
from cruxctl.common.utils.typer_utils import logger


def open_aws_dbctx():
    config_path = os.path.expanduser("~/.crux/databricks/config.toml")
    config = toml.load(config_path)
    profile_section = config.get("profile")
    profile_config = profile_section.get("prod")
    host = profile_config.get("host")
    http_path = profile_config.get("path")
    databricks_token = profile_config.get("token")
    try:
        conn = sql.connect(
            server_hostname=host, http_path=http_path, access_token=databricks_token
        )
        logger.info("Connected to Databricks")
        return conn
    except Exception as e:
        logger.error(f"Connection failed: {e}")
        raise
