name: ${COMPOSE_PROJECT_NAME:-vozex-customer}

x-backend-env: &backend-env
  env_file:
    - ./.env.backend

x-backend-common: &backend-common
  image: ${BACKEND_IMAGE}
  restart: unless-stopped
  <<: *backend-env
  volumes:
    - backend_storage:/var/www/html/storage
    - backend_cache:/var/www/html/bootstrap/cache
  networks:
    - app_internal
  depends_on:
    db:
      condition: service_healthy
    redis:
      condition: service_healthy

services:
  backend:
    <<: *backend-common
    container_name: ${INSTALL_ID}_backend
    environment:
      CONTAINER_ROLE: backend
      WAIT_FOR_DB: ${BACKEND_WAIT_FOR_DB:-true}
      WAIT_FOR_REDIS: ${BACKEND_WAIT_FOR_REDIS:-true}
      RUN_MIGRATIONS: ${BACKEND_RUN_MIGRATIONS:-true}
    expose:
      - "8080"
    healthcheck:
      test: ["CMD", "php", "-r", "$$r=@file_get_contents('http://127.0.0.1:8080/api/health'); exit($$r===false?1:0);"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s

  queue:
    <<: *backend-common
    container_name: ${INSTALL_ID}_queue
    environment:
      CONTAINER_ROLE: queue
      WAIT_FOR_DB: ${BACKEND_WAIT_FOR_DB:-true}
      WAIT_FOR_REDIS: ${BACKEND_WAIT_FOR_REDIS:-true}
      RUN_MIGRATIONS: "false"
    command: ["php", "artisan", "queue:work", "redis", "--sleep=3", "--tries=3", "--timeout=120", "--max-time=3600", "--queue=default"]

  scheduler:
    <<: *backend-common
    container_name: ${INSTALL_ID}_scheduler
    environment:
      CONTAINER_ROLE: scheduler
      WAIT_FOR_DB: ${BACKEND_WAIT_FOR_DB:-true}
      WAIT_FOR_REDIS: ${BACKEND_WAIT_FOR_REDIS:-true}
      RUN_MIGRATIONS: "false"
    command: ["php", "artisan", "schedule:work"]

  frontend:
    image: ${FRONTEND_IMAGE}
    container_name: ${INSTALL_ID}_frontend
    restart: unless-stopped
    depends_on:
      backend:
        condition: service_healthy
    ports:
      - "${FRONTEND_PORT}:80"
    networks:
      - app_internal
      - marketplace_network
    healthcheck:
      test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1/ || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 10s

  db:
    image: ${MYSQL_IMAGE:-mysql:8.0.36}
    container_name: ${INSTALL_ID}_db
    restart: unless-stopped
    command:
      - --default-authentication-plugin=mysql_native_password
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - app_internal
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$${MYSQL_ROOT_PASSWORD} --silent"]
      interval: 15s
      timeout: 10s
      retries: 10
      start_period: 30s

  redis:
    image: ${REDIS_IMAGE:-redis:7.2-alpine}
    container_name: ${INSTALL_ID}_redis
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis_data:/data
    networks:
      - app_internal
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 15s
      timeout: 5s
      retries: 10

volumes:
  backend_storage:
    name: ${INSTALL_ID}_backend_storage
  backend_cache:
    name: ${INSTALL_ID}_backend_cache
  db_data:
    name: ${INSTALL_ID}_db_data
  redis_data:
    name: ${INSTALL_ID}_redis_data

networks:
  app_internal:
    driver: bridge
    internal: true
  marketplace_network:
    external: true
    name: ${MARKETPLACE_NETWORK:-marketplace_network}
