using Quadarax.Foundation.Core.Value.Extensions;

namespace Quadarax.Foundation.Core.Data
{
    public class Result : IResult
    {
        #region *** Properties ***
        public bool IsSuccess { get; protected set; }
        public IEnumerable<IError> Errors => _errors;
        public static IResult Ok => new Result();
        public static IResult Fail => new Result(false, new List<IError> { new Error("Operation failed", 0, ErrorLevelEnum.Error) });
        #endregion

        #region *** Private Fields ***
        private readonly IList<IError> _errors = new List<IError>();
        #endregion

        #region *** Constructors ***

        public Result() : this(true, null)
        {
        }
        public Result(IEnumerable<IError> errors) : this(false, errors)
        {
        }
        public Result(bool isSuccess, IEnumerable<IError>? errors)
        {
            errors ??= new List<IError>();
            IsSuccess = isSuccess;
            _errors.AddRange(errors);
        }
        #endregion

        #region *** Operations ***
        public void SetSuccess(bool isSuccess)
        {
            IsSuccess = isSuccess;
        }
        public void AddError(IError error)
        {
            if (error == null)
                throw new ArgumentNullException(nameof(error));

            _errors.Add(error);
        }

        public void ClearErrors()
        {
            _errors.Clear();
        }

        public object? GetValue()
        {
            return null;
        }
        #endregion
    }
}
