version: "3.8"

services:
  redis:
    image: redis:7-alpine
    container_name: sqdf_redis
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    restart: unless-stopped

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: sqdf_backend
    working_dir: /var/www/html
    # Map host port 8002 -> container 8001 to avoid conflicts
    ports:
      - "8002:8001"
    env_file:
      - ./backend/.env
    environment:
      APP_ENV: local
      APP_DEBUG: "true"
      REDIS_HOST: redis
      CACHE_DRIVER: redis
      QUEUE_CONNECTION: redis
      SESSION_DRIVER: redis
    volumes:
      - ./backend:/var/www/html
    depends_on:
      - redis
    command: sh -c "composer install && php artisan key:generate || true && php artisan serve --host=0.0.0.0 --port=8001"
    restart: unless-stopped

  queue:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: sqdf_queue
    working_dir: /var/www/html
    env_file:
      - ./backend/.env
    environment:
      REDIS_HOST: redis
      CACHE_DRIVER: redis
      QUEUE_CONNECTION: redis
      SESSION_DRIVER: redis
    volumes:
      - ./backend:/var/www/html
    depends_on:
      - redis
    command: sh -c "composer install && php artisan queue:work --sleep=1 --tries=3"
    restart: unless-stopped

  frontend:
    image: node:20-alpine
    container_name: sqdf_frontend
    working_dir: /app
    # Map host port 5181 -> container 5173 to avoid conflicts
    ports:
      - "5181:5173"
    environment:
      # Digunakan oleh Vite proxy untuk mengarah ke backend container
      VITE_BACKEND_URL: http://backend:8001
      CHOKIDAR_USEPOLLING: "true"
    volumes:
      - ./frontend:/app
    depends_on:
      - backend
    command: sh -c "npm install && npm run dev -- --host --port 5173"
    restart: unless-stopped

volumes:
  redis-data: