-- =============================================
-- Data Migration: Package-Based System
-- Date: 2026-02-25
-- Description: Seeds golden package and backfills 
--   existing records with PackageId = 1
-- NOTE: This is automatically executed in the EF Migration
--   (AddPackageBasedSystem). This file is for reference only.
-- =============================================

USE [YourDatabaseName]; -- Replace with actual DB name
GO

-- 1. Seed Golden Package (Id=1)
SET IDENTITY_INSERT [CMS].[Packages] ON;

IF NOT EXISTS (SELECT 1 FROM [CMS].[Packages] WHERE [Id] = 1)
BEGIN
    INSERT INTO [CMS].[Packages] 
        ([Id], [Title], [Description], [ImagePath], [Price],
         [SortOrder], [IsActive], [IsBasePackage], 
         [SupportsDayaPurchase], [SupportsDirectPurchase],
         [ActivationFee], [DiscountMultiplier], [MagicWalletMultiplier],
         [MaxBalancesPerLeg], [MaxNetworkLevel],
         [MagicWalletMaxDeposit], [MagicWalletMaxCredit],
         [Created], [IsDeleted])
    VALUES 
        (1, N'پکیج طلایی', N'پکیج اصلی باشگاه مشتریان کارا بازار سلامت', 
         N'/images/packages/golden.png', 56000000,
         1, 1, 1, 
         1, 1,
         25200000, 2.0, 2.5,
         300, 15,
         1000000000, 2500000000,
         GETUTCDATE(), 0);
    
    PRINT 'Golden Package (Id=1) seeded successfully.';
END
ELSE
BEGIN
    PRINT 'Golden Package (Id=1) already exists. Skipping seed.';
END

SET IDENTITY_INSERT [CMS].[Packages] OFF;
GO

-- 2. Backfill PackageId on commission/network tables
UPDATE [CMS].[WeeklyCommissionPools] SET [PackageId] = 1 WHERE [PackageId] = 0;
PRINT CONCAT('WeeklyCommissionPools updated: ', @@ROWCOUNT, ' rows');

UPDATE [CMS].[UserCommissionPayouts] SET [PackageId] = 1 WHERE [PackageId] = 0;
PRINT CONCAT('UserCommissionPayouts updated: ', @@ROWCOUNT, ' rows');

UPDATE [CMS].[NetworkWeeklyBalances] SET [PackageId] = 1 WHERE [PackageId] = 0;
PRINT CONCAT('NetworkWeeklyBalances updated: ', @@ROWCOUNT, ' rows');

UPDATE [CMS].[ClubMembershipCycles] SET [PackageId] = 1 WHERE [PackageId] = 0;
PRINT CONCAT('ClubMembershipCycles updated: ', @@ROWCOUNT, ' rows');
GO

-- 3. Backfill ClubMembership First/Last Activation from existing ActivatedAt
UPDATE cm SET 
    cm.[FirstActivationDate] = cm.[ActivatedAt],
    cm.[LastActivationDate] = cm.[ActivatedAt],
    cm.[FirstPackageId] = 1,
    cm.[LastPackageId] = 1
FROM [CMS].[ClubMemberships] cm
WHERE cm.[ActivatedAt] IS NOT NULL 
  AND cm.[FirstActivationDate] IS NULL;

PRINT CONCAT('ClubMemberships backfilled: ', @@ROWCOUNT, ' rows');
GO

-- 4. Verification Queries
SELECT 'Packages' AS [Table], COUNT(*) AS [Total], 
       SUM(CASE WHEN [IsBasePackage] = 1 THEN 1 ELSE 0 END) AS [BasePackages]
FROM [CMS].[Packages] WHERE [IsDeleted] = 0;

SELECT 'WeeklyCommissionPools' AS [Table], 
       COUNT(*) AS [Total],
       SUM(CASE WHEN [PackageId] = 0 THEN 1 ELSE 0 END) AS [MissingPackageId]
FROM [CMS].[WeeklyCommissionPools] WHERE [IsDeleted] = 0;

SELECT 'ClubMemberships' AS [Table],
       COUNT(*) AS [Total],
       SUM(CASE WHEN [FirstPackageId] IS NOT NULL THEN 1 ELSE 0 END) AS [WithFirstPackage],
       SUM(CASE WHEN [LastActivationDate] IS NOT NULL THEN 1 ELSE 0 END) AS [WithLastActivation]
FROM [CMS].[ClubMemberships] WHERE [IsDeleted] = 0;

PRINT 'Data migration completed successfully.';
GO
