import logging
import os
import sys

from ..setup_data_dir import setup_data_dir
from ..utilities.db_utils import get_db_setting
from .app_factory import create_app
from .models.database import (
    DB_PATH,
    LEGACY_DEEP_RESEARCH_DB,
    LEGACY_RESEARCH_HISTORY_DB,
)

# Initialize logger
logger = logging.getLogger(__name__)

# Ensure data directory exists
setup_data_dir()

# Run schema upgrades if database exists
if os.path.exists(DB_PATH):
    try:
        logger.info("Running schema upgrades on existing database")
        from .database.schema_upgrade import run_schema_upgrades

        run_schema_upgrades()
    except Exception as e:
        logger.error(f"Error running schema upgrades: {e}")
        logger.warning("Continuing without schema upgrades")


# Check if we need to run database migration
def check_migration_needed():
    """Check if database migration is needed, based on presence of legacy files and absence of new DB"""
    if not os.path.exists(DB_PATH):
        # The new database doesn't exist, check if legacy databases exist
        legacy_files_exist = os.path.exists(LEGACY_DEEP_RESEARCH_DB) or os.path.exists(
            LEGACY_RESEARCH_HISTORY_DB
        )

        if legacy_files_exist:
            logger.info(
                "Legacy database files found, but ldr.db doesn't exist. Migration needed."
            )
            return True

    return False


# Create the Flask app and SocketIO instance
app, socketio = create_app()


def main():
    """
    Entry point for the web application when run as a command.
    This function is needed for the package's entry point to work properly.
    """
    # Check if migration is needed
    if check_migration_needed():
        logger.info(
            "Database migration required. Run migrate_db.py before starting the application."
        )
        print("=" * 80)
        print("DATABASE MIGRATION REQUIRED")
        print(
            "Legacy database files were found, but the new unified database doesn't exist."
        )
        print(
            "Please run 'python -m src.local_deep_research.web.database.migrate_to_ldr_db' to migrate your data."
        )
        print(
            "You can continue without migration, but your previous data won't be available."
        )
        print("=" * 80)

        # If --auto-migrate flag is passed, run migration automatically
        if "--auto-migrate" in sys.argv:
            logger.info("Auto-migration flag detected, running migration...")
            try:
                from .database.migrate_to_ldr_db import migrate_to_ldr_db

                success = migrate_to_ldr_db()
                if success:
                    logger.info("Database migration completed successfully.")
                else:
                    logger.warning("Database migration failed.")
            except Exception as e:
                logger.error(f"Error running database migration: {e}")
                print(f"Error: {e}")
                print("Please run migration manually.")

    # Get web server settings with defaults
    port = get_db_setting("web.port", 5000)
    host = get_db_setting("web.host", "0.0.0.0")
    debug = get_db_setting("web.debug", True)

    logger.info(f"Starting web server on {host}:{port} (debug: {debug})")
    socketio.run(app, debug=debug, host=host, port=port, allow_unsafe_werkzeug=True)


if __name__ == "__main__":
    main()
