namespace adas_core.Authentication.Models;

public class UciResponse<T>
{
    protected UciResponse()
    {
    }

    protected UciResponse(T entity)
    {
        Success = true;
        Data = entity;
        Message = null;
        Error = null;
    }

    public string? Error { get; set; }
    public DateTime Date { get; set; } = DateTime.Now;
    public bool Success { get; protected set; }
    public string? Message { get; protected set; }
    public T? Data { get; protected set; }


    public static UciResponse<T> FromSuccess(T entity)
    {
        return new UciResponse<T>(entity);
    }


    public static UciResponse<T> FromError(Exception ex)
    {
        var error = ex.GetType().Name;
        var index = error.LastIndexOf("Exception", StringComparison.Ordinal);
        if (index > -1) error = error[..index];
        return FromError(error, ex.Message);
    }

    public static UciResponse<T> FromError(string error, string? message = null)
    {
        return new UciResponse<T>
        {
            Success = false,
            Error = error,
            Message = message
        };
    }
}