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

import logging
import os
import sqlite3
import sys

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# 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")


def check_table_exists(conn, table_name):
    """
    Check if a table exists in the database

    Args:
        conn: SQLite connection
        table_name: Name of the table

    Returns:
        bool: True if table exists, False otherwise
    """
    cursor = conn.cursor()
    cursor.execute(
        "SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,)
    )
    return cursor.fetchone() is not None


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

    Args:
        conn: SQLite connection

    Returns:
        bool: True if operation was successful, False otherwise
    """
    try:
        cursor = conn.cursor()

        # Check if table exists
        if check_table_exists(conn, "research_log"):
            # For SQLite, DROP TABLE is the way to remove a table
            cursor.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 as e:
        logger.error(f"Error removing research_log table: {e}")
        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:
        # Connect to the database
        conn = sqlite3.connect(DB_PATH)

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

        # Close connection
        conn.close()

        logger.info("Schema upgrades completed successfully")
        return True
    except Exception as e:
        logger.error(f"Error during schema upgrades: {e}")
        return False


if __name__ == "__main__":
    run_schema_upgrades()
