from rest_framework.response import Response
from rest_framework import status


def api_response(success=True, data=None, message=None, code=None, status_code=status.HTTP_200_OK, **kwargs):
    """
    Standard API response format for the entire application
    
    Args:
        success (bool): Whether the request was successful
        data (any): The data to return, can be None, a dict, a list, etc.
        message (str): A message explaining the result
        code (int): A custom code for the response (different from HTTP status code)
        status_code (int): The HTTP status code to return
        **kwargs: Any additional fields to include in the response
    
    Returns:
        Response: A DRF Response object with standardized structure
    """
    if code is None:
        code = status_code
    
    response_data = {
        'success': success,
        'code': code,
    }
    
    if data is not None:
        response_data['data'] = data
    
    if message is not None:
        response_data['message'] = message
    
    # Add any additional fields
    for key, value in kwargs.items():
        response_data[key] = value
    
    return Response(response_data, status=status_code)


def success_response(data=None, message="操作成功", code=200, **kwargs):
    """
    Standard success response
    """
    return api_response(
        success=True,
        data=data,
        message=message,
        code=code,
        status_code=status.HTTP_200_OK,
        **kwargs
    )


def created_response(data=None, message="创建成功", code=201, **kwargs):
    """
    Standard response for resource creation
    """
    return api_response(
        success=True,
        data=data,
        message=message,
        code=code,
        status_code=status.HTTP_201_CREATED,
        **kwargs
    )


def error_response(message="操作失败", code=400, status_code=status.HTTP_400_BAD_REQUEST, **kwargs):
    """
    Standard error response
    """
    return api_response(
        success=False,
        data=None,
        message=message,
        code=code,
        status_code=status_code,
        **kwargs
    )


def not_found_response(message="资源不存在", code=404, **kwargs):
    """
    Standard not found response
    """
    return api_response(
        success=False,
        data=None,
        message=message,
        code=code,
        status_code=status.HTTP_404_NOT_FOUND,
        **kwargs
    )


def unauthorized_response(message="未授权访问", code=401, **kwargs):
    """
    Standard unauthorized response
    """
    return api_response(
        success=False,
        data=None,
        message=message,
        code=code,
        status_code=status.HTTP_401_UNAUTHORIZED,
        **kwargs
    )


def forbidden_response(message="禁止访问", code=403, **kwargs):
    """
    Standard forbidden response
    """
    return api_response(
        success=False,
        data=None,
        message=message,
        code=code,
        status_code=status.HTTP_403_FORBIDDEN,
        **kwargs
    ) 