/**
 * src/db/migrations/005-add-customer-memories.ts
 * Migration: long-term memory store powering remember/recall tools.
 *
 * customer_id is TEXT (not FK to customers.id) so it can hold synthetic IDs
 * ("system-*"), WhatsApp phone numbers, or real customer UUIDs interchangeably.
 * Empty string ('') represents tenant-wide memory (everyone in the business
 * sees it), non-empty represents per-customer memory.
 */
import { createChildLogger } from '../../utils/logger.js';
const log = createChildLogger('db:migration:005');
const MIGRATION_SQL = `
CREATE TABLE IF NOT EXISTS customer_memories (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
  customer_id TEXT NOT NULL DEFAULT '',
  key TEXT NOT NULL,
  value TEXT NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(tenant_id, customer_id, key)
);

CREATE INDEX IF NOT EXISTS idx_customer_memories_tenant
  ON customer_memories(tenant_id);

CREATE INDEX IF NOT EXISTS idx_customer_memories_lookup
  ON customer_memories(tenant_id, customer_id, key);

-- Allow case-insensitive partial-key search for fuzzy recall.
CREATE INDEX IF NOT EXISTS idx_customer_memories_key_trgm
  ON customer_memories USING gin (key gin_trgm_ops);
`;
export async function migrate() {
    log.info('Running migration 005: add customer_memories table');
    log.info('Run the following SQL in Supabase SQL Editor:');
    log.info('-- (also enable pg_trgm extension once: CREATE EXTENSION IF NOT EXISTS pg_trgm;)');
    log.info(MIGRATION_SQL);
}
export async function rollback() {
    log.info('Rollback SQL:');
    log.info(`DROP TABLE IF EXISTS customer_memories CASCADE;`);
}
//# sourceMappingURL=005-add-customer-memories.js.map