version: '3.8'

services:
  # PHP-FPM Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: examinee-app
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
      - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - examinee-network
    depends_on:
      - db
      - redis
    environment:
      - DB_CONNECTION=pgsql
      - DB_HOST=db
      - DB_PORT=5432
      - DB_DATABASE=examinee_db
      - DB_USERNAME=examinee_user
      - DB_PASSWORD=examinee_password
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
      - QUEUE_CONNECTION=redis

  # Nginx Service
  nginx:
    image: nginx:alpine
    container_name: examinee-nginx
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www/html
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - examinee-network
    depends_on:
      - app

  # PostgreSQL Service
  db:
    image: postgres:16-alpine
    container_name: examinee-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: examinee_db
      POSTGRES_USER: examinee_user
      POSTGRES_PASSWORD: examinee_password
    ports:
      - "5433:5432"
    volumes:
      - dbdata:/var/lib/postgresql/data
    networks:
      - examinee-network

  # Redis Service
  redis:
    image: redis:alpine
    container_name: examinee-redis
    restart: unless-stopped
    ports:
      - "6380:6379"
    networks:
      - examinee-network

  # Queue Worker Service
  queue:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: examinee-queue
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
    networks:
      - examinee-network
    depends_on:
      - app
      - redis
    environment:
      - DB_CONNECTION=pgsql
      - DB_HOST=db
      - DB_PORT=5432
      - DB_DATABASE=examinee_db
      - DB_USERNAME=examinee_user
      - DB_PASSWORD=examinee_password
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - QUEUE_CONNECTION=redis
    command: php artisan queue:work --tries=3 --timeout=90

  # Node Service for building assets
  node:
    image: node:20-alpine
    container_name: examinee-node
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
    networks:
      - examinee-network
    command: sh -c "npm install && npm run build"
    profiles:
      - build

networks:
  examinee-network:
    driver: bridge

volumes:
  dbdata:
    driver: local
