using System.Text.Json.Serialization;

namespace DigitalData.Core.DTO
{
    /// <summary>
    /// Represents a result of an operation that includes data, inheriting from <see cref="Result"/>.
    /// </summary>
    /// <typeparam name="T">The type of the data included in the result.</typeparam>
    public class DataResult<T> : Result
    {
        /// <summary>
        /// Gets or sets the data included in the result. This property is required.
        /// It will be ignored during JSON serialization if the value is null.
        /// </summary>
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public required T Data { get; set; }

        /// <summary>
        /// Converts the current <see cref="DataResult{T}"/> to a failed <see cref="DataResult{I}"/>,
        /// preserving the messages and notices.
        /// </summary>
        /// <typeparam name="I">The type of the data in the new failed result.</typeparam>
        /// <returns>A failed <see cref="DataResult{I}"/> with the current messages and notices.</returns>
        public DataResult<I> ToFail<I>() => Fail<I>().Message(Messages).Notice(Notices);
    }
}