services:
  app:
    build:
      context: ./php
    image: php:8.1
    volumes:
      - ./www:/var/www/html
    ports:
      - "${APP_PORT:-8080}:80"
    environment:
      - DB_PORT=3306
      - DB_HOST=${DB_HOST:-db}
      - MYSQL_DATABASE=${MYSQL_DATABASE:-electrotec}
      - MYSQL_USER=${MYSQL_USER:-root}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD:-}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root}
      - SETUP_TOKEN=${SETUP_TOKEN:-}
      - SEED_TOKEN=${SEED_TOKEN:-}
    depends_on:
      - db
    restart: always
    # Iniciar Apache directamente; no bloquear por la disponibilidad de MySQL
    command: apache2-foreground


  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
    # No es necesario exponer MySQL al host; phpMyAdmin accede por la red interna
    # Si necesitas acceso desde el host, mapea otro puerto, por ejemplo "3307:3306"
    # ports:
    #   - "3307:3306"
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - db
    environment:
      PMA_HOST: ${DB_HOST:-db}
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root}
    ports:
      - "8085:80"
    restart: always

volumes:
  db_data:
