services:
  postgres:
    image: timescale/timescaledb:latest-pg16
    container_name: wellnessapp-postgres
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-wellnessapp_local}
      POSTGRES_USER: ${POSTGRES_USER:-wellnessapp_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-change_me_local_password}
      POSTGRES_INITDB_ARGS: "-E UTF8"
    ports:
      - "${DB_PORT:-5432}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./scripts/init-db.sql:/docker-entrypoint-initdb.d/init-db.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 20
    restart: unless-stopped
    networks:
      - wellnessapp-network

  redis:
    image: redis:7.4-alpine
    container_name: wellnessapp-redis
    ports:
      # Use 6380 on host if system Redis uses 6379; set REDIS_PORT=6380 in .env.staging on server if needed
      - "${REDIS_PORT:-6380}:6379"
    volumes:
      - redis_data:/data
    command: ["redis-server", "--appendonly", "yes"]
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 20
    restart: unless-stopped
    networks:
      - wellnessapp-network

  api:
    build:
      context: .
      dockerfile: Dockerfile.staging
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - wellnessapp-network
    healthcheck:
      # Run inside container to avoid host loopback / IPv6 proxy issues (RHEL/AlmaLinux + Docker)
      # Using /api/v1/health endpoint (now in HealthCheckController)
      test: ["CMD", "node", "-e", "require('http').get('http://127.0.0.1:3000/api/v1/health', r => { r.on('data', () => {}); r.on('end', () => process.exit(r.statusCode === 200 ? 0 : 1)); }).on('error', () => process.exit(1))"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    environment:
      NODE_ENV: staging
      PORT: 3000
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: ${POSTGRES_DB:-wellnessapp_local}
      DB_USER: ${POSTGRES_USER:-wellnessapp_user}
      DB_PASSWORD: ${POSTGRES_PASSWORD:-change_me_local_password}
      DB_SSL_MODE: disable
      DATABASE_URL: postgresql://${POSTGRES_USER:-wellnessapp_user}:${POSTGRES_PASSWORD:-change_me_local_password}@postgres:5432/${POSTGRES_DB:-wellnessapp_local}?sslmode=disable
      REDIS_HOST: redis
      REDIS_PORT: 6379
      JWT_SECRET: ${JWT_SECRET}
      CORS_ORIGIN: ${CORS_ORIGIN:-http://localhost:3000}
      API_DOCS_ENABLED: ${API_DOCS_ENABLED:-true}
      GEMINI_API_KEY: ${GEMINI_API_KEY:-}
      AI_PROVIDER: ${AI_PROVIDER:-gemini}
    ports:
      - "3000:3000"

  migrate:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      NODE_ENV: development
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: ${POSTGRES_DB:-wellnessapp_local}
      DB_USER: ${POSTGRES_USER:-wellnessapp_user}
      DB_PASSWORD: ${POSTGRES_PASSWORD:-change_me_local_password}
      DB_SSL_MODE: disable
      DATABASE_URL: postgresql://${POSTGRES_USER:-wellnessapp_user}:${POSTGRES_PASSWORD:-change_me_local_password}@postgres:5432/${POSTGRES_DB:-wellnessapp_local}?sslmode=disable
      JWT_SECRET: ${JWT_SECRET:-dev_change_me_please_32_chars_minimum}
    command: ["npm", "run", "migrate:node"]
    networks:
      - wellnessapp-network
    profiles:
      - tools

  # pgAdmin for database management (optional, for local dev only)
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: wellnessapp-pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@wellnessapp.local}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - wellnessapp-network
    profiles:
      - tools  # Only start with: docker compose --profile tools up

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local

networks:
  wellnessapp-network:
    driver: bridge

