services:
  # Aplicación PHP/Laravel
  app:
    build:
      context: .
      dockerfile: .docker/php/Dockerfile
      args:
        USER_ID: ${USER_ID:-1000}
        GROUP_ID: ${GROUP_ID:-1000}
    restart: unless-stopped
    working_dir: /var/www
    volumes:
      - .:/var/www:cached,Z
      - vendor:/var/www/vendor
    environment:
      - APP_NAME=${APP_NAME:-Laravel}
      - APP_ENV=${APP_ENV:-local}
      - APP_KEY=${APP_KEY}
      - APP_DEBUG=${APP_DEBUG:-true}
      - APP_URL=${APP_URL}
      - DB_CONNECTION=${DB_CONNECTION:-mysql}
      - DB_HOST=${DB_HOST:-db} 
      - DB_PORT=${DB_PORT:-3306}
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
      - SESSION_DRIVER=${SESSION_DRIVER:-database}
      - CACHE_STORE=${CACHE_STORE:-database}
      - QUEUE_CONNECTION=${QUEUE_CONNECTION:-database}
    networks:
      - laravel_network
    expose:
      - 9000
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      db:
        condition: service_healthy

  # Node.js para Vite - USAR EL MISMO USER_ID
  node:
    build:
      context: .
      dockerfile: .docker/node/Dockerfile
      args:
        USER_ID: ${USER_ID:-1000}
        GROUP_ID: ${GROUP_ID:-1000}
    container_name: vue-build
    restart: unless-stopped
    working_dir: /var/www
    command: sh -c "npm run dev -- --host 0.0.0.0 --port 5173 --strictPort"
    volumes:
      - .:/var/www:cached
      - node_modules:/var/www/node_modules
    ports:
      - "5173:5173"
    environment:
      - NODE_ENV=development
      - CHOKIDAR_USEPOLLING=true
      - VITE_HOST=0.0.0.0
      - VITE_PORT=5173
      - VITE_HMR_HOST=localhost
      - VITE_HMR_PORT=5173
    networks:
      - laravel_network

  # Nginx
  nginx:
    image: nginx:1.25-alpine
    container_name: laravel-nginx
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - .:/var/www:cached
      - ./.docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro,Z
    depends_on:
      - app
      - node
    networks:
      - laravel_network
  # Servicio de Base de Datos
  db:
    image: mysql:8.0.42
    container_name: laravel-db
    restart: unless-stopped
    ports:
      - "3309:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-root}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d:ro,Z
    networks:
      - laravel_network
    healthcheck:
      test:
        - CMD-SHELL
        - >
          mysql -h127.0.0.1 -u"$$MYSQL_USER" -p"$$MYSQL_PASSWORD"
          -e "SELECT 1" "$$MYSQL_DATABASE" >/dev/null 2>&1
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s


volumes:
  node_modules:
    driver: local
  vendor:
    driver: local
  mysql_data:
    driver: local

networks:
  laravel_network:
    driver: bridge

