services:
  # ---------------------------------------------------------
  # Idempotent bootstrap: creates the app role + database
  # only if they don't already exist. Safe to run every time.
  # ---------------------------------------------------------
  mplatform-db-init:
    image: postgres:16
    depends_on:
      mplatform-postgres:
        condition: service_healthy
    environment:
      PGHOST: mplatform-postgres
      PGUSER: postgres
      PGPASSWORD: "postgres_admin_password"
      APP_USER: muser
      APP_PASSWORD: mpassword
      APP_DB: mplatform
    entrypoint: ["bash", "-c"]
    command:
      - |
        set -e
        psql -v ON_ERROR_STOP=1 <<-EOSQL
          DO \$\$
          BEGIN
             IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${APP_USER}') THEN
                CREATE ROLE ${APP_USER} LOGIN PASSWORD '${APP_PASSWORD}';
             ELSE
                ALTER ROLE ${APP_USER} WITH PASSWORD '${APP_PASSWORD}';
             END IF;
          END
          \$\$;
        EOSQL
        psql -tc "SELECT 1 FROM pg_database WHERE datname = '${APP_DB}'" | grep -q 1 || \
          psql -c "CREATE DATABASE ${APP_DB} OWNER ${APP_USER}"
        psql -c "GRANT ALL PRIVILEGES ON DATABASE ${APP_DB} TO ${APP_USER}"
    networks:
      - mplatform-net

  # ---------------------------------------------------------
  # OPTIONAL: only needed if mplatform does NOT auto-migrate
  # on startup. If it does (Database.Migrate() in Program.cs),
  # delete this service and remove the depends_on block for it
  # further down.
  # ---------------------------------------------------------
  mplatform-migrate:
    image: mvctechnologies/mplatform:latest
    depends_on:
      mplatform-db-init:
        condition: service_completed_successfully
    environment:
      ASPNETCORE_ENVIRONMENT: "Production"
      ConnectionStrings__DefaultConnection: "Host=mplatform-postgres;Port=5432;Database=mplatform;Username=muser;Password=mpassword"
    entrypoint: ["dotnet", "mplatform.dll", "--migrate-only"]  # adjust to the actual CLI flag/command
    networks:
      - mplatform-net
    restart: "no"

  mplatform:
    image: mvctechnologies/mplatform:latest
    depends_on:
      mplatform-db-init:
        condition: service_completed_successfully
      # If using mplatform-migrate, swap the above dependency to this instead:
      # mplatform-migrate:
      #   condition: service_completed_successfully
      mplatform-rabbitmq:
        condition: service_healthy
    environment:
      ASPNETCORE_ENVIRONMENT: "Production"
      ConnectionStrings__DefaultConnection: "Host=mplatform-postgres;Port=5432;Database=mplatform;Username=muser;Password=mpassword"
      RabbitMq__Host: "mplatform-rabbitmq"
      RabbitMq__Username: "muser"
      RabbitMq__Password: "mpassword"
      RabbitMq__VirtualHost: "/"
    volumes:
      - ./keys:/keys
    ports:
      - "8080:8080"
    restart: unless-stopped
    networks:
      - mplatform-net

  mplatform-worker:
    image: mvctechnologies/mplatform-worker:latest
    depends_on:
      mplatform-db-init:
        condition: service_completed_successfully
      mplatform-rabbitmq:
        condition: service_healthy
    environment:
      ASPNETCORE_ENVIRONMENT: "Production"
      ConnectionStrings__DefaultConnection: "Host=mplatform-postgres;Port=5432;Database=mplatform;Username=muser;Password=mpassword"
      RabbitMq__Host: "mplatform-rabbitmq"
      RabbitMq__Username: "muser"
      RabbitMq__Password: "mpassword"
      RabbitMq__VirtualHost: "/"
    volumes:
      - ./keys:/keys
    restart: unless-stopped
    networks:
      - mplatform-net

  mplatform-postgres:
    image: postgres:16
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: "postgres_admin_password"
      # No POSTGRES_DB here on purpose — mplatform-db-init owns
      # creating the real app database/role, idempotently.
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks:
      - mplatform-net

  mplatform-rabbitmq:
    image: rabbitmq:3-management
    environment:
      RABBITMQ_DEFAULT_USER: muser
      RABBITMQ_DEFAULT_PASS: mpassword
    ports:
      - "15672:15672"
      - "5672:5672"
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "check_port_connectivity"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks:
      - mplatform-net

volumes:
  postgres_data:
  rabbitmq_data:

networks:
  mplatform-net:
    driver: bridge