name: hindsight
# Docker Compose file for Hindsight with Timescale extensions
# - pgvectorscale: DiskANN vector search (disk-based, scalable)
# - pg_textsearch: BM25 text search (high-performance keyword retrieval)
#
# Quick start:
# docker compose -f docker/docker-compose/timescale/docker-compose.yaml up -d --build
#
# Required environment variables:
# - HINDSIGHT_DB_PASSWORD: Password for the PostgreSQL user
# - OPENAI_API_KEY (or configure another LLM provider)
#
# Optional environment variables with defaults:
# - HINDSIGHT_VERSION: Hindsight application version (default: latest)
# - HINDSIGHT_DB_USER: PostgreSQL user (default: hindsight_user)
# - HINDSIGHT_DB_NAME: PostgreSQL database name (default: hindsight_db)

services:
  db:
    # Custom PostgreSQL image with Timescale extensions (pgvectorscale + pg_textsearch)
    build:
      context: .
      dockerfile: Dockerfile
    container_name: hindsight-db-timescale
    restart: always
    # Expose PostgreSQL port (using 5438 to avoid conflicts with other setups)
    ports:
      - "5438:5432"
    environment:
      POSTGRES_USER: ${HINDSIGHT_DB_USER:-hindsight_user}
      POSTGRES_PASSWORD: ${HINDSIGHT_DB_PASSWORD:-hindsight_password}
      POSTGRES_DB: ${HINDSIGHT_DB_NAME:-hindsight_db}
    volumes:
      - pg_data:/var/lib/postgresql/data
    networks:
      - hindsight-net
    # Health check to ensure database is ready
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U hindsight_user"]
      interval: 5s
      timeout: 5s
      retries: 5

  timescale-init:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      db:
        condition: service_healthy
    environment:
      - PGPASSWORD=${HINDSIGHT_DB_PASSWORD:-hindsight_password}
    command: >
      bash -c "
        echo 'PostgreSQL is ready - creating hindsight_db database';
        psql -h hindsight-db-timescale -p 5432 -U hindsight_user -c 'CREATE DATABASE hindsight_db;' 2>/dev/null || echo 'Database already exists';
        echo 'Installing Timescale extensions...';
        echo '1/3: Installing pgvector (required by pgvectorscale)...';
        psql -h hindsight-db-timescale -p 5432 -U hindsight_user -d hindsight_db -c 'CREATE EXTENSION IF NOT EXISTS vector CASCADE;';
        echo '2/3: Installing pgvectorscale (DiskANN vector search)...';
        psql -h hindsight-db-timescale -p 5432 -U hindsight_user -d hindsight_db -c 'CREATE EXTENSION IF NOT EXISTS vectorscale CASCADE;';
        echo '3/3: Installing pg_textsearch (BM25 text search)...';
        psql -h hindsight-db-timescale -p 5432 -U hindsight_user -d hindsight_db -c 'CREATE EXTENSION IF NOT EXISTS pg_textsearch CASCADE;';
        echo '';
        echo '✅ Timescale extensions installed successfully';
        echo '';
        echo 'Installed extensions:';
        psql -h hindsight-db-timescale -p 5432 -U hindsight_user -d hindsight_db -c \"\\dx\" | grep -E '(vector|vectorscale|pg_textsearch)';
      "
    restart: "no"
    networks:
      - hindsight-net

  hindsight:
    image: ghcr.io/vectorize-io/hindsight:${HINDSIGHT_VERSION:-latest}
    container_name: hindsight-app-timescale
    ports:
      - "8888:8888"
      - "9999:9999"
    environment:
      # LLM Configuration
      HINDSIGHT_API_LLM_PROVIDER: ${HINDSIGHT_API_LLM_PROVIDER:-openai}
      HINDSIGHT_API_LLM_API_KEY: ${OPENAI_API_KEY:-your-api-key}

      # Database Configuration
      HINDSIGHT_API_DATABASE_URL: postgresql://${HINDSIGHT_DB_USER:-hindsight_user}:${HINDSIGHT_DB_PASSWORD:-hindsight_password}@db:5432/${HINDSIGHT_DB_NAME:-hindsight_db}

      # Timescale Extensions
      # pgvectorscale: DiskANN algorithm for disk-based scalable vector search
      HINDSIGHT_API_VECTOR_EXTENSION: pgvectorscale
      # pg_textsearch: High-performance BM25 text search
      HINDSIGHT_API_TEXT_SEARCH_EXTENSION: pg_textsearch

    depends_on:
      db:
        condition: service_healthy
      timescale-init:
        condition: service_completed_successfully
    networks:
      - hindsight-net


networks:
  hindsight-net:
    driver: bridge

volumes:
  pg_data:
