version: '3.9'

# =============================================================================
#  Agency PM — Docker Compose (Local Development)
#  Usage:
#    docker compose up -d          ← start all services
#    docker compose exec app bash  ← open shell in Laravel container
#    docker compose down           ← stop everything
#    docker compose down -v        ← stop + delete volumes (wipes DB)
# =============================================================================

services:

  # ── MySQL ──────────────────────────────────────────────────────────────────
  db:
    image: mysql:8.0
    container_name: agencypm_db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: root_secret
      MYSQL_DATABASE:      agencypm
      MYSQL_USER:          agencypm_user
      MYSQL_PASSWORD:      agencypm_pass
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-proot_secret"]
      interval: 5s
      retries: 10

  # ── PHP-FPM (Laravel) ──────────────────────────────────────────────────────
  app:
    build:
      context: ./backend
      dockerfile: ../docker/Dockerfile.php
    container_name: agencypm_app
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./backend:/var/www/html
      - ./docker/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro
    environment:
      APP_ENV:          local
      APP_DEBUG:        "true"
      DB_HOST:          db
      DB_PORT:          3306
      DB_DATABASE:      agencypm
      DB_USERNAME:      agencypm_user
      DB_PASSWORD:      agencypm_pass
      FRONTEND_URL:     http://localhost:5173
      SANCTUM_STATEFUL_DOMAINS: localhost:5173
    depends_on:
      db:
        condition: service_healthy

  # ── Nginx ──────────────────────────────────────────────────────────────────
  nginx:
    image: nginx:1.25-alpine
    container_name: agencypm_nginx
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - ./backend:/var/www/html:ro
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - app

  # ── Vite Dev Server (React) ────────────────────────────────────────────────
  frontend:
    image: node:20-alpine
    container_name: agencypm_frontend
    working_dir: /app
    volumes:
      - ./frontend:/app
      - frontend_nm:/app/node_modules
    ports:
      - "5173:5173"
    environment:
      VITE_API_URL: http://localhost:8000/api/v1
    command: sh -c "npm install && npm run dev -- --host 0.0.0.0"
    depends_on:
      - nginx

  # ── Queue Worker ──────────────────────────────────────────────────────────
  queue:
    build:
      context: ./backend
      dockerfile: ../docker/Dockerfile.php
    container_name: agencypm_queue
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./backend:/var/www/html
    command: php artisan queue:work --sleep=3 --tries=3
    environment:
      DB_HOST:     db
      DB_PORT:     3306
      DB_DATABASE: agencypm
      DB_USERNAME: agencypm_user
      DB_PASSWORD: agencypm_pass
    depends_on:
      db:
        condition: service_healthy

volumes:
  db_data:
  frontend_nm:
