



DELETE r FROM reports r LEFT JOIN videos v ON r.video_id = v.id WHERE v.id IS NULL;
ALTER TABLE reports ADD CONSTRAINT fk_reports_video FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE;


DELETE r FROM reports r LEFT JOIN users u ON r.user_id = u.id WHERE r.user_id IS NOT NULL AND u.id IS NULL;
ALTER TABLE reports ADD CONSTRAINT fk_reports_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;


UPDATE contact_messages SET page_id = NULL WHERE page_id IS NOT NULL AND page_id NOT IN (SELECT id FROM pages);
ALTER TABLE contact_messages ADD CONSTRAINT fk_contact_messages_page FOREIGN KEY (page_id) REFERENCES pages(id) ON DELETE SET NULL;


DELETE ij FROM import_jobs ij LEFT JOIN users u ON ij.user_id = u.id WHERE u.id IS NULL;
ALTER TABLE import_jobs ADD CONSTRAINT fk_import_jobs_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;


ALTER TABLE reports ADD INDEX idx_reports_video_status (video_id, status);
