-- Reset the superadmin user specifically
-- Run against PowderCoatingDb

BEGIN TRANSACTION;

-- Find and delete the superadmin user
DECLARE @UserId NVARCHAR(450);
SELECT @UserId = Id
FROM AspNetUsers
WHERE Email LIKE '%superadmin%' OR UserName LIKE '%superadmin%';

IF @UserId IS NOT NULL
BEGIN
    PRINT 'Found user ID: ' + @UserId;

    -- Delete related records
    DELETE FROM AspNetUserClaims WHERE UserId = @UserId;
    DELETE FROM AspNetUserRoles WHERE UserId = @UserId;
    DELETE FROM AspNetUserLogins WHERE UserId = @UserId;
    DELETE FROM AspNetUserTokens WHERE UserId = @UserId;

    -- Delete the user
    DELETE FROM AspNetUsers WHERE Id = @UserId;

    PRINT 'SuperAdmin user deleted successfully.';
END
ELSE
BEGIN
    PRINT 'SuperAdmin user not found.';
END

-- Show all remaining users
SELECT Email, UserName, CompanyRole
FROM AspNetUsers
ORDER BY Email;

COMMIT TRANSACTION;

PRINT 'Restart the application to recreate the superadmin user via seed data.';
