from typing import Optional
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, EmailStr

from app.models.user import User
from app.core.security import get_current_user

router = APIRouter()

default_settings = {
    "system": {
        "system_name": "智能星球",
        "refresh_interval": 60,
        "auto_refresh": True,
        "data_retention_days": 30,
        "max_concurrent_tasks": 5,
    },
    "notifications": {
        "email_enabled": False,
        "email_address": "",
        "critical_alerts": True,
        "warning_alerts": True,
        "daily_summary": False,
    },
    "security": {
        "session_timeout": 60,
        "max_login_attempts": 5,
        "password_policy": "medium",
    },
}

system_settings = default_settings["system"].copy()
notification_settings = default_settings["notifications"].copy()
security_settings = default_settings["security"].copy()


class SystemSettingsUpdate(BaseModel):
    system_name: str = "智能星球"
    refresh_interval: int = 60
    auto_refresh: bool = True
    data_retention_days: int = 30
    max_concurrent_tasks: int = 5


class NotificationSettingsUpdate(BaseModel):
    email_enabled: bool = False
    email_address: Optional[EmailStr] = None
    critical_alerts: bool = True
    warning_alerts: bool = True
    daily_summary: bool = False


class SecuritySettingsUpdate(BaseModel):
    session_timeout: int = 60
    max_login_attempts: int = 5
    password_policy: str = "medium"


@router.get("/system")
async def get_system_settings(current_user: User = Depends(get_current_user)):
    return {"system": system_settings}


@router.put("/system")
async def update_system_settings(
    settings: SystemSettingsUpdate,
    current_user: User = Depends(get_current_user),
):
    global system_settings
    system_settings = settings.model_dump()
    return {"status": "updated", "system": system_settings}


@router.get("/notifications")
async def get_notification_settings(current_user: User = Depends(get_current_user)):
    return {"notifications": notification_settings}


@router.put("/notifications")
async def update_notification_settings(
    settings: NotificationSettingsUpdate,
    current_user: User = Depends(get_current_user),
):
    global notification_settings
    notification_settings = settings.model_dump()
    return {"status": "updated", "notifications": notification_settings}


@router.get("/security")
async def get_security_settings(current_user: User = Depends(get_current_user)):
    return {"security": security_settings}


@router.put("/security")
async def update_security_settings(
    settings: SecuritySettingsUpdate,
    current_user: User = Depends(get_current_user),
):
    global security_settings
    security_settings = settings.model_dump()
    return {"status": "updated", "security": security_settings}


@router.get("")
async def get_all_settings(current_user: User = Depends(get_current_user)):
    return {
        "system": system_settings,
        "notifications": notification_settings,
        "security": security_settings,
    }
