"""
Database Initialization for Download Management

Creates the necessary database tables for tracking download status and retry logic.
"""

from sqlalchemy import create_engine, inspect
from loguru import logger

from .models import Base


def init_database():
    """Initialize the database with required tables"""

    # Create engine - use same path as research_library module
    engine = create_engine("sqlite:///data/research_library.db")

    try:
        # Create tables
        Base.metadata.create_all(engine)
        logger.info(
            "Download management database tables initialized successfully"
        )
    finally:
        # Always dispose engine to prevent file descriptor leaks
        engine.dispose()

    # Return None since engine is disposed - callers should create their own
    # engine/session if they need to interact with the database
    return


def verify_table_exists():
    """Verify that the required tables exist"""

    engine = create_engine("sqlite:///data/research_library.db")

    try:
        # Check if table exists using SQLAlchemy's inspect function
        inspector = inspect(engine)
        table_names = inspector.get_table_names()

        if "resource_download_status" in table_names:
            logger.info("✓ resource_download_status table exists")
            return True
        logger.warning("✗ resource_download_status table missing")
        return False
    finally:
        # Always dispose engine to prevent file descriptor leaks
        engine.dispose()


if __name__ == "__main__":
    # Initialize database when run directly
    init_database()
    verify_table_exists()
