using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using static Managing.Common.Enums;

namespace Managing.Infrastructure.Databases.PostgreSql.Entities;

[Table("Positions")]
public class PositionEntity
{
    [Key] [Required] public Guid Identifier { get; set; }

    public DateTime Date { get; set; }

    [Column(TypeName = "decimal(18,8)")] public decimal ProfitAndLoss { get; set; }

    public TradeDirection OriginDirection { get; set; }
    public PositionStatus Status { get; set; }
    public Ticker Ticker { get; set; }
    public PositionInitiator Initiator { get; set; }

    [MaxLength(255)] public string SignalIdentifier { get; set; }

    [MaxLength(255)] public string AccountName { get; set; }

    public int? UserId { get; set; }
    
    /// <summary>
    /// Identifier of the bot or entity that initiated this position
    /// </summary>
    [Required] public Guid InitiatorIdentifier { get; set; }

    // Foreign keys to trades
    public int? OpenTradeId { get; set; }
    public int? StopLossTradeId { get; set; }
    public int? TakeProfit1TradeId { get; set; }
    public int? TakeProfit2TradeId { get; set; }

    // Money management data stored as JSON
    [Column(TypeName = "text")] public string? MoneyManagementJson { get; set; }

    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

    // Navigation properties
    public UserEntity? User { get; set; }
    
    [ForeignKey("OpenTradeId")] public virtual TradeEntity? OpenTrade { get; set; }

    [ForeignKey("StopLossTradeId")] public virtual TradeEntity? StopLossTrade { get; set; }

    [ForeignKey("TakeProfit1TradeId")] public virtual TradeEntity? TakeProfit1Trade { get; set; }

    [ForeignKey("TakeProfit2TradeId")] public virtual TradeEntity? TakeProfit2Trade { get; set; }
}