/*
  Correct users wrongly marked as DayaLoan — should be Manual.

  Affects:
    - CMS.Users.PackagePurchaseMethod          (1 → 3)
    - CMS.ClubMemberships.PurchaseMethod       (1 or 0 → 3)
    - CMS.ClubMembershipCycles.PurchaseMethod  (1 or 0 → 3)
    - CMS.UserPackagePurchases.PurchaseMethod  (1 → 3)
    - CMS.Users.HasReceivedDayaCredit / DayaCreditReceivedAt clear
    - CMS.DayaLoanContracts soft-delete stub rows
        (IsProcessed=0, no TransactionId, no ProcessedDate)
        for these UserIds only

  PackagePurchaseMethod: None=0, DayaLoan=1, DirectPurchase=2, Manual=3

  NOTE: UserId=43 shares NationalCode 5289780335 with UserId=42 (real Excel Daya).
        This script only touches UserId=43's own rows — not UserId=42's contract.

  Defaults to ROLLBACK. Uncomment COMMIT after verifying PRINT counts.
*/

SET NOCOUNT ON;
SET XACT_ABORT ON;

BEGIN TRAN;

DECLARE @FixUsers TABLE (UserId BIGINT PRIMARY KEY);
INSERT INTO @FixUsers (UserId) VALUES
(2),(7),(8),(9),(10),(11),(12),(13),(40),(41),(43),(45),(47),
(52),(58),(69),(71),(72),(74),(75),(87),(88),(89),(90),(91),(93),
(96),(99),(103),(105),(106),(111),(113),(115),(116),(119),(121),
(125),(130),(142),(162),(169),(170),(171),(172),(175),(176);

------------------------------------------------------------
-- 1) Users
------------------------------------------------------------
UPDATE u
SET
    u.PackagePurchaseMethod = 3, -- Manual
    u.HasReceivedDayaCredit = 0,
    u.DayaCreditReceivedAt = NULL,
    u.LastModified = SYSUTCDATETIME()
FROM CMS.Users u
INNER JOIN @FixUsers f ON f.UserId = u.Id
WHERE u.IsDeleted = 0
  AND (
        u.PackagePurchaseMethod = 1 -- DayaLoan
     OR u.HasReceivedDayaCredit = 1
     OR u.DayaCreditReceivedAt IS NOT NULL
  );

PRINT CONCAT('Users updated: ', @@ROWCOUNT);

------------------------------------------------------------
-- 2) ClubMemberships
------------------------------------------------------------
UPDATE m
SET
    m.PurchaseMethod = 3, -- Manual
    m.LastModified = SYSUTCDATETIME()
FROM CMS.ClubMemberships m
INNER JOIN @FixUsers f ON f.UserId = m.UserId
WHERE m.IsDeleted = 0
  AND m.PurchaseMethod IN (0, 1); -- None or DayaLoan

PRINT CONCAT('ClubMemberships updated: ', @@ROWCOUNT);

------------------------------------------------------------
-- 3) ClubMembershipCycles
------------------------------------------------------------
UPDATE c
SET
    c.PurchaseMethod = 3, -- Manual
    c.LastModified = SYSUTCDATETIME()
FROM CMS.ClubMembershipCycles c
INNER JOIN @FixUsers f ON f.UserId = c.UserId
WHERE c.IsDeleted = 0
  AND c.PurchaseMethod IN (0, 1);

PRINT CONCAT('ClubMembershipCycles updated: ', @@ROWCOUNT);

------------------------------------------------------------
-- 4) UserPackagePurchases (incl. backfill rows)
------------------------------------------------------------
UPDATE upp
SET
    upp.PurchaseMethod = 3, -- Manual
    upp.LastModified = SYSUTCDATETIME()
FROM CMS.UserPackagePurchases upp
INNER JOIN @FixUsers f ON f.UserId = upp.UserId
WHERE upp.IsDeleted = 0
  AND upp.PurchaseMethod = 1; -- only Daya → Manual (keep Direct=2 if any)

PRINT CONCAT('UserPackagePurchases updated: ', @@ROWCOUNT);

------------------------------------------------------------
-- 5) Soft-delete stub DayaLoanContracts (never processed)
------------------------------------------------------------
UPDATE d
SET
    d.IsDeleted = 1,
    d.LastModified = SYSUTCDATETIME()
FROM CMS.DayaLoanContracts d
INNER JOIN @FixUsers f ON f.UserId = d.UserId
WHERE d.IsDeleted = 0
  AND d.IsProcessed = 0
  AND d.TransactionId IS NULL
  AND d.ProcessedDate IS NULL;

PRINT CONCAT('DayaLoanContracts soft-deleted (stubs): ', @@ROWCOUNT);

------------------------------------------------------------
-- Verify
------------------------------------------------------------
SELECT 'Users still Daya' AS CheckName, COUNT(*) AS Cnt
FROM CMS.Users u
INNER JOIN @FixUsers f ON f.UserId = u.Id
WHERE u.IsDeleted = 0 AND u.PackagePurchaseMethod = 1
UNION ALL
SELECT 'Club still Daya/None', COUNT(*)
FROM CMS.ClubMemberships m
INNER JOIN @FixUsers f ON f.UserId = m.UserId
WHERE m.IsDeleted = 0 AND m.PurchaseMethod IN (0, 1)
UNION ALL
SELECT 'Cycle still Daya/None', COUNT(*)
FROM CMS.ClubMembershipCycles c
INNER JOIN @FixUsers f ON f.UserId = c.UserId
WHERE c.IsDeleted = 0 AND c.PurchaseMethod IN (0, 1)
UNION ALL
SELECT 'UPP still Daya', COUNT(*)
FROM CMS.UserPackagePurchases upp
INNER JOIN @FixUsers f ON f.UserId = upp.UserId
WHERE upp.IsDeleted = 0 AND upp.PurchaseMethod = 1
UNION ALL
SELECT 'Active stub Daya contracts', COUNT(*)
FROM CMS.DayaLoanContracts d
INNER JOIN @FixUsers f ON f.UserId = d.UserId
WHERE d.IsDeleted = 0
  AND d.IsProcessed = 0
  AND d.TransactionId IS NULL
  AND d.ProcessedDate IS NULL;

-- Sample after
SELECT u.Id AS UserId,
       u.FirstName, u.LastName, u.Mobile,
       u.PackagePurchaseMethod,
       m.PurchaseMethod AS ClubPurchaseMethod
FROM CMS.Users u
INNER JOIN @FixUsers f ON f.UserId = u.Id
LEFT JOIN CMS.ClubMemberships m ON m.UserId = u.Id AND m.IsDeleted = 0
WHERE u.IsDeleted = 0
ORDER BY u.Id;

-- Uncomment to apply:
-- COMMIT;
ROLLBACK;
PRINT 'Rolled back (safety). Uncomment COMMIT and remove ROLLBACK to apply.';
