"""应用状态管理"""
import os
import platform
import time


class AppState:
    """应用状态"""

    def __init__(self):
        self.start_time = time.time()
        self.platform = platform.system()
        self.python_version = platform.python_version()
        self.version = os.getenv("APP_VERSION", "1.0.0")
        self.environment = os.getenv("ENVIRONMENT", "development")

    def get_uptime_seconds(self) -> float:
        """获取运行时间（秒）"""
        return time.time() - self.start_time

    def get_uptime_formatted(self) -> str:
        """获取格式化的运行时间"""
        seconds = self.get_uptime_seconds()

        days = int(seconds // 86400)
        hours = int((seconds % 86400) // 3600)
        minutes = int((seconds % 3600) // 60)
        secs = int(seconds % 60)

        parts = []
        if days > 0:
            parts.append(f"{days}天")
        if hours > 0:
            parts.append(f"{hours}小时")
        if minutes > 0:
            parts.append(f"{minutes}分钟")
        parts.append(f"{secs}秒")

        return "".join(parts)

    def get_info(self) -> dict:
        """获取应用信息"""
        return {
            "version": self.version,
            "environment": self.environment,
            "platform": self.platform,
            "python_version": self.python_version,
            "uptime_seconds": round(self.get_uptime_seconds(), 2),
            "uptime_formatted": self.get_uptime_formatted(),
        }


# 全局实例
app_state = AppState()
