import torch


def mae(target, predictions: list, total=True):
    """
    Calculates mean absolute error
    MAE is different from MAPE in that the average of mean error is normalized over the average of all the actual values

    Parameters
    ----------
    target : torch.Tensor
        true values of the target variable
    predictions : list
        - predictions[0] = y_hat_test, predicted expected values of the target variable (torch.Tensor)
    total : bool, default = True
        Used in other loss functions to specify whether to return overall loss or loss over
        the horizon. This function only supports the former.

    Returns
    -------
    torch.Tensor
        A scalar with the overall mae (the lower the better)

    Raises
    ------
    NotImplementedError
        When 'total' is set to False, as mae does not support loss over the horizon
    """

    if not total:
        raise NotImplementedError("mae does not support loss over the horizon")

    y_hat_test = predictions[0]

    return torch.mean(torch.abs(target - y_hat_test))