-- Cleanup script to remove text and translation records actualized before September 13, 2024
-- This script should be run with appropriate database permissions

BEGIN;

-- First, let's see what we're about to delete (for verification)
SELECT 
    'Text records to be deleted' as table_name,
    COUNT(*) as count,
    MIN(actualized_at) as earliest_date,
    MAX(actualized_at) as latest_date
FROM vgate.text 
WHERE actualized_at < '2024-09-13 00:00:00+00';

SELECT 
    'Translation records to be deleted' as table_name,
    COUNT(*) as count
FROM vgate.translation t
WHERE t.text_id IN (
    SELECT id FROM vgate.text 
    WHERE actualized_at < '2024-09-13 00:00:00+00'
);

-- Delete translation records first (due to foreign key constraints)
DELETE FROM vgate.translation 
WHERE text_id IN (
    SELECT id FROM vgate.text 
    WHERE actualized_at < '2024-09-13 00:00:00+00'
);

-- Delete text records
DELETE FROM vgate.text 
WHERE actualized_at < '2024-09-13 00:00:00+00';

-- Verify cleanup
SELECT 
    'Remaining text records' as table_name,
    COUNT(*) as count,
    MIN(actualized_at) as earliest_date,
    MAX(actualized_at) as latest_date
FROM vgate.text;

SELECT 
    'Remaining translation records' as table_name,
    COUNT(*) as count
FROM vgate.translation;

COMMIT;

