# =============================================================================
# Sehat Sahoolat — Local development stack (works on macOS / Windows / Linux)
# =============================================================================
#
# What this brings up on `docker compose up -d`:
#   - postgres : pgvector-enabled Postgres 16 (port 5432)
#   - redis    : Redis 7 (port 6379)
#   - whisper  : faster-whisper transcription sidecar (port 8001)
#   - backend  : NestJS API (port 3000) — auto-runs migrations + seeds on boot
#
# Frontends (patient/doctor/admin portals) run on the host with Node 22+ for
# faster hot-reload. After `docker compose up -d` finishes:
#
#   cd web && npm install
#   cd apps/patient-portal && npx next dev -p 3001
#   cd apps/admin-portal   && npx next dev -p 3003
#   cd apps/doctor-portal  && npx next dev -p 3002
#
# First-run setup is documented in README.md → "Quickstart (Windows / Mac)".
# =============================================================================

name: sehat-dev

services:
  postgres:
    # pgvector/pgvector ships postgres:16 with the `vector` extension already
    # available. Required for the knowledge-base RAG pipeline.
    image: pgvector/pgvector:pg16
    container_name: sehat-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: sehat
      POSTGRES_PASSWORD: sehat_dev
      POSTGRES_DB: sehat_sahoolat
    ports:
      - '5432:5432'
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U sehat -d sehat_sahoolat']
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: sehat-redis
    restart: unless-stopped
    ports:
      - '6379:6379'
    volumes:
      - redis_data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 5

  whisper:
    build:
      context: .
      dockerfile: deploy/dockerfiles/Dockerfile.whisper
    container_name: sehat-whisper
    restart: unless-stopped
    environment:
      WHISPER_MODEL: small
      WHISPER_DEVICE: cpu
      WHISPER_COMPUTE_TYPE: int8
      WHISPER_CACHE_DIR: /home/sehat/.cache/huggingface
      HF_HOME: /home/sehat/.cache/huggingface
    ports:
      - '8001:8001'
    volumes:
      - whisper_cache:/home/sehat/.cache/huggingface

  backend:
    build:
      context: .
      dockerfile: deploy/dockerfiles/Dockerfile.backend
    container_name: sehat-backend
    restart: unless-stopped
    env_file:
      - ./backend/.env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      NODE_ENV: development
      PORT: 3000
      DB_HOST: postgres
      DB_PORT: 5432
      DB_USERNAME: sehat
      DB_PASSWORD: sehat_dev
      DB_DATABASE: sehat_sahoolat
      DB_SSL: 'false'
      REDIS_HOST: redis
      REDIS_PORT: 6379
      # Dev defaults — replace in production
      JWT_SECRET: dev-jwt-secret-change-in-production-immediately
      JWT_EXPIRES_IN: 12h
      JWT_REFRESH_SECRET: dev-refresh-secret-change-in-production
      JWT_REFRESH_EXPIRES_IN: 7d
      ENCRYPTION_KEY: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
      FRONTEND_URL: http://localhost:3001,http://localhost:3002,http://localhost:3003
      WHISPER_URL: http://whisper:8001
      WHISPER_BASE_URL: http://whisper:8001
      # Optional secrets — leave blank in dev or supply via .env file at repo root
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-}
      AGORA_APP_ID: ${AGORA_APP_ID:-}
      AGORA_APP_CERTIFICATE: ${AGORA_APP_CERTIFICATE:-}
      GOOGLE_APPLICATION_CREDENTIALS: ${GOOGLE_APPLICATION_CREDENTIALS:-/app/secrets/firebase-admin.json}
      FIREBASE_CREDENTIALS_PATH: ${FIREBASE_CREDENTIALS_PATH:-/app/secrets/firebase-admin.json}
      SMTP_HOST: ${SMTP_HOST:-}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_SECURE: ${SMTP_SECURE:-false}
      SMTP_USER: ${SMTP_USER:-}
      SMTP_PASS: ${SMTP_PASS:-}
      SMTP_FROM: ${SMTP_FROM:-Sehat Sahoolat <admin@sehatsahoolat.com>}
    ports:
      - '3000:3000'
    volumes:
      - backend_uploads:/app/uploads
      - ./backend/secrets:/app/secrets:ro
    # On first boot: run migrations + seed dev data, then start the server.
    # On subsequent boots: migrations are idempotent (no-op if already applied)
    # and the seed script skips inserts when rows already exist.
    command: >
      sh -c "
        node ./node_modules/typeorm/cli.js migration:run -d ./dist/src/data-source.js &&
        (node ./dist/scripts/dev-seed.js || true) &&
        node dist/src/main.js
      "

volumes:
  postgres_data:
  redis_data:
  whisper_cache:
  backend_uploads:
