using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using Managing.Domain.Users;
using Orleans;
using static Managing.Common.Enums;

namespace Managing.Domain.Trades
{
    [GenerateSerializer]
    public class Position
    {
        public Position(Guid identifier, string accountName, TradeDirection originDirection, Ticker ticker,
            LightMoneyManagement moneyManagement, PositionInitiator initiator, DateTime date, User user)
        {
            Identifier = identifier;
            AccountName = accountName;
            OriginDirection = originDirection;
            Ticker = ticker;
            MoneyManagement = moneyManagement;
            Initiator = initiator;
            Date = date;
            Status = Initiator == PositionInitiator.PaperTrading ? PositionStatus.Filled : PositionStatus.New;
            User = user;
        }

        [Id(0)] [Required] public string AccountName { get; set; }

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

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

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

        [Id(4)] [Required] public LightMoneyManagement MoneyManagement { get; set; }

        [Id(5)]
        [Required]
        [JsonPropertyName("Open")]
        public Trade Open { get; set; }

        [Id(6)]
        [Required]
        [JsonPropertyName("StopLoss")]
        public Trade StopLoss { get; set; }

        [Id(7)]
        [Required]
        [JsonPropertyName("TakeProfit1")]
        public Trade TakeProfit1 { get; set; }

        [Id(8)]
        [JsonPropertyName("TakeProfit2")]
        public Trade TakeProfit2 { get; set; }

        [Id(9)]
        [JsonPropertyName("ProfitAndLoss")]
        public ProfitAndLoss ProfitAndLoss { get; set; }

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

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

        [Id(12)] [Required] public Guid Identifier { get; set; }

        [Id(13)] [Required] public PositionInitiator Initiator { get; set; }

        [Id(14)] [Required] public User User { get; set; }

        /// <summary>
        /// Identifier of the bot or entity that initiated this position
        /// </summary>
        [Id(15)] [Required] public Guid InitiatorIdentifier { get; set; }

        public bool IsFinished()
        {
            return Status switch
            {
                PositionStatus.Finished => true,
                PositionStatus.Flipped => true,
                _ => false
            };
        }
    }
}