x-app-env: &app-env
  APP_ENV: ${APP_ENV:-production}
  APP_DEBUG: ${APP_DEBUG:-false}
  DB_HOST: db
  DB_PORT: 3306
  DB_DATABASE: ${DB_DATABASE:-volley_tournaments}
  DB_USERNAME: ${DB_USERNAME:-volley}
  DB_PASSWORD: ${DB_PASSWORD:-change-volley-password}
  JWT_SECRET: ${JWT_SECRET:-change-this-long-random-secret}
  CORS_ORIGIN: ${CORS_ORIGIN:-*}

services:
  app:
    image: ${APP_IMAGE:-volley-manager:latest}
    restart: unless-stopped
    ports:
      - "${APP_PORT:-8080}:80"
    environment:
      <<: *app-env
    volumes:
      - uploads_data:/var/www/html/public/uploads
      - logs_data:/var/www/html/storage/logs
    depends_on:
      db:
        condition: service_healthy

  websocket:
    image: ${APP_IMAGE:-volley-manager:latest}
    restart: unless-stopped
    command: php /var/www/html/bin/websocket-server.php
    ports:
      - "${WS_PORT:-8081}:8081"
    environment:
      <<: *app-env
    depends_on:
      db:
        condition: service_healthy

  init:
    image: ${APP_IMAGE:-volley-manager:latest}
    profiles:
      - init
    command: php /var/www/html/scripts/seed_ltv26_template.php
    environment:
      <<: *app-env
    depends_on:
      db:
        condition: service_healthy

  db:
    image: mysql:8.4
    restart: unless-stopped
    ports:
      - "${MYSQL_PORT:-3307}:3306"
    environment:
      MYSQL_DATABASE: ${DB_DATABASE:-volley_tournaments}
      MYSQL_USER: ${DB_USERNAME:-volley}
      MYSQL_PASSWORD: ${DB_PASSWORD:-change-volley-password}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-change-root-password}
    volumes:
      - db_data:/var/lib/mysql
      - ./database/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro
      - ./database/seeds.sql:/docker-entrypoint-initdb.d/02-seeds.sql:ro
      - ./database/migrations/20260519_scoresheet_advanced.sql:/docker-entrypoint-initdb.d/03-scoresheet-advanced.sql:ro
      - ./database/migrations/20260519_ltv26_template_fields.sql:/docker-entrypoint-initdb.d/04-ltv26-template-fields.sql:ro
      - ./database/migrations/20260519_sheet_templates.sql:/docker-entrypoint-initdb.d/05-sheet-templates.sql:ro
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-p${DB_ROOT_PASSWORD:-change-root-password}"]
      interval: 5s
      timeout: 5s
      retries: 20

volumes:
  db_data:
  uploads_data:
  logs_data:
