"""Database utilities for metrics module with SQLAlchemy."""

from contextlib import contextmanager
from typing import Generator

from loguru import logger
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker

from ..utilities.db_utils import DB_PATH
from .db_models import Base


class MetricsDatabase:
    """Database manager for metrics using SQLAlchemy."""

    def __init__(self):
        # Use the same database as the rest of the app
        self.engine = create_engine(
            f"sqlite:///{DB_PATH}", connect_args={"check_same_thread": False}
        )
        self.SessionLocal = sessionmaker(
            bind=self.engine, autocommit=False, autoflush=False
        )
        self._init_database()

    def _init_database(self):
        """Initialize database tables for metrics."""
        try:
            Base.metadata.create_all(self.engine)
            logger.info("Metrics tables initialized successfully")
        except Exception as e:
            logger.exception(f"Error initializing metrics tables: {e}")

    @contextmanager
    def get_session(self) -> Generator[Session, None, None]:
        """Get a database session with automatic cleanup."""
        session = self.SessionLocal()
        try:
            yield session
            session.commit()
        except Exception:
            session.rollback()
            raise
        finally:
            session.close()


# Singleton instance
_metrics_db = None


def get_metrics_db() -> MetricsDatabase:
    """Get the singleton metrics database instance."""
    global _metrics_db
    if _metrics_db is None:
        _metrics_db = MetricsDatabase()
    return _metrics_db
