-- Reset the broken users (admin@demo.com and superadmin)
-- This will delete and let seed data recreate them
-- Run against PowderCoatingDb

BEGIN TRANSACTION;

-- Function to delete a user and all related data
DECLARE @UserIdsToDelete TABLE (UserId NVARCHAR(450), Email NVARCHAR(256));

-- Find the broken users
INSERT INTO @UserIdsToDelete (UserId, Email)
SELECT Id, Email
FROM AspNetUsers
WHERE Email = 'admin@demo.com' OR UserName = 'superadmin';

-- Delete related records for each user
DELETE FROM AspNetUserClaims
WHERE UserId IN (SELECT UserId FROM @UserIdsToDelete);

DELETE FROM AspNetUserRoles
WHERE UserId IN (SELECT UserId FROM @UserIdsToDelete);

DELETE FROM AspNetUserLogins
WHERE UserId IN (SELECT UserId FROM @UserIdsToDelete);

DELETE FROM AspNetUserTokens
WHERE UserId IN (SELECT UserId FROM @UserIdsToDelete);

-- Delete the users
DELETE FROM AspNetUsers
WHERE Id IN (SELECT UserId FROM @UserIdsToDelete);

-- Show what was deleted
SELECT 'Deleted users:' as Result;
SELECT * FROM @UserIdsToDelete;

COMMIT TRANSACTION;

PRINT 'Broken users deleted. Restart the application to let seed data recreate them.';
