# ================================================================
# services/production/app/core/database.py
# ================================================================
"""
Database configuration for production service
"""

import structlog
from shared.database import DatabaseManager, create_database_manager
from shared.database.base import Base
from shared.database.transactions import TransactionManager
from app.core.config import settings

logger = structlog.get_logger()

# Create database manager following shared pattern
database_manager = create_database_manager(
    settings.DATABASE_URL, 
    settings.SERVICE_NAME
)

# Transaction manager for the service
transaction_manager = TransactionManager(database_manager)

# Use exactly the same pattern as training/forecasting services
async def get_db():
    """Database dependency"""
    async with database_manager.get_session() as db:
        yield db

def get_db_transaction():
    """Get database transaction manager"""
    return database_manager.get_transaction()

async def get_db_health():
    """Check database health"""
    try:
        health_status = await database_manager.health_check()
        return health_status.get("healthy", False)
    except Exception as e:
        logger.error(f"Database health check failed: {e}")
        return False

async def init_database():
    """Initialize database tables"""
    try:
        await database_manager.create_tables()
        logger.info("Production service database initialized successfully")
    except Exception as e:
        logger.error(f"Failed to initialize database: {e}")
        raise