services:
  nginx:
    image: nginx:alpine
    container_name: bpdlh-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./docker/nginx/conf.d:/etc/nginx/conf.d
      - .:/var/www/html
    depends_on:
      - php
    networks:
      - bpdlh

  php:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    container_name: bpdlh-php
    volumes:
      - .:/var/www/html
    environment:
      APP_ENV: ${APP_ENV:-local}
      APP_DEBUG: ${APP_DEBUG:-true}
      DB_HOST: postgres
      DB_PORT: 5432
      DB_DATABASE: ${DB_DATABASE:-bpdlh_los}
      DB_USERNAME: ${DB_USERNAME:-bpdlh}
      DB_PASSWORD: ${DB_PASSWORD:-secret}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      MINIO_ENDPOINT: http://minio:9000
      MINIO_KEY: ${MINIO_KEY:-minioadmin}
      MINIO_SECRET: ${MINIO_SECRET:-minioadmin}
      MINIO_BUCKET: ${MINIO_BUCKET:-bpdlh-files}
      QUEUE_CONNECTION: redis
      CACHE_DRIVER: redis
      SESSION_DRIVER: redis
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
      minio:
        condition: service_started
    networks:
      - bpdlh

  postgres:
    image: postgis/postgis:16-3.4
    container_name: bpdlh-postgres
    environment:
      POSTGRES_DB: ${DB_DATABASE:-bpdlh_los}
      POSTGRES_USER: ${DB_USERNAME:-bpdlh}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-secret}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME:-bpdlh} -d ${DB_DATABASE:-bpdlh_los}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - bpdlh

  redis:
    image: redis:7-alpine
    container_name: bpdlh-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    networks:
      - bpdlh

  minio:
    image: minio/minio:latest
    container_name: bpdlh-minio
    environment:
      MINIO_ROOT_USER: ${MINIO_KEY:-minioadmin}
      MINIO_ROOT_PASSWORD: ${MINIO_SECRET:-minioadmin}
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data
    command: server /data --console-address ":9001"
    networks:
      - bpdlh

  queue-worker:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    container_name: bpdlh-queue
    volumes:
      - .:/var/www/html
    command: php artisan queue:work redis --queue=critical,default --max-time=3600 --tries=3
    environment:
      APP_ENV: ${APP_ENV:-local}
      DB_HOST: postgres
      DB_PORT: 5432
      DB_DATABASE: ${DB_DATABASE:-bpdlh_los}
      DB_USERNAME: ${DB_USERNAME:-bpdlh}
      DB_PASSWORD: ${DB_PASSWORD:-secret}
      REDIS_HOST: redis
      QUEUE_CONNECTION: redis
    depends_on:
      - php
      - redis
    restart: unless-stopped
    networks:
      - bpdlh

volumes:
  postgres_data:
  redis_data:
  minio_data:

networks:
  bpdlh:
    driver: bridge
