version: '3.8'

services:
  # Laravel App
  app:
    build:
      context: ./docker/php
      dockerfile: Dockerfile
    container_name: laravel-app
    restart: always
    working_dir: /var/www
    volumes:
      - .:/var/www
    command: >
      /bin/sh -c 'mkdir -p /var/www/storage /var/www/bootstrap/cache &&
                  chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache &&
                  chmod -R 775 /var/www/storage /var/www/bootstrap/cache &&
                  composer install --no-dev --optimize-autoloader &&
                  php-fpm'
    healthcheck:
      test: [ "CMD", "sh", "-c", "pgrep php-fpm" ]
      interval: 30s
      timeout: 10s
      retries: 10
    networks:
      - app_network
    environment:
      - API_URL=http://web:80
      - DB_HOST=db_mysql
      - DB_PORT=3306
      - DB_DATABASE=unioil-app
      - DB_USERNAME=root
      - DB_PASSWORD=secret

  # Nginx (renamed to avoid conflict)
  nginx-frontend:
    image: nginx:1.26.3-alpine
    container_name: web-app
    restart: always
    ports:
      - "8000:80"
    volumes:
      - .:/var/www
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:rw
    depends_on:
      app:
        condition: service_healthy
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost" ]
      interval: 30s
      timeout: 10s
      retries: 5
    networks:
      - app_network

networks:
  app_network:
    external: true
    driver: bridge
