from __future__ import annotations

import json
import os

from recipes.build_graph.assets import DATABASE_JSON_FILENAME
from solutions.graph.models import DbType


def write_database_info(database_type: DbType, database_name: str | None, build_dir: str) -> None:
    """
    Writes database.json file with connection information.

    Args:
        database_type (DbType): Type of database ('kuzu' or 'neo4j').
        database_name (str | None): Name of Neo4j database (required for Neo4j, None for Kuzu).
        build_dir (str): Directory to write the file.

    Raises:
        ValueError: If database_type is not 'kuzu' or 'neo4j'.
        ValueError: If database_type is 'neo4j' but database_name is None.
    """
    if database_type == "neo4j":
        if not database_name:
            raise ValueError("Neo4j database name is required.")
        database_info = {
            "type": "neo4j",
            "params": {
                "databaseName": database_name
            }
        }
    elif database_type == "kuzu":
        database_info = {
            "type": "kuzu"
        }
    else:
        raise ValueError(f"Unknown database type: {database_type}")

    db_info_path = os.path.join(build_dir, DATABASE_JSON_FILENAME)
    with open(db_info_path, "w", encoding="utf-8") as f:
        json.dump(database_info, f, indent=4)
