"""
Database initialization for Library - Unified Document Architecture.

This module handles:
- Seeding source_types table with predefined types
- Creating the default "Library" collection
- Must be called on app startup for each user
"""

import uuid
from loguru import logger
from sqlalchemy.exc import IntegrityError

from .models import SourceType, Collection
from .session_context import get_user_db_session


def seed_source_types(username: str, password: str = None) -> None:
    """
    Seed the source_types table with predefined document source types.

    Args:
        username: User to seed types for
        password: User's password (optional, uses session context)
    """
    predefined_types = [
        {
            "name": "research_download",
            "display_name": "Research Download",
            "description": "Documents downloaded from research sessions (arXiv, PubMed, etc.)",
            "icon": "download",
        },
        {
            "name": "user_upload",
            "display_name": "User Upload",
            "description": "Documents manually uploaded by the user",
            "icon": "upload",
        },
        {
            "name": "manual_entry",
            "display_name": "Manual Entry",
            "description": "Documents manually created or entered",
            "icon": "edit",
        },
    ]

    try:
        with get_user_db_session(username, password) as session:
            for type_data in predefined_types:
                # Check if type already exists
                existing = (
                    session.query(SourceType)
                    .filter_by(name=type_data["name"])
                    .first()
                )

                if not existing:
                    source_type = SourceType(id=str(uuid.uuid4()), **type_data)
                    session.add(source_type)
                    logger.info(f"Created source type: {type_data['name']}")

            session.commit()
            logger.info("Source types seeded successfully")

    except IntegrityError as e:
        logger.warning(f"Source types may already exist: {e}")
    except Exception:
        logger.exception("Error seeding source types")
        raise


def ensure_default_library_collection(
    username: str, password: str = None
) -> str:
    """
    Ensure the default "Library" collection exists for a user.
    Creates it if it doesn't exist.

    Args:
        username: User to check/create library for
        password: User's password (optional, uses session context)

    Returns:
        UUID of the Library collection
    """
    try:
        with get_user_db_session(username, password) as session:
            # Check if default library exists
            library = (
                session.query(Collection).filter_by(is_default=True).first()
            )

            if library:
                logger.debug(f"Default Library collection exists: {library.id}")
                return library.id

            # Create default Library collection
            library_id = str(uuid.uuid4())
            library = Collection(
                id=library_id,
                name="Library",
                description="Default collection for research downloads and documents",
                collection_type="default_library",
                is_default=True,
            )
            session.add(library)
            session.commit()

            logger.info(f"Created default Library collection: {library_id}")
            return library_id

    except Exception:
        logger.exception("Error ensuring default Library collection")
        raise


def initialize_library_for_user(username: str, password: str = None) -> dict:
    """
    Complete initialization of library system for a user.
    Seeds source types and ensures default Library collection exists.

    Args:
        username: User to initialize for
        password: User's password (optional, uses session context)

    Returns:
        Dict with initialization results
    """
    results = {
        "source_types_seeded": False,
        "library_collection_id": None,
        "success": False,
    }

    try:
        # Seed source types
        seed_source_types(username, password)
        results["source_types_seeded"] = True

        # Ensure Library collection
        library_id = ensure_default_library_collection(username, password)
        results["library_collection_id"] = library_id

        results["success"] = True
        logger.info(f"Library initialization complete for user: {username}")

    except Exception as e:
        logger.exception(f"Library initialization failed for {username}")
        results["error"] = str(e)

    return results


def get_default_library_id(username: str, password: str = None) -> str:
    """
    Get the ID of the default Library collection for a user.
    Creates it if it doesn't exist.

    Args:
        username: User to get library for
        password: User's password (optional, uses session context)

    Returns:
        UUID of the Library collection
    """
    return ensure_default_library_collection(username, password)


def get_source_type_id(
    username: str, type_name: str, password: str = None
) -> str:
    """
    Get the ID of a source type by name.

    Args:
        username: User to query for
        type_name: Name of source type (e.g., 'research_download', 'user_upload')
        password: User's password (optional, uses session context)

    Returns:
        UUID of the source type

    Raises:
        ValueError: If source type not found
    """
    try:
        with get_user_db_session(username, password) as session:
            source_type = (
                session.query(SourceType).filter_by(name=type_name).first()
            )

            if not source_type:
                raise ValueError(f"Source type not found: {type_name}")

            return source_type.id

    except Exception:
        logger.exception("Error getting source type ID")
        raise
