import logging
from typing import Dict

from graph.connections.db_connection import DbConnection
from graph.ingestion.dataiku.data_types import NativeDataikuDataType

logger = logging.getLogger(__name__)


def create_node_table(
    table_name: str, primary_key: str, property_to_type: Dict[str, NativeDataikuDataType], connection: DbConnection
) -> None:
    constraint_name = f"{table_name}_{primary_key}_node_key"
    query = f"""
        CREATE CONSTRAINT `{constraint_name}`
        FOR (n:`{table_name}`) REQUIRE (n.`{primary_key}`) IS NODE KEY
    """

    logger.debug(
        f"Creating NODE_KEY constraint %s for %s on property %s with query '%s'.",
        constraint_name,
        table_name,
        primary_key,
        query,
    )

    connection.execute(query).close()

    logger.info(f"Done creating NODE_KEY constraint for table {table_name}.")


def create_edge_table(
    table_name: str,
    from_node: str,
    to_node: str,
    property_to_type: Dict[str, NativeDataikuDataType],
    connection: DbConnection,
) -> None:
    logger.debug("Nothing to do for edges in Neo4j.")
