x-osprey-image: &osprey-image tychodata/osprey:latest

services:
  #ollama:
  #  image: ollama/ollama:latest
  #  container_name: tychodata-ollama
  #  restart: always
  #  ports:
  #    - "11434:11434"
  #  volumes:
  #    - ollama_data:/root/.ollama
  #  healthcheck:
  #    test: ["CMD", "ollama", "list"]
  #    interval: 30s
  #    timeout: 10s
  #    retries: 3

  tychodata-db:
    image: pgvector/pgvector:pg16
    shm_size: 1g
    container_name: tychodata-osprey-db
    restart: always
    environment:
      POSTGRES_USER: "${TYCHODATA_OSPREY_USER:-admin}"
      POSTGRES_PASSWORD: "${TYCHODATA_OSPREY_PASSWORD:-osprey_default_2025}"
      POSTGRES_DB: "tychodata"
      # PostgreSQL Performance Tuning for 12GB RAM system
      # POSTGRES_SHARED_BUFFERS: "3GB"
      # POSTGRES_EFFECTIVE_CACHE_SIZE: "9GB"
      # POSTGRES_WORK_MEM: "64MB"
      # POSTGRES_MAINTENANCE_WORK_MEM: "512MB"
      # POSTGRES_MAX_CONNECTIONS: "100"
    # command:
      # - "postgres"
      # - "-c"
      # - "shared_buffers=3GB"
      # - "-c"
      # - "effective_cache_size=9GB"
      # - "-c"
      # - "work_mem=64MB"
      # - "-c"
      # - "maintenance_work_mem=512MB"
      # - "-c"
      # - "random_page_cost=1.1"
      # - "-c"
      # - "effective_io_concurrency=200"
      # - "-c"
      # - "max_parallel_workers_per_gather=2"
      # - "-c"
      # - "max_parallel_workers=4"
      # - "-c"
      # - "checkpoint_completion_target=0.9"
      # - "-c"
      # - "wal_buffers=16MB"
      # - "-c"
      # - "default_statistics_target=100"
    ports:
      - "5432:5432"
    volumes:
      - pg_data:/var/lib/postgresql/data

  tychodata:
    image: *osprey-image
    container_name: tychodata-osprey-app
    volumes:
      - uploads:/app/uploads
      - storage:/app/storage
      - certs:/app/certs          # Persists generated / uploaded SSL certs
    restart: always
    depends_on:
      - tychodata-db
      #- ollama
    ports:
      - "80:80"        # HTTP  — used for Let's Encrypt ACME challenge
      - "443:443"      # HTTPS — served directly by uvicorn
    environment:
      TYCHODATA_OSPREY_DBNAME: "tychodata"
      TYCHODATA_OSPREY_HOST: tychodata-db
      TYCHODATA_OSPREY_PORT: 5432
      TYCHODATA_OSPREY_SSLMODE: "disable"
      TYCHODATA_OSPREY_USER: "${TYCHODATA_OSPREY_USER:-admin}"
      TYCHODATA_OSPREY_PASSWORD: "${TYCHODATA_OSPREY_PASSWORD:-osprey_default_2025}"
      WORKERS: 4
      # SSL / HTTPS settings
      # ssl_mode: auto | self-signed | uploaded  (default: auto)
      # "auto" = priority chain: uploaded cert → self-signed fallback
      # To use Let's Encrypt: configure domain + email in Settings → SSL / HTTPS, then restart.
      TYCHODATA_OSPREY_SENDGRID_API_KEY: "${TYCHODATA_OSPREY_SENDGRID_API_KEY}"
      TYCHODATA_OSPREY_SSL_MODE: "${TYCHODATA_OSPREY_SSL_MODE:-auto}"
      TYCHODATA_OSPREY_SSL_PORT: "${TYCHODATA_OSPREY_SSL_PORT:-443}"
      TYCHODATA_OSPREY_SSL_CERT_DIR: "/app/certs"
    command: python run_https.py
    pull_policy: always

  tychodata-worker:
    image: *osprey-image
    container_name: tychodata-osprey-worker
    volumes:
      - uploads:/app/uploads
      - storage:/app/storage
    restart: always
    depends_on:
      - tychodata-db
      #- ollama
    mem_limit: 8g
    memswap_limit: 10g
    environment:
      TYCHODATA_OSPREY_DBNAME: "tychodata"
      TYCHODATA_OSPREY_HOST: tychodata-db
      TYCHODATA_OSPREY_PORT: 5432
      TYCHODATA_OSPREY_SSLMODE: "disable"
      TYCHODATA_OSPREY_USER: "${TYCHODATA_OSPREY_USER:-admin}"
      TYCHODATA_OSPREY_PASSWORD: "${TYCHODATA_OSPREY_PASSWORD:-osprey_default_2025}"
      TYCHODATA_OSPREY_SENDGRID_API_KEY: "${TYCHODATA_OSPREY_SENDGRID_API_KEY}"
      TYCHODATA_OSPREY_WORKER_WAIT_TIME: 5
      PYTHONUNBUFFERED: "1"

    command: python -m src.workers.job_worker --skip-first-run

  # One-off migrations container
  migrations:
    image: *osprey-image
    container_name: tychodata-osprey-migrations
    depends_on:
      - tychodata-db
    environment:
      TYCHODATA_OSPREY_DBNAME: "tychodata"
      TYCHODATA_OSPREY_HOST: tychodata-db
      TYCHODATA_OSPREY_PORT: 5432
      TYCHODATA_OSPREY_SSLMODE: "disable"
      TYCHODATA_OSPREY_USER: "${TYCHODATA_OSPREY_USER:-admin}"
      TYCHODATA_OSPREY_PASSWORD: "${TYCHODATA_OSPREY_PASSWORD:-osprey_default_2025}"
      TYCHODATA_OSPREY_SENDGRID_API_KEY: "${TYCHODATA_OSPREY_SENDGRID_API_KEY}"
    command: alembic upgrade head
    restart: "no"

volumes:
  pg_data:
  storage:
  uploads:
  certs:
  #ollama_data:
