"""
Schema upgrade script for Local Deep Research database.
Handles schema upgrades for existing ldr.db databases.
"""

import os
import sys

from loguru import logger
from sqlalchemy import create_engine, inspect

# Add the parent directory to sys.path to allow relative imports
sys.path.append(
    os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
)

try:
    from src.local_deep_research.web.models.database import DB_PATH
except ImportError:
    # Fallback path if import fails
    current_dir = os.path.dirname(os.path.abspath(__file__))
    project_root = os.path.abspath(
        os.path.join(current_dir, "..", "..", "..", "..")
    )
    DB_PATH = os.path.join(project_root, "src", "data", "ldr.db")

from .models import Base, ResearchStrategy


def remove_research_log_table(engine):
    """
    Remove the redundant research_log table if it exists

    Args:
        engine: SQLAlchemy engine

    Returns:
        bool: True if operation was successful, False otherwise
    """
    try:
        inspector = inspect(engine)

        # Check if table exists
        if inspector.has_table("research_log"):
            # For SQLite, we need to use raw SQL for DROP TABLE
            with engine.connect() as conn:
                conn.execute("DROP TABLE research_log")
                conn.commit()
            logger.info("Successfully removed redundant 'research_log' table")
            return True
        else:
            logger.info("Table 'research_log' does not exist, no action needed")
            return True
    except Exception:
        logger.exception("Error removing research_log table")
        return False


def create_research_strategy_table(engine):
    """
    Create the research_strategies table if it doesn't exist

    Args:
        engine: SQLAlchemy engine

    Returns:
        bool: True if operation was successful, False otherwise
    """
    try:
        inspector = inspect(engine)

        # Check if table exists
        if not inspector.has_table("research_strategies"):
            # Create the table using ORM
            Base.metadata.create_all(
                engine, tables=[ResearchStrategy.__table__]
            )
            logger.info("Successfully created 'research_strategies' table")
            return True
        else:
            logger.info(
                "Table 'research_strategies' already exists, no action needed"
            )
            return True
    except Exception:
        logger.exception("Error creating research_strategies table")
        return False


def run_schema_upgrades():
    """
    Run all schema upgrade operations on the database

    Returns:
        bool: True if all upgrades successful, False otherwise
    """
    # Check if database exists
    if not os.path.exists(DB_PATH):
        logger.warning(
            f"Database not found at {DB_PATH}, skipping schema upgrades"
        )
        return False

    logger.info(f"Running schema upgrades on {DB_PATH}")

    try:
        # Create SQLAlchemy engine
        engine = create_engine(f"sqlite:///{DB_PATH}")

        # 1. Remove the redundant research_log table
        remove_research_log_table(engine)

        # 2. Create research_strategies table
        create_research_strategy_table(engine)

        logger.info("Schema upgrades completed successfully")
        return True
    except Exception:
        logger.exception("Error during schema upgrades")
        return False


if __name__ == "__main__":
    run_schema_upgrades()
