namespace FrontOffice.Main.Utilities;

/// <summary>
/// DTO for Network Tree node
/// </summary>
public class NetworkNodeDto
{
    public long UserId { get; set; }
    public string FullName { get; set; } = string.Empty;
    public string Mobile { get; set; } = string.Empty;
    public string? Avatar { get; set; }
    public string Position { get; set; } = string.Empty; // "Root", "Left" or "Right"
    public NetworkNodeDto? LeftChild { get; set; }
    public NetworkNodeDto? RightChild { get; set; }
    public int Level { get; set; }
    public bool IsActive { get; set; } = true;
    public DateTime? JoinedAt { get; set; }
    public bool IsClubActive { get; set; }
    public string? ActivationWeekNumber { get; set; }
    /// <summary>
    /// کد معرف کاربر
    /// </summary>
    public string? ReferralCode { get; set; }

    /// <summary>
    /// نام پکیج موثر (مثلاً پکیج طلایی / نقره‌ای)
    /// </summary>
    public string? PackageName { get; set; }

    /// <summary>تعداد اعضای فعال با پکیج طلایی جاری در پای چپ</summary>
    public int GoldLeftLegTotal { get; set; }

    /// <summary>تعداد اعضای فعال با پکیج طلایی جاری در پای راست</summary>
    public int GoldRightLegTotal { get; set; }

    /// <summary>تعداد اعضای فعال با پکیج نقره‌ای جاری در پای چپ</summary>
    public int SilverLeftLegTotal { get; set; }

    /// <summary>تعداد اعضای فعال با پکیج نقره‌ای جاری در پای راست</summary>
    public int SilverRightLegTotal { get; set; }
}

/// <summary>
/// DTO for flat node structure (for d3-org-chart)
/// </summary>
public class FlatNetworkNodeDto
{
    public string Id { get; set; } = string.Empty;
    public string ParentId { get; set; } = string.Empty;
    public string FullName { get; set; } = string.Empty;
    public string Mobile { get; set; } = string.Empty;
    public string? Avatar { get; set; }
    public string Position { get; set; } = string.Empty;
    public int Level { get; set; }
    public bool IsActive { get; set; } = true;
    public bool IsClubActive { get; set; }
    public string? ActivationWeekNumber { get; set; }
    public DateTime? JoinedAt { get; set; }
    /// <summary>
    /// کد معرف کاربر - فقط برای کاربران فعال در باشگاه نمایش داده شود
    /// </summary>
    public string? ReferralCode { get; set; }

    /// <summary>
    /// نام پکیج موثر (مثلاً پکیج طلایی / نقره‌ای)
    /// </summary>
    public string? PackageName { get; set; }

    public int GoldLeftLegTotal { get; set; }
    public int GoldRightLegTotal { get; set; }
    public int SilverLeftLegTotal { get; set; }
    public int SilverRightLegTotal { get; set; }
}

/// <summary>
/// DTO for Network Tree response
/// </summary>
public class NetworkTreeDto
{
    public NetworkNodeDto? RootNode { get; set; }
    public int TotalMembers { get; set; }
    public int CurrentDepth { get; set; }
    
    /// <summary>
    /// Convert hierarchical tree to flat array for d3-org-chart
    /// </summary>
    public List<FlatNetworkNodeDto> ToFlatArray()
    {
        var result = new List<FlatNetworkNodeDto>();
        if (RootNode == null) return result;
        
        TraverseAndFlatten(RootNode, "", result);
        return result;
    }
    
    private void TraverseAndFlatten(NetworkNodeDto node, string parentId, List<FlatNetworkNodeDto> result)
    {
        var flatNode = new FlatNetworkNodeDto
        {
            Id = node.UserId.ToString(),
            ParentId = parentId,
            FullName = node.FullName,
            Mobile = node.Mobile,
            Avatar = node.Avatar,
            Position = node.Position,
            Level = node.Level,
            IsActive = node.IsActive,
            IsClubActive = node.IsClubActive,
            ActivationWeekNumber = node.ActivationWeekNumber,
            JoinedAt = node.JoinedAt,
            ReferralCode = node.ReferralCode,
            PackageName = node.PackageName,
            GoldLeftLegTotal = node.GoldLeftLegTotal,
            GoldRightLegTotal = node.GoldRightLegTotal,
            SilverLeftLegTotal = node.SilverLeftLegTotal,
            SilverRightLegTotal = node.SilverRightLegTotal
        };
        
        result.Add(flatNode);
        
        if (node.LeftChild != null)
        {
            TraverseAndFlatten(node.LeftChild, flatNode.Id, result);
        }
        
        if (node.RightChild != null)
        {
            TraverseAndFlatten(node.RightChild, flatNode.Id, result);
        }
    }
}

/// <summary>
/// DTO for Last Member info
/// </summary>
public class LastMemberDto
{
    public long UserId { get; set; }
    public string FullName { get; set; } = string.Empty;
    public string Position { get; set; } = string.Empty;
    public DateTime JoinedAt { get; set; }
}

/// <summary>
/// DTO for Network Statistics
/// </summary>
public class NetworkStatisticsDto
{
    public int LeftLegCount { get; set; }
    public int RightLegCount { get; set; }
    public int TotalMembers { get; set; }
    public int TreeDepth { get; set; }
    public string WeakerLeg { get; set; } = string.Empty; // "Left" or "Right"
    public LastMemberDto? LastMember { get; set; }

    /// <summary>تعداد اعضای فعال با پکیج طلایی/نقره‌ای جاری در هر پا — نوار پایین شجره</summary>
    public int GoldLeftLegTotal { get; set; }
    public int GoldRightLegTotal { get; set; }
    public int SilverLeftLegTotal { get; set; }
    public int SilverRightLegTotal { get; set; }
}
