"""
Admin Service Package

管理服务模块，包含：
- dashboard: 仪表板统计和系统资源
- users: 用户管理
- projects: 项目管理
- tasks: 任务管理
- settings: 系统设置
"""

from typing import Any

from .dashboard import DashboardService
from .users import UserManagementService
from .projects import ProjectManagementService
from .tasks import TaskManagementService
from .settings import SettingsService


class AdminService:
    """Admin service for management operations - Combines all admin services"""

    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            # Initialize all service components
            cls._instance._dashboard = DashboardService()
            cls._instance._users = UserManagementService()
            cls._instance._projects = ProjectManagementService()
            cls._instance._tasks = TaskManagementService()
            cls._instance._settings = SettingsService()
        return cls._instance

    # Dashboard methods
    async def get_dashboard_stats(self):
        return await self._dashboard.get_dashboard_stats()

    async def get_system_resources(self):
        return await self._dashboard.get_system_resources()

    async def get_recent_activity(self, limit: int = 20):
        return await self._dashboard.get_recent_activity(limit)

    # User management methods
    async def list_users(self, page: int = 1, page_size: int = 20, filters=None, sort_by: str = "created_at", sort_order: str = "desc"):
        return await self._users.list_users(page, page_size, filters, sort_by, sort_order)

    async def get_user(self, user_id: str):
        return await self._users.get_user(user_id)

    async def update_user(self, user_id: str, update_data):
        return await self._users.update_user(user_id, update_data)

    async def toggle_user_active(self, user_id: str, is_active: bool):
        return await self._users.toggle_user_active(user_id, is_active)

    async def delete_user(self, user_id: str):
        return await self._users.delete_user(user_id)

    # Project management methods
    async def list_projects(self, page: int = 1, page_size: int = 20, filters=None, sort_by: str = "created_at", sort_order: str = "desc"):
        return await self._projects.list_projects(page, page_size, filters, sort_by, sort_order)

    async def get_project(self, project_id: str):
        return await self._projects.get_project(project_id)

    async def delete_project(self, project_id: str):
        return await self._projects.delete_project(project_id)

    # Task management methods
    async def list_tasks(self, page: int = 1, page_size: int = 20, filters=None, sort_by: str = "created_at", sort_order: str = "desc"):
        return await self._tasks.list_tasks(page, page_size, filters, sort_by, sort_order)

    async def get_task_detail(self, task_id: str):
        return await self._tasks.get_task_detail(task_id)

    async def get_task_stats(self):
        return await self._tasks.get_task_stats()

    async def get_task_queue_status(self):
        return await self._tasks.get_task_queue_status()

    async def retry_task(self, task_id: str):
        return await self._tasks.retry_task(task_id)

    async def delete_task(self, task_id: str):
        return await self._tasks.delete_task(task_id)

    # Settings methods
    async def get_system_settings(self):
        return await self._settings.get_system_settings()

    async def update_system_setting(self, key: str, value: Any):
        return await self._settings.update_system_setting(key, value)


# Global admin service instance
admin_service = AdminService()

__all__ = [
    "AdminService",
    "admin_service",
    "DashboardService",
    "UserManagementService",
    "ProjectManagementService",
    "TaskManagementService",
    "SettingsService",
]
