-- Migration Rollback: Remove tenant_id column from all tables
-- Date: 2026-01-26
-- Description: Rollback multi-tenancy support from all tables

BEGIN;

-- ============================================================================
-- File Service Tables
-- ============================================================================

DROP INDEX IF EXISTS idx_files_tenant;
ALTER TABLE files DROP COLUMN IF EXISTS tenant_id;

-- ============================================================================
-- Payment Service Tables
-- ============================================================================

DROP INDEX IF EXISTS idx_service_pricing_tenant;
DROP INDEX IF EXISTS idx_user_coupons_tenant;
DROP INDEX IF EXISTS idx_coupons_tenant;
DROP INDEX IF EXISTS idx_ledger_entries_tenant;
DROP INDEX IF EXISTS idx_payments_tenant;
DROP INDEX IF EXISTS idx_orders_tenant;

ALTER TABLE service_pricing DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE user_coupons DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE coupons DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE ledger_entries DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE payments DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE orders DROP COLUMN IF EXISTS tenant_id;

-- ============================================================================
-- Knowledge Service Tables
-- ============================================================================

DROP INDEX IF EXISTS idx_experiences_tenant;
DROP INDEX IF EXISTS idx_user_memories_tenant;
DROP INDEX IF EXISTS idx_knowledge_chunks_tenant;
DROP INDEX IF EXISTS idx_knowledge_articles_tenant;
DROP INDEX IF EXISTS idx_document_embeddings_tenant;
DROP INDEX IF EXISTS idx_documents_tenant;

ALTER TABLE experiences DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE user_memories DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE knowledge_chunks DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE knowledge_articles DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE document_embeddings DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE documents DROP COLUMN IF EXISTS tenant_id;

-- ============================================================================
-- Conversation Service Tables
-- ============================================================================

DROP INDEX IF EXISTS idx_token_usages_tenant;
DROP INDEX IF EXISTS idx_messages_tenant;
DROP INDEX IF EXISTS idx_conversations_tenant;

ALTER TABLE token_usages DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE messages DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE conversations DROP COLUMN IF EXISTS tenant_id;

-- ============================================================================
-- Evolution Service Tables
-- ============================================================================

DROP INDEX IF EXISTS idx_monthly_financial_reports_tenant;
DROP INDEX IF EXISTS idx_daily_statistics_tenant;
DROP INDEX IF EXISTS idx_evolution_logs_tenant;
DROP INDEX IF EXISTS idx_audit_logs_tenant;
DROP INDEX IF EXISTS idx_admin_users_tenant;
DROP INDEX IF EXISTS idx_admins_tenant;

ALTER TABLE monthly_financial_reports DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE daily_statistics DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE evolution_logs DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE audit_logs DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE admin_users DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE admins DROP COLUMN IF EXISTS tenant_id;

-- ============================================================================
-- User Service Tables
-- ============================================================================

DROP INDEX IF EXISTS idx_verification_codes_tenant;
DROP INDEX IF EXISTS idx_users_tenant_fingerprint;
DROP INDEX IF EXISTS idx_users_tenant_phone;
DROP INDEX IF EXISTS idx_users_tenant;

ALTER TABLE verification_codes DROP COLUMN IF EXISTS tenant_id;
ALTER TABLE users DROP COLUMN IF EXISTS tenant_id;

COMMIT;
