"""
Utility functions for consistent graph database path handling.

This module ensures consistent path construction for graph databases
using the LadybugDB single-file format with .lbug extension.
"""

from pathlib import Path

from ....config import env


def get_lbug_database_path(db_name: str, base_path: str | None = None) -> Path:
  """
  Get the correct path for a graph database, handling shared repositories.

  This function ensures consistent path construction across the codebase,
  using the LadybugDB single-file format with .lbug extension.

  Args:
      db_name: Name of the database (e.g., 'sec', 'kg1a2b3c')
      base_path: Optional base path override (defaults to LBUG_DATABASE_PATH env var)

  Returns:
      Path object for the database file (.lbug extension)
  """
  # Get base path from environment or parameter
  if base_path is None:
    base_path_str = env.LBUG_DATABASE_PATH
  else:
    base_path_str = base_path

  base_path_obj = Path(base_path_str)

  db_path = base_path_obj / f"{db_name}.lbug"

  return db_path


def ensure_lbug_directory(db_path: str | Path) -> None:
  """
  Ensure the parent directory for a graph database exists.

  Args:
      db_path: Path to the database file
  """
  db_path = Path(db_path)
  db_path.parent.mkdir(parents=True, exist_ok=True)
