from sqlalchemy.orm import Session
from models.notification import Notification, NotificationType
from models.user import User
from models.post import Post
from models.vlog import Vlog
from models.album import Album
from models.event import Event
from datetime import datetime

class NotificationService:
    """Service for managing notifications."""
    
    @staticmethod
    def create_mention_notification(
        db: Session,
        mentioned_user_id: int,
        author: User,
        content_type: str,
        content_id: int,
        content_preview: str = None
    ):
        """Create a notification for a user mention."""
        if mentioned_user_id == author.id:
            return  # Don't notify self
        
        notification = Notification(
            user_id=mentioned_user_id,
            type=NotificationType.POST_MENTION,
            title="Vous avez été mentionné",
            message=f"{author.full_name} vous a mentionné dans un(e) {content_type}",
            link=f"/{content_type}s/{content_id}",
            is_read=False,
            created_at=datetime.utcnow()
        )
        
        db.add(notification)
        db.commit()
        return notification
    
    @staticmethod
    def create_event_notification(
        db: Session,
        user_id: int,
        event: Event,
        author: User
    ):
        """Create a notification for a new event."""
        if user_id == author.id:
            return  # Don't notify creator
        
        notification = Notification(
            user_id=user_id,
            type=NotificationType.EVENT_INVITATION,
            title="Nouvel événement",
            message=f"{author.full_name} a créé un nouvel événement : {event.title}",
            link=f"/events/{event.id}",
            is_read=False,
            created_at=datetime.utcnow()
        )
        
        db.add(notification)
        db.commit()
        return notification
    
    @staticmethod
    def create_album_notification(
        db: Session,
        user_id: int,
        album: Album,
        author: User
    ):
        """Create a notification for a new album."""
        if user_id == author.id:
            return  # Don't notify creator
        
        notification = Notification(
            user_id=user_id,
            type=NotificationType.NEW_ALBUM,
            title="Nouvel album",
            message=f"{author.full_name} a créé un nouvel album : {album.title}",
            link=f"/albums/{album.id}",
            is_read=False,
            created_at=datetime.utcnow()
        )
        
        db.add(notification)
        db.commit()
        return notification
    
    @staticmethod
    def create_vlog_notification(
        db: Session,
        user_id: int,
        vlog: Vlog,
        author: User
    ):
        """Create a notification for a new vlog."""
        if user_id == author.id:
            return  # Don't notify creator
        
        notification = Notification(
            user_id=user_id,
            type=NotificationType.NEW_VLOG,
            title="Nouveau vlog",
            message=f"{author.full_name} a publié un nouveau vlog : {vlog.title}",
            link=f"/vlogs/{vlog.id}",
            is_read=False,
            created_at=datetime.utcnow()
        )
        
        db.add(notification)
        db.commit()
        return notification
    
    @staticmethod
    def create_system_notification(
        db: Session,
        user_id: int,
        title: str,
        message: str,
        link: str = None
    ):
        """Create a system notification."""
        notification = Notification(
            user_id=user_id,
            type=NotificationType.SYSTEM,
            title=title,
            message=message,
            link=link,
            is_read=False,
            created_at=datetime.utcnow()
        )
        
        db.add(notification)
        db.commit()
        return notification
