"""
Database Initialization for Download Management

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

from sqlalchemy import create_engine
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")

    # Create tables
    Base.metadata.create_all(engine)

    logger.info("Download management database tables initialized successfully")
    return engine


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

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

    # Check if table exists
    inspector = engine.dialect.get_table_names(engine)

    if "resource_download_status" in inspector:
        logger.info("✓ resource_download_status table exists")
        return True
    else:
        logger.warning("✗ resource_download_status table missing")
        return False


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