"""
具体事件类型定义
定义系统中使用的各种事件
"""
from typing import Any, Dict, Optional, List
from dataclasses import dataclass
from llama_index.core.schema import BaseNode

from .event_bus import Event, EventType


@dataclass
class IndexBuildEvent(Event):
    """索引构建事件"""
    
    @staticmethod
    def build_started(nodes_count: int, source: str = "IndexBuilder") -> "IndexBuildEvent":
        """索引构建开始"""
        return IndexBuildEvent(
            event_type=EventType.INDEX_BUILD_STARTED,
            data={"nodes_count": nodes_count},
            source=source
        )
    
    @staticmethod
    def build_completed(
        index_type: str,
        nodes_count: int,
        duration: float,
        source: str = "IndexBuilder"
    ) -> "IndexBuildEvent":
        """索引构建完成"""
        return IndexBuildEvent(
            event_type=EventType.INDEX_BUILD_COMPLETED,
            data={
                "index_type": index_type,
                "nodes_count": nodes_count,
                "duration": duration
            },
            source=source
        )
    
    @staticmethod
    def build_failed(
        index_type: str,
        error: str,
        source: str = "IndexBuilder"
    ) -> "IndexBuildEvent":
        """索引构建失败"""
        return IndexBuildEvent(
            event_type=EventType.INDEX_BUILD_FAILED,
            data={
                "index_type": index_type,
                "error": error
            },
            source=source
        )


@dataclass
class IndexLoadEvent(Event):
    """索引加载事件"""
    
    @staticmethod
    def load_started(index_type: str, source: str = "IndexLoader") -> "IndexLoadEvent":
        """索引加载开始"""
        return IndexLoadEvent(
            event_type=EventType.INDEX_LOAD_STARTED,
            data={"index_type": index_type},
            source=source
        )
    
    @staticmethod
    def load_completed(
        index_type: str,
        duration: float,
        source: str = "IndexLoader"
    ) -> "IndexLoadEvent":
        """索引加载完成"""
        return IndexLoadEvent(
            event_type=EventType.INDEX_LOAD_COMPLETED,
            data={
                "index_type": index_type,
                "duration": duration
            },
            source=source
        )
    
    @staticmethod
    def load_failed(
        index_type: str,
        error: str,
        source: str = "IndexLoader"
    ) -> "IndexLoadEvent":
        """索引加载失败"""
        return IndexLoadEvent(
            event_type=EventType.INDEX_LOAD_FAILED,
            data={
                "index_type": index_type,
                "error": error
            },
            source=source
        )


@dataclass
class StorageEvent(Event):
    """存储事件"""
    
    @staticmethod
    def persisted(
        storage_type: str,
        location: str,
        source: str = "StorageManager"
    ) -> "StorageEvent":
        """存储持久化"""
        return StorageEvent(
            event_type=EventType.STORAGE_PERSISTED,
            data={
                "storage_type": storage_type,
                "location": location
            },
            source=source
        )
    
    @staticmethod
    def deleted(
        storage_type: str,
        location: str,
        source: str = "StorageManager"
    ) -> "StorageEvent":
        """存储删除"""
        return StorageEvent(
            event_type=EventType.STORAGE_DELETED,
            data={
                "storage_type": storage_type,
                "location": location
            },
            source=source
        )
    
    @staticmethod
    def switched(
        from_backend: str,
        to_backend: str,
        source: str = "StorageManager"
    ) -> "StorageEvent":
        """存储后端切换"""
        return StorageEvent(
            event_type=EventType.STORAGE_SWITCHED,
            data={
                "from_backend": from_backend,
                "to_backend": to_backend
            },
            source=source
        )


@dataclass
class QueryEvent(Event):
    """查询事件"""
    
    @staticmethod
    def query_started(
        query_text: str,
        retrieval_mode: str,
        source: str = "QueryEngine"
    ) -> "QueryEvent":
        """查询开始"""
        return QueryEvent(
            event_type=EventType.QUERY_STARTED,
            data={
                "query_text": query_text,
                "retrieval_mode": retrieval_mode
            },
            source=source
        )
    
    @staticmethod
    def query_completed(
        query_text: str,
        results_count: int,
        duration: float,
        source: str = "QueryEngine"
    ) -> "QueryEvent":
        """查询完成"""
        return QueryEvent(
            event_type=EventType.QUERY_COMPLETED,
            data={
                "query_text": query_text,
                "results_count": results_count,
                "duration": duration
            },
            source=source
        )
    
    @staticmethod
    def query_failed(
        query_text: str,
        error: str,
        source: str = "QueryEngine"
    ) -> "QueryEvent":
        """查询失败"""
        return QueryEvent(
            event_type=EventType.QUERY_FAILED,
            data={
                "query_text": query_text,
                "error": error
            },
            source=source
        )


@dataclass
class FileWatcherEvent(Event):
    """文件监控事件"""
    
    @staticmethod
    def file_created(
        file_path: str,
        file_name: str,
        file_size: int = 0,
        source: str = "FileWatcher"
    ) -> "FileWatcherEvent":
        """文件创建事件"""
        return FileWatcherEvent(
            event_type=EventType.FILE_CREATED,
            data={
                "file_path": file_path,
                "file_name": file_name,
                "file_size": file_size,
                "event_type": "created"
            },
            source=source
        )
    
    @staticmethod
    def file_modified(
        file_path: str,
        file_name: str,
        file_size: int = 0,
        source: str = "FileWatcher"
    ) -> "FileWatcherEvent":
        """文件修改事件"""
        return FileWatcherEvent(
            event_type=EventType.FILE_MODIFIED,
            data={
                "file_path": file_path,
                "file_name": file_name,
                "file_size": file_size,
                "event_type": "modified"
            },
            source=source
        )
    
    @staticmethod
    def file_deleted(
        file_path: str,
        file_name: str,
        source: str = "FileWatcher"
    ) -> "FileWatcherEvent":
        """文件删除事件"""
        return FileWatcherEvent(
            event_type=EventType.FILE_DELETED,
            data={
                "file_path": file_path,
                "file_name": file_name,
                "event_type": "deleted"
            },
            source=source
        )
    
    @staticmethod
    def file_moved(
        src_path: str,
        dest_path: str,
        src_name: str,
        dest_name: str,
        source: str = "FileWatcher"
    ) -> "FileWatcherEvent":
        """文件移动事件"""
        return FileWatcherEvent(
            event_type=EventType.FILE_MOVED,
            data={
                "src_path": src_path,
                "dest_path": dest_path,
                "src_name": src_name,
                "dest_name": dest_name,
                "event_type": "moved"
            },
            source=source
        )
    
    @staticmethod
    def watcher_started(
        watch_dir: str,
        source: str = "FileWatcher"
    ) -> "FileWatcherEvent":
        """文件监控器启动事件"""
        return FileWatcherEvent(
            event_type=EventType.FILE_WATCHER_STARTED,
            data={
                "watch_dir": watch_dir,
                "event_type": "watcher_started"
            },
            source=source
        )
    
    @staticmethod
    def watcher_stopped(
        watch_dir: str,
        source: str = "FileWatcher"
    ) -> "FileWatcherEvent":
        """文件监控器停止事件"""
        return FileWatcherEvent(
            event_type=EventType.FILE_WATCHER_STOPPED,
            data={
                "watch_dir": watch_dir,
                "event_type": "watcher_stopped"
            },
            source=source
        )
    
    @staticmethod
    def watcher_error(
        error: str,
        watch_dir: str = "",
        source: str = "FileWatcher"
    ) -> "FileWatcherEvent":
        """文件监控器错误事件"""
        return FileWatcherEvent(
            event_type=EventType.FILE_WATCHER_ERROR,
            data={
                "error": error,
                "watch_dir": watch_dir,
                "event_type": "watcher_error"
            },
            source=source
        )


# 别名定义，保持向后兼容
FileChangeEvent = FileWatcherEvent
IncrementalIndexEvent = FileWatcherEvent
