-- =============================================
-- Diagnose: Package holder counts per leg (read-only)
-- Compares OLD source (NetworkWeeklyBalances) vs NEW (active members + current cycle)
-- Usage: SET @RootUserId to the tree owner UserId
-- =============================================

DECLARE @RootUserId BIGINT = 1; -- ← change to target user

DECLARE @GoldPackageId BIGINT = (
    SELECT TOP 1 Id FROM CMS.Packages
    WHERE IsDeleted = 0 AND IsActive = 1 AND IsBasePackage = 1
    ORDER BY Id
);
DECLARE @SilverPackageId BIGINT = (
    SELECT TOP 1 Id FROM CMS.Packages
    WHERE IsDeleted = 0 AND IsActive = 1 AND IsBasePackage = 0
      AND (Title LIKE N'%نقر%' OR MaxBalancesPerLeg <= 30)
    ORDER BY Id
);

DECLARE @LeftChildId BIGINT = (
    SELECT Id FROM CMS.Users
    WHERE NetworkParentId = @RootUserId AND LegPosition = 0 AND IsDeleted = 0
);
DECLARE @RightChildId BIGINT = (
    SELECT Id FROM CMS.Users
    WHERE NetworkParentId = @RootUserId AND LegPosition = 1 AND IsDeleted = 0
);

;WITH LegUsers AS (
    SELECT @LeftChildId AS RootLegUserId, 0 AS LegSide
    WHERE @LeftChildId IS NOT NULL
    UNION ALL
    SELECT @RightChildId, 1
    WHERE @RightChildId IS NOT NULL
),
Subtree AS (
    SELECT lu.RootLegUserId, lu.LegSide, lu.RootLegUserId AS UserId
    FROM LegUsers lu

    UNION ALL

    SELECT s.RootLegUserId, s.LegSide, u.Id
    FROM Subtree s
    INNER JOIN CMS.Users u ON u.NetworkParentId = s.UserId AND u.IsDeleted = 0
),
ActivePackageHolders AS (
    SELECT
        s.LegSide,
        cc.PackageId,
        COUNT(DISTINCT s.UserId) AS HolderCount
    FROM Subtree s
    INNER JOIN CMS.ClubMemberships cm ON cm.UserId = s.UserId AND cm.IsActive = 1 AND cm.IsDeleted = 0
    INNER JOIN CMS.ClubMembershipCycles cc ON cc.UserId = s.UserId AND cc.IsCurrentCycle = 1
    GROUP BY s.LegSide, cc.PackageId
),
LatestNwb AS (
    SELECT PackageId, LeftLegTotal, RightLegTotal, WeekDefinitionId,
           ROW_NUMBER() OVER (PARTITION BY PackageId ORDER BY WeekDefinitionId DESC) AS rn
    FROM CMS.NetworkWeeklyBalances
    WHERE UserId = @RootUserId
      AND PackageId IN (@GoldPackageId, @SilverPackageId)
)
SELECT
    @RootUserId AS RootUserId,
    @GoldPackageId AS GoldPackageId,
    @SilverPackageId AS SilverPackageId,
    -- NEW: real holder counts
    ISNULL((SELECT HolderCount FROM ActivePackageHolders WHERE LegSide = 0 AND PackageId = @GoldPackageId), 0) AS New_GoldLeft,
    ISNULL((SELECT HolderCount FROM ActivePackageHolders WHERE LegSide = 1 AND PackageId = @GoldPackageId), 0) AS New_GoldRight,
    ISNULL((SELECT HolderCount FROM ActivePackageHolders WHERE LegSide = 0 AND PackageId = @SilverPackageId), 0) AS New_SilverLeft,
    ISNULL((SELECT HolderCount FROM ActivePackageHolders WHERE LegSide = 1 AND PackageId = @SilverPackageId), 0) AS New_SilverRight,
    -- OLD: weekly balance scores (previous wrong source)
    ISNULL((SELECT LeftLegTotal FROM LatestNwb WHERE PackageId = @GoldPackageId AND rn = 1), 0) AS Old_Nwb_GoldLeft,
    ISNULL((SELECT RightLegTotal FROM LatestNwb WHERE PackageId = @GoldPackageId AND rn = 1), 0) AS Old_Nwb_GoldRight,
    ISNULL((SELECT LeftLegTotal FROM LatestNwb WHERE PackageId = @SilverPackageId AND rn = 1), 0) AS Old_Nwb_SilverLeft,
    ISNULL((SELECT RightLegTotal FROM LatestNwb WHERE PackageId = @SilverPackageId AND rn = 1), 0) AS Old_Nwb_SilverRight;
