using System.ComponentModel.DataAnnotations;
using Managing.Domain.Users;

namespace Managing.Domain.Backtests;

/// <summary>
/// Domain model for bundle backtest requests
/// </summary>
public class BundleBacktestRequest
{
    public BundleBacktestRequest()
    {
        RequestId = Guid.NewGuid().ToString();
        CreatedAt = DateTime.UtcNow;
        Status = BundleBacktestRequestStatus.Pending;
        Results = new List<string>();
        BacktestRequestsJson = string.Empty;
    }

    /// <summary>
    /// Constructor that allows setting a specific ID
    /// </summary>
    /// <param name="requestId">The specific ID to use</param>
    public BundleBacktestRequest(string requestId)
    {
        RequestId = requestId;
        CreatedAt = DateTime.UtcNow;
        Status = BundleBacktestRequestStatus.Pending;
        Results = new List<string>();
        BacktestRequestsJson = string.Empty;
    }

    /// <summary>
    /// Unique identifier for the bundle backtest request
    /// </summary>
    [Required]
    public string RequestId { get; set; }

    /// <summary>
    /// The user who created this request
    /// </summary>
    [Required]
    public User User { get; set; }

    /// <summary>
    /// When the request was created
    /// </summary>
    [Required]
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// When the request was completed (if completed)
    /// </summary>
    public DateTime? CompletedAt { get; set; }

    /// <summary>
    /// Current status of the bundle backtest request
    /// </summary>
    [Required]
    public BundleBacktestRequestStatus Status { get; set; }

    /// <summary>
    /// Display name for the bundle backtest request
    /// </summary>
    [Required]
    public string Name { get; set; }

    /// <summary>
    /// The list of backtest requests to execute (serialized as JSON)
    /// </summary>
    [Required]
    public string BacktestRequestsJson { get; set; } = string.Empty;

    /// <summary>
    /// The results of the bundle backtest execution
    /// </summary>
    public List<string> Results { get; set; } = new();

    /// <summary>
    /// Total number of backtests in the bundle
    /// </summary>
    [Required]
    public int TotalBacktests { get; set; }

    /// <summary>
    /// Number of backtests completed so far
    /// </summary>
    [Required]
    public int CompletedBacktests { get; set; }

    /// <summary>
    /// Number of backtests that failed
    /// </summary>
    [Required]
    public int FailedBacktests { get; set; }

    /// <summary>
    /// Progress percentage (0-100)
    /// </summary>
    public double ProgressPercentage => TotalBacktests > 0 ? (double)CompletedBacktests / TotalBacktests * 100 : 0;

    /// <summary>
    /// Error message if the request failed
    /// </summary>
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// Progress information (JSON serialized)
    /// </summary>
    public string? ProgressInfo { get; set; }

    /// <summary>
    /// Current backtest being processed
    /// </summary>
    public string? CurrentBacktest { get; set; }

    /// <summary>
    /// Estimated time remaining in seconds
    /// </summary>
    public int? EstimatedTimeRemainingSeconds { get; set; }
}

/// <summary>
/// Status of a bundle backtest request
/// </summary>
public enum BundleBacktestRequestStatus
{
    /// <summary>
    /// Request is pending execution
    /// </summary>
    Pending,

    /// <summary>
    /// Request is currently being processed
    /// </summary>
    Running,

    /// <summary>
    /// Request completed successfully
    /// </summary>
    Completed,

    /// <summary>
    /// Request failed with an error
    /// </summary>
    Failed,

    /// <summary>
    /// Request was cancelled
    /// </summary>
    Cancelled
}