"""
Admin Service - Settings Module

包含系统设置相关功能。
"""

import logging
from typing import Any, Optional

from datetime import datetime

from src.models.admin_schemas import (
    SystemSettingsResponse,
    SystemSettingItem,
)

logger = logging.getLogger(__name__)


class SettingsService:
    """Settings service for admin operations"""

    async def get_system_settings(self) -> SystemSettingsResponse:
        """Get system settings"""
        # For now, return default settings
        # In production, these would be stored in a database or config file
        settings = [
            SystemSettingItem(
                key="max_concurrent_tasks",
                value=5,
                description="Maximum number of concurrent tasks",
                category="tasks",
                is_editable=True,
                updated_at=None,
            ),
            SystemSettingItem(
                key="task_timeout_seconds",
                value=3600,
                description="Task timeout in seconds",
                category="tasks",
                is_editable=True,
                updated_at=None,
            ),
            SystemSettingItem(
                key="max_retries",
                value=3,
                description="Maximum number of retries for failed tasks",
                category="tasks",
                is_editable=True,
                updated_at=None,
            ),
            SystemSettingItem(
                key="maintenance_mode",
                value=False,
                description="Enable maintenance mode",
                category="system",
                is_editable=True,
                updated_at=None,
            ),
            SystemSettingItem(
                key="registration_enabled",
                value=True,
                description="Allow new user registration",
                category="auth",
                is_editable=True,
                updated_at=None,
            ),
            SystemSettingItem(
                key="api_rate_limit",
                value=100,
                description="API rate limit per minute",
                category="api",
                is_editable=True,
                updated_at=None,
            ),
        ]

        categories = list(set(s.category for s in settings))

        return SystemSettingsResponse(
            settings=settings,
            categories=categories,
        )

    async def update_system_setting(self, key: str, value: Any) -> Optional[SystemSettingItem]:
        """Update a system setting"""
        # For now, just return the updated setting
        # In production, this would persist to database or config file
        settings = await self.get_system_settings()

        for setting in settings.settings:
            if setting.key == key:
                setting.value = value
                setting.updated_at = datetime.now().isoformat()
                return setting

        return None
