-- #262: Executive Meetings Requests/Approvals/Tasks removal cleanup.
--
-- Drops the two retired tables and scrubs orphaned rows from the
-- surviving Executive Meetings tables that referenced them by string.
-- Idempotent: safe to re-run. Wraps everything in a single transaction
-- so a partial failure leaves the DB unchanged.
--
-- Run order:
--   1) Apply this script to dev / prod databases.
--   2) Run `pnpm --filter @workspace/db push` to confirm the Drizzle
--      schema and the database are in sync (should be a no-op).
--
-- Run command (dev):
--   psql "$DATABASE_URL" -f artifacts/api-server/scripts/cleanup-em-requests-tasks.sql

BEGIN;

-- 1. Notification preferences rows for retired event types.
DELETE FROM executive_meeting_notification_prefs
WHERE notification_type IN (
  'request_submitted',
  'request_approved',
  'request_rejected',
  'request_needs_edit',
  'task_assigned',
  'task_completed'
);

-- 2. Notifications rows for retired event types (executive-meeting domain only).
DELETE FROM executive_meeting_notifications
WHERE notification_type IN (
  'request_submitted',
  'request_approved',
  'request_rejected',
  'request_needs_edit',
  'task_assigned',
  'task_completed'
);

-- 3. Global notifications (bell feed) tied to retired request/task events.
DELETE FROM notifications
WHERE related_type IN ('executive_meeting_request', 'executive_meeting_task');

-- 4. Audit logs that targeted request/task entities.
DELETE FROM executive_meeting_audit_logs
WHERE entity_type IN ('request', 'task');

-- 5. Drop the retired tables themselves. CASCADE removes any FK indices
--    that referenced them; nothing in the surviving schema depends on
--    these two tables.
DROP TABLE IF EXISTS executive_meeting_tasks CASCADE;
DROP TABLE IF EXISTS executive_meeting_requests CASCADE;

COMMIT;
