version: '3.8'

services:
  # Laravel Application
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: showprima_app
    restart: unless-stopped
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./storage/app/public:/var/www/public/storage
      - ./storage/logs:/var/www/storage/logs
    environment:
      - DB_CONNECTION=${DB_CONNECTION:-mysql}
      - DB_HOST=${DB_HOST:-mysql}
      - DB_PORT=${DB_PORT:-3306}
      - DB_DATABASE=${DB_DATABASE:-showprima_dev}
      - DB_USERNAME=${DB_USERNAME:-showprima}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
      - QUEUE_CONNECTION=redis
      - HORIZON_PREFIX=showprima_horizon
    ports:
      - "8000:80"
    depends_on:
      - redis
      - mysql
      - chrome
    networks:
      - showprima_network

  # Browsershot service for PDF generation (custom build)
  browsershot:
    build:
      context: ./docker/browsershot
      dockerfile: Dockerfile
    container_name: showprima_browsershot
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/html:delegated
      - ./docker/browsershot/php.ini:/usr/local/etc/php/php.ini
      - browsershot_cache:/var/www/html/storage/browsershot
      - chrome_data:/home/chrome
    environment:
      - APP_ENV=production
      - APP_DEBUG=false
      - CHROME_PATH=/usr/bin/google-chrome-stable
      - NODE_PATH=/usr/bin/node
      - PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable
      - BROWSERSHOT_TIMEOUT=300
      - BROWSERSHOT_WAIT_UNTIL=networkidle0
      - BROWSERSHOT_NO_SANDBOX=true
      - BROWSERSHOT_DISABLE_GPU=true
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_DATABASE=${DB_DATABASE:-showprima_dev}
      - DB_USERNAME=${DB_USERNAME:-showprima}
      - DB_PASSWORD=${DB_PASSWORD:-secret}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    networks:
      - showprima_network
    depends_on:
      redis:
        condition: service_healthy
      mysql:
        condition: service_healthy
    cap_add:
      - SYS_ADMIN
    security_opt:
      - seccomp=unconfined
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 3s
      retries: 3
      start_period: 5s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: showprima_mysql
    restart: unless-stopped
    environment:
      - MYSQL_DATABASE=${DB_DATABASE:-showprima_dev}
      - MYSQL_USER=${DB_USERNAME:-showprima}
      - MYSQL_PASSWORD=${DB_PASSWORD:-secret}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD:-root_secret}
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./docker/mysql/init:/docker-entrypoint-initdb.d
    networks:
      - showprima_network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD:-root_secret}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  # Nginx Web Server
  nginx:
    image: nginx:alpine
    container_name: showprima_nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./docker/nginx/conf.d:/etc/nginx/conf.d
      - ./docker/nginx/ssl:/etc/nginx/ssl
    depends_on:
      - app
    networks:
      - showprima_network

  # Redis Cache & Queue
  redis:
    image: redis:7.2-alpine
    container_name: showprima_redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru --appendonly yes --dir /data
    volumes:
      - redis_data:/data
    environment:
      - REDIS_REPLICATION_MODE=master
    networks:
      - showprima_network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Laravel Horizon (Queue Worker)
  horizon:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: showprima_horizon
    restart: unless-stopped
    working_dir: /var/www
    command: php artisan horizon
    volumes:
      - ./:/var/www
    environment:
      - DB_CONNECTION=${DB_CONNECTION:-mysql}
      - DB_HOST=${DB_HOST:-mysql}
      - DB_PORT=${DB_PORT:-3306}
      - DB_DATABASE=${DB_DATABASE:-showprima_dev}
      - DB_USERNAME=${DB_USERNAME:-showprima}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
      - QUEUE_CONNECTION=redis
      - HORIZON_PREFIX=showprima_horizon
    depends_on:
      - redis
      - mysql
    networks:
      - showprima_network

  # Chrome/Browsershot for PDF generation (browserless image)
  chrome:
    image: browserless/chrome:latest
    container_name: showprima_chrome
    restart: unless-stopped
    environment:
      - CONNECTION_TIMEOUT=60000
      - MAX_CONCURRENT_SESSIONS=10
      - PREBOOT_CHROME=true
      - KEEP_ALIVE=true
      - WORKSPACE_DELETE_EXPIRED=true
      - DEFAULT_BLOCK_ADS=true
      - DEFAULT_STEALTH=true
    ports:
      - "3333:3000"
    networks:
      - showprima_network
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2G
        reservations:
          cpus: '0.5'
          memory: 512M

  # Optional: Browserless service for heavy PDF generation
  browserless:
    image: browserless/chrome:latest
    container_name: showprima_browserless
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      - CONNECTION_TIMEOUT=60000
      - MAX_CONCURRENT_SESSIONS=10
      - MAX_QUEUE_LENGTH=20
      - PREBOOT_CHROME=true
      - KEEP_ALIVE=true
      - WORKSPACE_DELETE_EXPIRED=true
      - WORKSPACE_EXPIRE_DAYS=1
      - DEFAULT_BLOCK_ADS=true
      - DEFAULT_STEALTH=true
      - DEFAULT_HEADLESS=true
      - DEFAULT_IGNORE_HTTPS_ERRORS=true
      - FUNCTION_ENABLE_INCOGNITO_MODE=true
      - ENABLE_CORS=true
      - CORS_ALLOW_ORIGIN=*
      - TOKEN=${BROWSERLESS_TOKEN:-}
    networks:
      - showprima_network
    cap_add:
      - SYS_ADMIN
    profiles:
      - browserless
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000"]
      interval: 30s
      timeout: 3s
      retries: 3
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Optional: Redis Commander for web-based management UI (development only)
  redis-commander:
    image: rediscommander/redis-commander:latest
    container_name: showprima_redis_commander
    environment:
      - REDIS_HOSTS=local:redis:6379
    ports:
      - "8081:8081"
    depends_on:
      redis:
        condition: service_healthy
    networks:
      - showprima_network
    profiles:
      - debug

volumes:
  redis_data:
    driver: local
    name: showprima_redis_data
  mysql_data:
    driver: local
    name: showprima_mysql_data
  browsershot_cache:
    driver: local
    name: showprima_browsershot_cache
  chrome_data:
    driver: local
    name: showprima_chrome_data

networks:
  showprima_network:
    driver: bridge
    name: showprima_network