import dataiku

from backends.explorer.connections.db_config_scanner import DbConfigScanner


def do(payload, config, plugin_config, inputs):
    parameter_name = payload["parameterName"]

    if parameter_name == "database_type":
        db_folder_id = payload['rootModel'].get("db_folder_id")
        if not db_folder_id:
            return {"choices": []}

        folder = dataiku.Folder(db_folder_id)
        scanner = DbConfigScanner([folder])
        snapshot_id = scanner.get_snapshot_ids()[0]
        db_type = scanner.get_db_type(snapshot_id)

        label = "Built-in" if db_type == "kuzu" else "Neo4j"

        return {"choices": [{"value":db_type, "label": label}]}
    else:
        raise Exception(f"Unexpected parameter name '{parameter_name}'.")
