# Kemoverse - a gacha-style bot for the Fediverse.
# Copyright © 2025 Moon and contributors.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see https://www.gnu.org/licenses/.

"""Mock FediverseService for testing purposes"""

from typing import List, Optional, Union, BinaryIO
from fediverse_service import FediverseService
from fediverse_types import FediverseNotification, FediversePost, FediverseFile, Visibility


class MockFediverseService(FediverseService):
    """Mock implementation of FediverseService for testing"""
    
    def __init__(self):
        self.notifications = []
        self.created_posts = []
        self.uploaded_files = []
        
    def get_notifications(self, since_id: Optional[str] = None) -> List[FediverseNotification]:
        """Return mock notifications, optionally filtered by since_id"""
        if since_id is None:
            return self.notifications
        
        # Filter notifications newer than since_id
        filtered = []
        for notif in self.notifications:
            if notif.id > since_id:
                filtered.append(notif)
        return filtered
    
    def create_post(
        self,
        text: str,
        reply_to_id: Optional[str] = None,
        visibility: Visibility = Visibility.HOME,
        file_ids: Optional[List[str]] = None,
        visible_user_ids: Optional[List[str]] = None
    ) -> str:
        """Mock post creation, returns fake post ID"""
        post_id = f"mock_post_{len(self.created_posts)}"
        
        # Store the post for assertions
        self.created_posts.append({
            'id': post_id,
            'text': text,
            'reply_to_id': reply_to_id,
            'visibility': visibility,
            'file_ids': file_ids,
            'visible_user_ids': visible_user_ids
        })
        
        return post_id
    
    def upload_file(self, file_data: Union[BinaryIO, bytes]) -> FediverseFile:
        """Mock file upload, returns fake file"""
        file_id = f"mock_file_{len(self.uploaded_files)}"
        
        mock_file = FediverseFile(
            id=file_id,
            url=f"https://example.com/files/{file_id}",
            type="image/png",
            name="test_file.png"
        )
        
        self.uploaded_files.append(mock_file)
        return mock_file
    
    # Helper methods for testing
    def add_mock_notification(self, notification: FediverseNotification):
        """Add a mock notification for testing"""
        self.notifications.append(notification)
    
    def clear_all(self):
        """Clear all mock data"""
        self.notifications.clear()
        self.created_posts.clear()
        self.uploaded_files.clear()