from flask import jsonify, make_response


def success(data=None, message="操作成功", code=200):
    """
    Return a standardized success response

    Args:
        data: The data to return
        message: A success message
        code: HTTP status code

    Returns:
        dict: A standardized success response
    """
    return {
        "code": code,
        "message": message,
        "data": data
    }


def failed(message="操作失败", code=500, data=None, error=None):
    """
    Return a standardized error response

    Args:
        message: An error message
        code: HTTP status code
        data: Optional data to return
        error: Detailed error information

    Returns:
        dict: A standardized error response
    """
    result = {
        "code": code,
        "message": message,
        "data": data
    }
    if error is not None:
        result["error"] = error
    return result


def json_response(data, status_code=200):
    """
    Create a JSON response with proper headers

    Args:
        data: The data to return (will be passed to jsonify)
        status_code: HTTP status code

    Returns:
        Flask Response object with proper JSON headers
    """
    response = make_response(jsonify(data), status_code)
    response.headers['Content-Type'] = 'application/json; charset=utf-8'
    return response


def success_response(data=None, message="操作成功", status_code=200):
    """
    Return a standardized success response with proper headers

    Args:
        data: The data to return
        message: A success message
        status_code: HTTP status code

    Returns:
        Flask Response object with proper JSON headers
    """
    return json_response(success(data, message, status_code), status_code)


def failed_response(message="操作失败", status_code=500, data=None, error=None):
    """
    Return a standardized error response with proper headers

    Args:
        message: An error message
        status_code: HTTP status code
        data: Optional data to return
        error: Detailed error information

    Returns:
        Flask Response object with proper JSON headers
    """
    return json_response(
        failed(message, status_code, data, error), status_code
    )
