-- ===========
--  Down Migration
-- ===========

-- Drop triggers (must be dropped before their tables)
DROP TRIGGER IF EXISTS update_routes_updated_at ON routes;
DROP TRIGGER IF EXISTS update_projects_updated_at ON projects;
DROP TRIGGER IF EXISTS update_workspaces_updated_at ON workspaces;
DROP TRIGGER IF EXISTS update_plans_updated_at ON plans;
DROP TRIGGER IF EXISTS update_users_updated_at ON users;

-- Drop the trigger function (after all triggers are gone)
DROP FUNCTION IF EXISTS update_updated_at_column();

-- Drop indexes
DROP INDEX IF EXISTS idx_routes_project_id;
DROP INDEX IF EXISTS idx_projects_workspace_id;
DROP INDEX IF EXISTS idx_workspaces_owner_id;

-- Drop tables in reverse dependency order
-- Product Logic
DROP TABLE IF EXISTS routes;
DROP TABLE IF EXISTS projects;

-- RBAC
DROP TABLE IF EXISTS role_permissions;
DROP TABLE IF EXISTS permissions;
DROP TABLE IF EXISTS roles;

-- Tenancy
DROP TABLE IF EXISTS workspace_members;
DROP TABLE IF EXISTS workspaces;

-- Users & Plans
DROP TABLE IF EXISTS plans;
DROP TABLE IF EXISTS users;
