using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Managing.Domain.Candles;
using Orleans;
using static Managing.Common.Enums;

namespace Managing.Domain.Indicators;

[GenerateSerializer]
public class LightSignal
{
    public LightSignal(Ticker ticker, TradeDirection direction, Confidence confidence, Candle candle, DateTime date,
        TradingExchanges exchange, IndicatorType indicatorType, SignalType signalType, string indicatorName)
    {
        Direction = direction;
        Confidence = confidence;
        // Cast to base Candle type to avoid Orleans serialization issues with nested types
        Candle = candle as Candle ?? new Candle
        {
            Exchange = candle.Exchange,
            Ticker = candle.Ticker,
            OpenTime = candle.OpenTime,
            Date = candle.Date,
            Open = candle.Open,
            Close = candle.Close,
            High = candle.High,
            Low = candle.Low,
            Timeframe = candle.Timeframe,
            Volume = candle.Volume
        };
        Date = date;
        Ticker = ticker;
        Exchange = exchange;
        Status = SignalStatus.WaitingForPosition;
        IndicatorType = indicatorType;
        IndicatorName = indicatorName;
        SignalType = signalType;

        Identifier =
            $"{indicatorName}-{indicatorType}-{direction}-{ticker}-{Candle?.Close.ToString(CultureInfo.InvariantCulture)}-{date:yyyyMMdd-HHmmss}";
    }

    [Id(0)] [Required] public SignalStatus Status { get; set; }

    [Id(1)] [Required] public TradeDirection Direction { get; }

    [Id(2)] [Required] public Confidence Confidence { get; set; }

    [Id(3)] [Required] public Timeframe Timeframe { get; }

    [Id(4)] [Required] public DateTime Date { get; private set; }

    [Id(5)] [Required] public Candle Candle { get; }

    [Id(6)] [Required] public string Identifier { get; set; }

    [Id(7)] [Required] public Ticker Ticker { get; }

    [Id(8)] [Required] public TradingExchanges Exchange { get; set; }

    [Id(9)] [Required] public IndicatorType IndicatorType { get; set; }

    [Id(10)] [Required] public SignalType SignalType { get; set; }

    [Id(11)] [Required] public string IndicatorName { get; set; }

    public void SetConfidence(Confidence confidence)
    {
        Confidence = confidence;
    }
}