version: "3.9"

services:
  postgres:
    image: postgres:17
    container_name: openproject-postgres
    restart: unless-stopped

    env_file:
      - .env

    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

    volumes:
      - ./volumes/pgdata_v17:/var/lib/postgresql/data

    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 10

    networks:
      - openproject

  openproject:
    image: openproject/openproject:17.6
    container_name: openproject
    restart: unless-stopped

    depends_on:
      postgres:
        condition: service_healthy

    env_file:
      - .env

    ports:
      # MAPEAMENTO CORRETO: Direciona a porta da VPS (ex: 8088) para a porta 80 do container
      - "${OPENPROJECT_PORT}:80"

    volumes:
      - ./volumes/assets:/var/openproject/assets

    environment:
      DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}

      OPENPROJECT_HOST__NAME: ${OPENPROJECT_HOST_NAME}
      OPENPROJECT_HTTPS: ${OPENPROJECT_HTTPS}
      
      # Garante o envio correto das duas variáveis para o motor Rails do OpenProject
      SECRET_KEY_BASE: ${OPENPROJECT_SECRET_KEY_BASE}
      OPENPROJECT_SECRET_KEY_BASE: ${OPENPROJECT_SECRET_KEY_BASE}

      RAILS_MIN_THREADS: 4
      RAILS_MAX_THREADS: 16

    networks:
      - openproject

networks:
  openproject:
    driver: bridge
