-- Rollback script for multi-agent coordination system
-- Use this to completely remove all multi-agent tables and functions
-- Author: Rivet (Ops)
-- WARNING: This will destroy all data!

BEGIN;

-- Drop views first (depend on tables)
DROP VIEW IF EXISTS active_tasks_by_priority CASCADE;
DROP VIEW IF EXISTS bot_workload_summary CASCADE;
DROP VIEW IF EXISTS recent_bot_activity CASCADE;
DROP VIEW IF EXISTS pending_communications CASCADE;
DROP VIEW IF EXISTS bot_health_status CASCADE;
DROP VIEW IF EXISTS failed_tasks_alert CASCADE;
DROP VIEW IF EXISTS handoff_pipeline CASCADE;

-- Drop functions
DROP FUNCTION IF EXISTS assign_task_to_bot(UUID, TEXT) CASCADE;
DROP FUNCTION IF EXISTS create_and_assign_task(TEXT, TEXT, TEXT, TEXT, TEXT) CASCADE;
DROP FUNCTION IF EXISTS current_bot_id() CASCADE;
DROP FUNCTION IF EXISTS update_updated_at_column() CASCADE;
DROP FUNCTION IF EXISTS log_task_status_change() CASCADE;
DROP FUNCTION IF EXISTS set_task_completed_at() CASCADE;
DROP FUNCTION IF EXISTS update_bot_heartbeat() CASCADE;

-- Drop tables (in reverse dependency order)
DROP TABLE IF EXISTS bot_audit_log CASCADE;
DROP TABLE IF EXISTS bot_handoffs CASCADE;
DROP TABLE IF EXISTS bot_comms_queue CASCADE;
DROP TABLE IF EXISTS bot_tasks CASCADE;
DROP TABLE IF EXISTS bot_status CASCADE;

COMMIT;