-- Migration: Add tenant_id column to all tables for multi-tenancy support
-- Date: 2026-01-26
-- Description: Complete migration to add tenant_id to all service tables

BEGIN;

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

ALTER TABLE users ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE verification_codes ADD COLUMN IF NOT EXISTS tenant_id uuid;

CREATE INDEX IF NOT EXISTS idx_users_tenant ON users(tenant_id);
CREATE INDEX IF NOT EXISTS idx_users_tenant_phone ON users(tenant_id, phone);
CREATE INDEX IF NOT EXISTS idx_users_tenant_fingerprint ON users(tenant_id, fingerprint);
CREATE INDEX IF NOT EXISTS idx_verification_codes_tenant ON verification_codes(tenant_id);

COMMENT ON COLUMN users.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN verification_codes.tenant_id IS 'Tenant ID for multi-tenancy support';

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

ALTER TABLE admins ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE admins ADD COLUMN IF NOT EXISTS is_super_admin boolean DEFAULT false;
ALTER TABLE admin_users ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE audit_logs ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE evolution_logs ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE daily_statistics ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE monthly_financial_reports ADD COLUMN IF NOT EXISTS tenant_id uuid;

CREATE INDEX IF NOT EXISTS idx_admins_tenant ON admins(tenant_id);
CREATE INDEX IF NOT EXISTS idx_admins_super_admin ON admins(is_super_admin);
CREATE INDEX IF NOT EXISTS idx_admin_users_tenant ON admin_users(tenant_id);
CREATE INDEX IF NOT EXISTS idx_audit_logs_tenant ON audit_logs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_evolution_logs_tenant ON evolution_logs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_daily_statistics_tenant ON daily_statistics(tenant_id);
CREATE INDEX IF NOT EXISTS idx_monthly_financial_reports_tenant ON monthly_financial_reports(tenant_id);

COMMENT ON COLUMN admins.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN admin_users.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN audit_logs.tenant_id IS 'Tenant ID for multi-tenancy support';

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

ALTER TABLE conversations ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE messages ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE token_usages ADD COLUMN IF NOT EXISTS tenant_id uuid;

CREATE INDEX IF NOT EXISTS idx_conversations_tenant ON conversations(tenant_id);
CREATE INDEX IF NOT EXISTS idx_messages_tenant ON messages(tenant_id);
CREATE INDEX IF NOT EXISTS idx_token_usages_tenant ON token_usages(tenant_id);

COMMENT ON COLUMN conversations.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN messages.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN token_usages.tenant_id IS 'Tenant ID for multi-tenancy support';

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

ALTER TABLE documents ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE document_embeddings ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE knowledge_articles ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE knowledge_chunks ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE user_memories ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE experiences ADD COLUMN IF NOT EXISTS tenant_id uuid;

CREATE INDEX IF NOT EXISTS idx_documents_tenant ON documents(tenant_id);
CREATE INDEX IF NOT EXISTS idx_document_embeddings_tenant ON document_embeddings(tenant_id);
CREATE INDEX IF NOT EXISTS idx_knowledge_articles_tenant ON knowledge_articles(tenant_id);
CREATE INDEX IF NOT EXISTS idx_knowledge_chunks_tenant ON knowledge_chunks(tenant_id);
CREATE INDEX IF NOT EXISTS idx_user_memories_tenant ON user_memories(tenant_id);
CREATE INDEX IF NOT EXISTS idx_experiences_tenant ON experiences(tenant_id);

COMMENT ON COLUMN documents.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN knowledge_articles.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN user_memories.tenant_id IS 'Tenant ID for multi-tenancy support';

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

ALTER TABLE orders ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE payments ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE ledger_entries ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE coupons ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE user_coupons ADD COLUMN IF NOT EXISTS tenant_id uuid;
ALTER TABLE service_pricing ADD COLUMN IF NOT EXISTS tenant_id uuid;

CREATE INDEX IF NOT EXISTS idx_orders_tenant ON orders(tenant_id);
CREATE INDEX IF NOT EXISTS idx_payments_tenant ON payments(tenant_id);
CREATE INDEX IF NOT EXISTS idx_ledger_entries_tenant ON ledger_entries(tenant_id);
CREATE INDEX IF NOT EXISTS idx_coupons_tenant ON coupons(tenant_id);
CREATE INDEX IF NOT EXISTS idx_user_coupons_tenant ON user_coupons(tenant_id);
CREATE INDEX IF NOT EXISTS idx_service_pricing_tenant ON service_pricing(tenant_id);

COMMENT ON COLUMN orders.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN payments.tenant_id IS 'Tenant ID for multi-tenancy support';
COMMENT ON COLUMN ledger_entries.tenant_id IS 'Tenant ID for multi-tenancy support';

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

ALTER TABLE files ADD COLUMN IF NOT EXISTS tenant_id uuid;

CREATE INDEX IF NOT EXISTS idx_files_tenant ON files(tenant_id);

COMMENT ON COLUMN files.tenant_id IS 'Tenant ID for multi-tenancy support';

COMMIT;

-- ============================================================================
-- Verification
-- ============================================================================

-- Run this after migration to verify:
-- SELECT table_name, column_name FROM information_schema.columns
-- WHERE column_name = 'tenant_id' AND table_schema = 'public'
-- ORDER BY table_name;
