-- Execute cleanup of test data from production
-- This will actually delete the test data

BEGIN;

-- Show what we're about to delete
SELECT COUNT(*) as "Records to delete" FROM memories 
WHERE 
    -- Test patterns from unit tests
    content LIKE 'Batch test content%' OR
    content LIKE 'Memory #%' OR
    content LIKE 'Test content%' OR
    content LIKE 'Content with%tier' OR
    content LIKE 'Content for%test%' OR
    content LIKE 'Normal content%' OR
    content LIKE 'Concurrent%test%' OR
    content LIKE 'Recovery test%' OR
    
    -- Simple test entries
    content = 'Python code' OR
    content = 'Rust code' OR
    content = 'Python tutorial' OR
    content = 'No tags content' OR
    content = 'Important data starting in working tier' OR
    
    -- E2E test patterns
    content LIKE 'User''s important note without any AI processing' OR
    content LIKE 'Error: Connection timeout at line 42' OR
    content LIKE 'Just the content, nothing else' OR
    content LIKE 'The complete implementation of the authentication system%' OR
    content LIKE 'Pre-interruption content' OR
    content LIKE 'Recovery%content%' OR
    content LIKE 'Initial content' OR
    content LIKE 'Duplicate content test' OR
    
    -- Test tags patterns
    (tags && ARRAY['test', 'integration', 'mcp', 'stress-test', 'recovery-test']) OR
    EXISTS (
        SELECT 1 FROM unnest(tags) AS tag 
        WHERE tag LIKE 'batch-%' OR 
              tag LIKE 'concurrent-%' OR
              tag LIKE 'tag%' OR
              tag = 'single' OR
              tag = 'partial'
    );

-- Delete the test data
DELETE FROM memories 
WHERE 
    -- Test patterns from unit tests
    content LIKE 'Batch test content%' OR
    content LIKE 'Memory #%' OR
    content LIKE 'Test content%' OR
    content LIKE 'Content with%tier' OR
    content LIKE 'Content for%test%' OR
    content LIKE 'Normal content%' OR
    content LIKE 'Concurrent%test%' OR
    content LIKE 'Recovery test%' OR
    
    -- Simple test entries
    content = 'Python code' OR
    content = 'Rust code' OR
    content = 'Python tutorial' OR
    content = 'No tags content' OR
    content = 'Important data starting in working tier' OR
    
    -- E2E test patterns
    content LIKE 'User''s important note without any AI processing' OR
    content LIKE 'Error: Connection timeout at line 42' OR
    content LIKE 'Just the content, nothing else' OR
    content LIKE 'The complete implementation of the authentication system%' OR
    content LIKE 'Pre-interruption content' OR
    content LIKE 'Recovery%content%' OR
    content LIKE 'Initial content' OR
    content LIKE 'Duplicate content test' OR
    
    -- Test tags patterns
    (tags && ARRAY['test', 'integration', 'mcp', 'stress-test', 'recovery-test']) OR
    EXISTS (
        SELECT 1 FROM unnest(tags) AS tag 
        WHERE tag LIKE 'batch-%' OR 
              tag LIKE 'concurrent-%' OR
              tag LIKE 'tag%' OR
              tag = 'single' OR
              tag = 'partial'
    );

-- Show remaining count
SELECT COUNT(*) as "Remaining records after cleanup" FROM memories;

COMMIT;

SELECT 'Cleanup completed successfully!' as status;