# ============================================
# AlloyDB Setup Commands for Cloud Shell
# ============================================
# Copy-paste these one at a time into Cloud Shell

# -------------------------------------------
# STEP 1: Get AlloyDB Private IP
# -------------------------------------------
ALLOYDB_IP=$(gcloud alloydb instances describe vibn-primary \
  --cluster=vibn \
  --region=northamerica-northeast1 \
  --format="value(ipAddress)")

echo "AlloyDB IP: $ALLOYDB_IP"

# -------------------------------------------
# STEP 2: Test Connection
# -------------------------------------------
psql "host=$ALLOYDB_IP port=5432 user=mark@getacquired.com dbname=postgres sslmode=disable" -c "\l"

# -------------------------------------------
# STEP 3: Create vibn Database
# -------------------------------------------
psql "host=$ALLOYDB_IP port=5432 user=mark@getacquired.com dbname=postgres sslmode=disable" -c "CREATE DATABASE vibn;"

# -------------------------------------------
# STEP 4: Enable Extensions
# -------------------------------------------
psql "host=$ALLOYDB_IP port=5432 user=mark@getacquired.com dbname=vibn sslmode=disable" <<EOF
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT extname, extversion FROM pg_extension WHERE extname IN ('vector', 'uuid-ossp');
EOF

# -------------------------------------------
# STEP 5: Create a temporary SQL file for the schema
# -------------------------------------------
cat > /tmp/knowledge-chunks-schema.sql << 'EOFSCHEMA'
-- Enable required extensions (already done above, but safe to repeat)
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Create the knowledge_chunks table
CREATE TABLE IF NOT EXISTS knowledge_chunks (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  project_id TEXT NOT NULL,
  knowledge_item_id TEXT NOT NULL,
  chunk_index INT NOT NULL,
  content TEXT NOT NULL,
  embedding VECTOR(768) NOT NULL,
  source_type TEXT,
  importance TEXT CHECK (importance IN ('primary', 'supporting', 'irrelevant') OR importance IS NULL),
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Standard indexes
CREATE INDEX IF NOT EXISTS idx_knowledge_chunks_project_id 
  ON knowledge_chunks (project_id);

CREATE INDEX IF NOT EXISTS idx_knowledge_chunks_knowledge_item_id 
  ON knowledge_chunks (knowledge_item_id);

CREATE INDEX IF NOT EXISTS idx_knowledge_chunks_project_knowledge 
  ON knowledge_chunks (project_id, knowledge_item_id);

CREATE INDEX IF NOT EXISTS idx_knowledge_chunks_item_index 
  ON knowledge_chunks (knowledge_item_id, chunk_index);

-- Vector similarity index using IVFFlat
CREATE INDEX IF NOT EXISTS idx_knowledge_chunks_embedding 
  ON knowledge_chunks 
  USING ivfflat (embedding vector_cosine_ops) 
  WITH (lists = 100);

-- Auto-update trigger
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_knowledge_chunks_updated_at
  BEFORE UPDATE ON knowledge_chunks
  FOR EACH ROW
  EXECUTE FUNCTION update_updated_at_column();
EOFSCHEMA

# -------------------------------------------
# STEP 6: Run the Schema File
# -------------------------------------------
psql "host=$ALLOYDB_IP port=5432 user=mark@getacquired.com dbname=vibn sslmode=disable" -f /tmp/knowledge-chunks-schema.sql

# -------------------------------------------
# STEP 7: Verify Everything
# -------------------------------------------
psql "host=$ALLOYDB_IP port=5432 user=mark@getacquired.com dbname=vibn sslmode=disable" <<EOF
-- Check table exists
\dt knowledge_chunks

-- Check indexes
\di

-- Count rows (should be 0)
SELECT COUNT(*) FROM knowledge_chunks;

-- Test vector operations
SELECT 1 as test;
EOF

echo ""
echo "✅ AlloyDB setup complete!"
echo ""
echo "Connection string for your .env.local:"
echo "ALLOYDB_HOST=$ALLOYDB_IP"
echo "ALLOYDB_PORT=5432"
echo "ALLOYDB_USER=mark@getacquired.com"
echo "ALLOYDB_PASSWORD="
echo "ALLOYDB_DATABASE=vibn"
echo "ALLOYDB_SSL=false"

