using Exilion.TradingAtomics;
using Managing.Domain.Candles;
using Managing.Domain.MoneyManagements;
using Managing.Domain.Strategies;
using Managing.Domain.Trades;
using Managing.Domain.Users;
using System.ComponentModel.DataAnnotations;
using Managing.Domain.Strategies.Base;
using static Managing.Common.Enums;

namespace Managing.Domain.Backtests;

public class Backtest
{
    public Backtest(
        Ticker ticker,
        string scenario,
        List<Position> positions,
        List<Signal> signals,
        Timeframe timeframe,
        List<Candle> candles,
        BotType botType,
        string accountName)
    {
        Ticker = ticker;
        Positions = positions;
        Signals = signals;
        Timeframe = timeframe;
        Candles = candles;
        Scenario = scenario;
        BotType = botType;
        AccountName = accountName;
        WalletBalances = new List<KeyValuePair<DateTime, decimal>>();
        StrategiesValues = new Dictionary<StrategyType, StrategiesResultBase>();
        
        // Initialize start and end dates if candles are provided
        if (candles != null && candles.Count > 0)
        {
            StartDate = candles.Min(c => c.Date);
            EndDate = candles.Max(c => c.Date);
        }
        else
        {
            StartDate = DateTime.UtcNow.AddDays(-30);
            EndDate = DateTime.UtcNow;
        }
    }

    [Required] public string Id { get; set; }
    [Required] public decimal FinalPnl { get; set; }
    [Required] public int WinRate { get; set; }
    [Required] public decimal GrowthPercentage { get; set; }
    [Required] public decimal HodlPercentage { get; set; }
    [Required] public Ticker Ticker { get; }
    [Required] public string Scenario { get; set; }
    [Required] public List<Position> Positions { get; }
    [Required] public List<Signal> Signals { get; }
    [Required] public Timeframe Timeframe { get; }
    [Required] public BotType BotType { get; }
    [Required] public string AccountName { get; }
    [Required] public List<Candle> Candles { get; set; }
    [Required] public DateTime StartDate { get; set; }
    [Required] public DateTime EndDate { get; set; }
    [Required] public PerformanceMetrics Statistics { get; set; }
    [Required] public decimal Fees { get; set; }
    [Required] public List<KeyValuePair<DateTime, decimal>> WalletBalances { get; set; }
    [Required] public MoneyManagement OptimizedMoneyManagement { get; set; }
    [Required] public MoneyManagement MoneyManagement { get; set; }
    [Required] public User User { get; set; }
    [Required] public Dictionary<StrategyType, StrategiesResultBase> StrategiesValues { get; set; }
    [Required] public double Score { get; set; }

    public string GetStringReport()
    {
        return
            $"{Ticker} | {Timeframe} |  Positions: {Positions.Count} |  Winrate: {WinRate}%     |   Pnl: {FinalPnl:#.##}$   |     %Pnl: {GrowthPercentage:#.##}%    |    %Hodl: {HodlPercentage:#.##}%";
    }
}