services:
  app:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    working_dir: /var/www/html
    volumes:
      - .:/var/www/html
      - vendor_data:/var/www/html/vendor
    environment:
      COMPOSER_ALLOW_SUPERUSER: 1
    healthcheck:
      test: ["CMD-SHELL", "php -r 'exit(extension_loaded(\"pdo_pgsql\") && extension_loaded(\"redis\") ? 0 : 1);'"]
      interval: 10s
      timeout: 5s
      retries: 10
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
      minio-init:
        condition: service_completed_successfully

  web:
    image: nginx:1.27-alpine
    ports:
      - "8080:80"
    volumes:
      - .:/var/www/html:ro
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - app

  worker:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    command: php artisan queue:work --queue=imports,communications,signatures,pdf,default --sleep=2 --tries=3 --timeout=1800
    working_dir: /var/www/html
    volumes:
      - .:/var/www/html
      - vendor_data:/var/www/html/vendor
    depends_on:
      app:
        condition: service_healthy

  scheduler:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    command: sh -lc "while true; do php artisan schedule:run --verbose --no-interaction; sleep 60; done"
    working_dir: /var/www/html
    volumes:
      - .:/var/www/html
      - vendor_data:/var/www/html/vendor
    depends_on:
      app:
        condition: service_healthy

  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: digital_dossier
      POSTGRES_USER: digital_dossier
      POSTGRES_PASSWORD: change-me
    ports:
      - "54320:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U digital_dossier -d digital_dossier"]
      interval: 5s
      timeout: 5s
      retries: 20

  redis:
    image: redis:7-alpine
    ports:
      - "63790:6379"

  minio:
    image: minio/minio:latest
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minio
      MINIO_ROOT_PASSWORD: minio-secret
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data

  minio-init:
    image: minio/mc:latest
    depends_on:
      - minio
    entrypoint: >
      /bin/sh -c "
      until mc alias set local http://minio:9000 minio minio-secret >/dev/null 2>&1; do sleep 1; done;
      mc mb --ignore-existing local/digital-dossier;
      exit 0;
      "

  mailpit:
    image: axllent/mailpit:latest
    ports:
      - "1025:1025"
      - "8025:8025"

volumes:
  postgres_data:
  minio_data:
  vendor_data:
