"""
Panel Provider - In-app notifications stored in database.
"""
import logging

from . import BaseNotificationProvider, ProviderResponse
from ..models import NotificationChannel

logger = logging.getLogger('notification.panel')


class PanelProvider(BaseNotificationProvider):
    """
    Panel notification provider.
    Notifications are stored in database and shown in user's panel.
    No external service needed.
    """
    
    channel = NotificationChannel.PANEL
    
    def send(
        self,
        recipient: str,
        content: str,
        subject: str | None = None,
        **kwargs
    ) -> ProviderResponse:
        """
        Store notification for panel display.
        
        For panel notifications, the actual storage is handled
        at the service level. This provider just validates
        and returns success.
        
        Args:
            recipient: User ID
            content: Notification content
            subject: Not used for panel
            
        Returns:
            ProviderResponse with success status
        """
        if not self.validate_recipient(recipient):
            return ProviderResponse(
                success=False,
                error=f"Invalid user ID: {recipient}"
            )
        
        logger.info(f"Panel notification stored for user {recipient}")
        
        return ProviderResponse(
            success=True,
            message_id=f"panel_{recipient}_{hash(content) % 10000}",
            raw_response={"stored": True}
        )
    
    def validate_recipient(self, recipient: str) -> bool:
        """
        Validate user ID.
        
        For panel notifications, recipient should be a valid user ID.
        """
        if not recipient:
            return False
        
        # Check if it's a valid integer or UUID string
        try:
            int(recipient)
            return True
        except ValueError:
            # Could be UUID
            import re
            uuid_pattern = r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
            return bool(re.match(uuid_pattern, recipient.lower()))
