version: '3.8'

services:
  # PHP Application
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: itplus-app
    working_dir: /var/www/html
    volumes:
      - .:/var/www/html
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=itplusus_itplus
      - DB_USERNAME=itplus
      - DB_PASSWORD=itplus_password
      - REDIS_HOST=redis
      - CACHE_STORE=redis
    networks:
      - itplus-network
    command: >
      sh -c "composer install &&
             php artisan migrate --force &&
             php artisan serve --host=0.0.0.0"

  # MySQL Database
  db:
    image: mysql:8.0
    container_name: itplus-db
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: itplusus_itplus
      MYSQL_USER: itplus
      MYSQL_PASSWORD: itplus_password
    volumes:
      - itplus-mysql:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - itplus-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1"]
      timeout: 20s
      retries: 10

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: itplus-redis
    ports:
      - "6379:6379"
    networks:
      - itplus-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      timeout: 3s
      retries: 5

  # PhpMyAdmin (Database Management - Optional)
  phpmyadmin:
    image: phpmyadmin
    container_name: itplus-phpmyadmin
    depends_on:
      - db
    environment:
      PMA_HOST: db
      PMA_USER: root
      PMA_PASSWORD: root_password
    ports:
      - "8080:80"
    networks:
      - itplus-network

volumes:
  itplus-mysql:

networks:
  itplus-network:
    driver: bridge
