services:
  caddy:
    image: caddy:2.8-alpine
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    env_file:
      - .env.production
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - web
      - api

  web:
    build:
      context: ./hippo-leads
      dockerfile: Dockerfile
    restart: unless-stopped
    env_file:
      - .env.production
    environment:
      NODE_ENV: production
      PORT: 3000
      NEXT_PUBLIC_API_BASE_URL: https://${DOMAIN}
      NEXT_TELEMETRY_DISABLED: "1"
    depends_on:
      - api

  api:
    build:
      context: ./leadfinder-api
      dockerfile: Dockerfile
    restart: unless-stopped
    env_file:
      - .env.production
    environment:
      APP_ENV: production
      APP_DEBUG: "false"
      APP_URL: https://${DOMAIN}
      FRONTEND_URL: https://${DOMAIN}
      DB_CONNECTION: pgsql
      DB_HOST: db
      DB_PORT: 5432
      DB_DATABASE: ${POSTGRES_DB}
      DB_USERNAME: ${POSTGRES_USER}
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      CACHE_DRIVER: redis
      QUEUE_CONNECTION: redis
      SESSION_DRIVER: database
      LOG_CHANNEL: stderr
      SEARCH_ENRICH_AI: ${SEARCH_ENRICH_AI}
      SEARCH_ENRICH_WEBSITE: ${SEARCH_ENRICH_WEBSITE}
      SEARCH_AGENCY_FALLBACK: ${SEARCH_AGENCY_FALLBACK}
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      OPENAI_ENRICH_MODEL: ${OPENAI_ENRICH_MODEL}
      SERPAPI_API_KEY: ${SERPAPI_API_KEY}
      BOUNCER_API_KEY: ${BOUNCER_API_KEY}
    depends_on:
      - db
      - redis

  worker:
    build:
      context: ./leadfinder-api
      dockerfile: Dockerfile
    restart: unless-stopped
    env_file:
      - .env.production
    command: ["php", "artisan", "queue:work", "--queue=scrapes,default", "--sleep=1", "--tries=3", "--timeout=120"]
    environment:
      APP_ENV: production
      APP_DEBUG: "false"
      APP_URL: https://${DOMAIN}
      FRONTEND_URL: https://${DOMAIN}
      DB_CONNECTION: pgsql
      DB_HOST: db
      DB_PORT: 5432
      DB_DATABASE: ${POSTGRES_DB}
      DB_USERNAME: ${POSTGRES_USER}
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      CACHE_DRIVER: redis
      QUEUE_CONNECTION: redis
      SESSION_DRIVER: database
      LOG_CHANNEL: stderr
      SEARCH_ENRICH_AI: ${SEARCH_ENRICH_AI}
      SEARCH_ENRICH_WEBSITE: ${SEARCH_ENRICH_WEBSITE}
      SEARCH_AGENCY_FALLBACK: ${SEARCH_AGENCY_FALLBACK}
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      OPENAI_ENRICH_MODEL: ${OPENAI_ENRICH_MODEL}
      SERPAPI_API_KEY: ${SERPAPI_API_KEY}
      BOUNCER_API_KEY: ${BOUNCER_API_KEY}
    depends_on:
      - db
      - redis

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    volumes:
      - redis_data:/data

volumes:
  caddy_data:
  caddy_config:
  postgres_data:
  redis_data:
