from typing import List, Optional
from src.core.database.database import Database
from src.core.database.dao.session_dao import SessionDAO
from src.core.models.models import Session
from src.utils.logger import get_logger

logger = get_logger("session_manager")

class SessionManager:
    def __init__(self, db: Database):
        self.db = db
        self.session_dao = SessionDAO(db)

    def create_session(self, name: Optional[str] = None) -> Session:
        """Создание новой сессии с именем."""
        session = self.session_dao.create(name)
        logger.info(f"Created session with ID {session.id} and name {session.name}")
        return session

    def get_session(self, session_id: int) -> Optional[Session]:
        """Получение сессии по ID."""
        session = self.session_dao.get_by_id(session_id)
        if session:
            logger.info(f"Retrieved session with ID {session_id}, name {session.name}")
        else:
            logger.warning(f"Session with ID {session_id} not found")
        return session

    def get_all_sessions(self) -> List[Session]:
        """Получение всех сессий."""
        sessions = self.session_dao.get_all()
        logger.info(f"Retrieved {len(sessions)} sessions")
        return sessions

    def delete_session(self, session_id: int) -> bool:
        """Удаление сессии по ID."""
        success = self.session_dao.delete(session_id)
        if success:
            logger.info(f"Deleted session with ID {session_id}")
        else:
            logger.warning(f"Session with ID {session_id} not found for deletion")
        return success

    def update_session_name(self, session_id: int, name: str) -> bool:
        """Обновление имени сессии."""
        success = self.session_dao.update_name(session_id, name)
        if success:
            logger.info(f"Updated name for session with ID {session_id} to {name}")
        else:
            logger.warning(f"Session with ID {session_id} not found for name update")
        return success