from enum import Enum
from typing import Optional
from flask import request, session
from services.service_log import LogService

class OperationType(Enum):
    """操作类型枚举"""
    LOGIN = "登录"
    LOGOUT = "注销"
    CREATE = "新增"
    UPDATE = "修改"
    DELETE = "删除"
    PROCESS_TASK = "开始任务"
    PROCESS = "处理数据"
    SEND = "发送数据"

class OperationModule(Enum):
    """操作模块枚举"""
    ACCOUNT = "账户"
    PROJECT = "项目"
    SUB_PROJECT = "工程"
    SUB_PROJECT_DETAIL = "工程明细"

class LogHelper:
    _log_service = LogService()

    @staticmethod
    def get_client_ip() -> str:
        """获取客户端IP地址"""
        if request:
            if 'X-Forwarded-For' in request.headers:
                return request.headers['X-Forwarded-For'].split(',')[0]
            return request.remote_addr
        return ''
    @staticmethod
    def log_success(operation_type: OperationType,
                    operation_module: Optional[OperationModule],
                    operation_desc: str,
                    operation_data: Optional[str] = None,
                    data_changes: Optional[str] = None,
                    username: Optional[str] = None)-> None:
        """添加成功操作日志
        Args:
            operation_type: 操作类型（枚举值）
            operation_desc: 操作描述
            operation_module: 操作模块（枚举值，可选）
            operation_data: 操作数据（可选）
            data_changes: 数据变更记录（可选）
            username: 用户名（可选，默认从session获取）
        """
        LogHelper.add_log(
            operation_type=operation_type,
            operation_desc=operation_desc,
            operation_module=operation_module,
            operation_result=1,
            operation_data=operation_data,
            data_changes=data_changes,
            username=username
        )
    @staticmethod
    def log_fail(operation_type: OperationType,
                 operation_module: Optional[OperationModule],
                 operation_desc: str,
                 operation_data: Optional[str] = None,
                 data_changes: Optional[str] = None,
                 username: Optional[str] = None)-> None:
        """添加失败操作日志
        Args:
            operation_type: 操作类型（枚举值）
            operation_desc: 操作描述
            operation_module: 操作模块（枚举值，可选）
            operation_data: 操作数据（可选）
            data_changes: 数据变更记录（可选）
            username: 用户名（可选，默认从session获取）
        """
        LogHelper.add_log(
            operation_type=operation_type,
            operation_desc=operation_desc,
            operation_module=operation_module,
            operation_result=0,
            operation_data=operation_data,
            data_changes=data_changes,
            username=username
        )

    @staticmethod
    def add_log(operation_type: OperationType,
                operation_desc: str,
                operation_module: Optional[OperationModule] = None,
                operation_result: Optional[int] = None,
                operation_data: Optional[str] = None,
                data_changes: Optional[str] = None,
                username: Optional[str] = None) -> None:
        """添加操作日志

        Args:
            operation_type: 操作类型（枚举值）
            operation_desc: 操作描述
            operation_module: 操作模块（枚举值，可选）
            operation_result: 操作结果(0:失败, 1:成功)
            operation_data: 操作数据（可选）
            data_changes: 数据变更记录（可选）
            username: 用户名（可选，默认从session获取）
        """
        LogHelper._log_service.add_operation_log(
            username=username or session.get('username'),
            operation_type=operation_type.value,
            operation_desc=operation_desc,
            operation_module=operation_module.value if operation_module else None,
            operation_result=operation_result,
            operation_data=operation_data,
            operation_ip=LogHelper.get_client_ip(),
            data_changes=data_changes
        )