from sqlalchemy.orm import Session
from app.db.models import Notification


class IdempotencyService:
    """Handle idempotent notification requests"""
    
    @staticmethod
    def check_idempotency(db: Session, tenant_id: int, idempotency_key: str) -> Notification | None:
        """
        Check if notification with this idempotency key already exists
        
        Args:
            db: database session
            tenant_id: tenant ID for isolation
            idempotency_key: unique key for this request
        
        Returns:
            Existing Notification if found, None otherwise
        """
        if not idempotency_key:
            return None
        
        existing = (
            db.query(Notification)
            .filter(
                Notification.tenant_id == tenant_id,
                Notification.idempotency_key == idempotency_key
            )
            .first()
        )
        
        return existing





























