services:
  nginx:
    image: nginx:1.25-alpine
    container_name: medoracle-nginx-dev
    depends_on:
      - app
    ports:
      - "80:80"
    volumes:
      - ./nginx/nginx.dev.conf:/etc/nginx/conf.d/default.conf:ro
      - ./public/uploads:/var/www/html/public/uploads:ro
    restart: unless-stopped
  app:
    build:
      context: .
      dockerfile: Dockerfile.dev
    container_name: medoracle-app-dev
    image: medoracle-app-dev:latest

    depends_on:
      migrate:
        condition: service_completed_successfully
    environment:
      ENVIRONMENT: ${ENVIRONMENT:-dev}
      DEBUG: ${DEBUG:-true}
      HMR: ${HMR:-0}
      DB_DRIVER: ${DB_DRIVER:-mysql}
      DB_HOST: ${DB_HOST:-db}
      DB_PORT: ${DB_PORT:-3306}
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
    volumes:
      # CRITICAL: Only mount uploads directory (needed for user uploads)
      # All other files are INSIDE the image for maximum performance on Windows
      - ./public/uploads:/var/www/html/public/uploads:delegated
      # Named volumes for cache/logs (much faster than bind mounts)
      - cache:/var/www/html/var/cache
      - logs:/var/www/html/var/log
      - extensions:/var/www/html/extra/extensions
      - artifacts:/var/www/html/extra/artifacts

  db:
    image: mysql:8.0
    container_name: medoracle-db-dev
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    ports:
      - "3307:3306"
    command: ["--default-authentication-plugin=mysql_native_password", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "-uroot", "-p${DB_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 20
      start_period: 30s
    volumes:
      - db_data_dev:/var/lib/mysql
    restart: unless-stopped

  migrate:
    image: medoracle-app-dev:latest
    container_name: medoracle-migrate-dev
    working_dir: /var/www/html
    depends_on:
      db:
        condition: service_healthy
    entrypoint:
      - sh
      - -c
      - |
        set -e
        echo 'Aguardando banco de dados estar completamente pronto...'
        sleep 10

        echo 'Testando conexão com o banco...'
        echo "Usando credenciais do ambiente (.env.dev)"
        max_attempts=30
        attempt=0
        until php -r '$$h=getenv("DB_HOST");$$P=getenv("DB_PORT")?:3306;$$u=getenv("DB_USER");$$p=getenv("DB_PASSWORD");$$n=getenv("DB_NAME");try{$$pdo=new PDO("mysql:host=$$h;port=$$P;dbname=$$n",$$u,$$p);echo "OK\n";exit(0);}catch(Throwable $$e){echo "Erro: ".$$e->getMessage()."\n";exit(1);}'; do
          attempt=$$((attempt + 1))
          if [ $$attempt -ge $$max_attempts ]; then
            echo "Falha ao conectar ao banco após $$max_attempts tentativas"
            exit 1
          fi
          echo "Aguardando conexão com o banco... (tentativa $$attempt/$$max_attempts)"
          sleep 2
        done

        echo 'Banco conectado! Executando migrations...'
        php /var/www/html/bin/console migrations:migrate --no-interaction --all-or-nothing=0
        echo 'Migrations executadas com sucesso!'
    environment:
      ENVIRONMENT: ${ENVIRONMENT}
      DEBUG: ${DEBUG}
      HMR: ${HMR}
      DB_DRIVER: ${DB_DRIVER}
      DB_HOST: ${DB_HOST}
      DB_PORT: ${DB_PORT}
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
    restart: "no"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: phpmyadmin-dev
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    ports:
      - "8081:80"
    depends_on:
      - db
    restart: unless-stopped
      
volumes:
  cache:
  logs:
  extensions:
  artifacts:
  db_data_dev:
  uploads:

