services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: ${BASKIT_MYSQL_PASSWORD:-baskit}
      MYSQL_DATABASE: ${BASKIT_MYSQL_DB:-baskit}
    ports:
      # Host 3307 avoids clashing with WAMP MySQL on 3306
      - "${BASKIT_MYSQL_PUBLISH_PORT:-3307}:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$$MYSQL_ROOT_PASSWORD || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  worker:
    build: .
    environment:
      BASKIT_DATA_DIR: ${BASKIT_DATA_DIR:-data}
      BASKIT_EXPORTS_DIR: ${BASKIT_EXPORTS_DIR:-exports}
      BASKIT_PNP_DB: ${BASKIT_PNP_DB:-data/pnp.db}
      BASKIT_CHECKERS_DB: ${BASKIT_CHECKERS_DB:-data/checkers.db}
      BASKIT_WOOLWORTHS_DB: ${BASKIT_WOOLWORTHS_DB:-data/woolworths.db}
      BASKIT_DB: ${BASKIT_DB:-data/baskit.db}
      BASKIT_MYSQL_HOST: ${BASKIT_MYSQL_HOST:-mysql}
      BASKIT_MYSQL_PORT: ${BASKIT_MYSQL_PORT:-3306}
      BASKIT_MYSQL_USER: ${BASKIT_MYSQL_USER:-root}
      BASKIT_MYSQL_PASSWORD: ${BASKIT_MYSQL_PASSWORD:-baskit}
      BASKIT_MYSQL_DB: ${BASKIT_MYSQL_DB:-baskit}
      BASKIT_SCRAPE_DELAY: ${BASKIT_SCRAPE_DELAY:-0.6}
      BASKIT_CHECKERS_DELAY: ${BASKIT_CHECKERS_DELAY:-0.6}
      BASKIT_WOOLWORTHS_DELAY: ${BASKIT_WOOLWORTHS_DELAY:-0.5}
      BASKIT_PNP_DELAY: ${BASKIT_PNP_DELAY:-1.0}
      BASKIT_PER_TERM_CAP: ${BASKIT_PER_TERM_CAP:-100}
      BASKIT_HEADLESS: ${BASKIT_HEADLESS:-true}
      BASKIT_RETAILERS: ${BASKIT_RETAILERS:-checkers,woolworths}
      BASKIT_REFRESH: ${BASKIT_REFRESH:-true}
      BASKIT_PNP_REFRESH: ${BASKIT_PNP_REFRESH:-false}
      BASKIT_RUN_MYSQL_LOAD: ${BASKIT_RUN_MYSQL_LOAD:-true}
      BASKIT_PIPELINE_INTERVAL_HOURS: ${BASKIT_PIPELINE_INTERVAL_HOURS:-1}
      BASKIT_ROTATE_COOLDOWN_SEC: ${BASKIT_ROTATE_COOLDOWN_SEC:-60}
      BASKIT_ROTATE_MAX_PARALLEL: ${BASKIT_ROTATE_MAX_PARALLEL:-2}
      BASKIT_ROTATE_START_AFTER_SEC: ${BASKIT_ROTATE_START_AFTER_SEC:-2700}
      BASKIT_COMPARE_MIN_RETAILERS: ${BASKIT_COMPARE_MIN_RETAILERS:-2}
      # Baskit Ops ingestion API (v2 sink) — set OPS_SERVICE_KEY in Dokploy env
      OPS_INGEST_BASE_URL: ${OPS_INGEST_BASE_URL:-https://baskitops.appnotify.co.za}
      OPS_SERVICE_KEY: ${OPS_SERVICE_KEY:-}
      INGEST_SERVICE_KEY: ${INGEST_SERVICE_KEY:-}
      OPS_VERIFY_TLS: ${OPS_VERIFY_TLS:-true}
      OPS_BATCH_SIZE: ${OPS_BATCH_SIZE:-200}
      OPS_TIMEOUT: ${OPS_TIMEOUT:-120}
      PIPELINE_ALERT_WEBHOOK_URL: ${PIPELINE_ALERT_WEBHOOK_URL:-https://cursor.testworks.co.za/api/webhook.php}
      PYTHONUNBUFFERED: "1"
    volumes:
      - ./data:/app/data
      - ./exports:/app/exports
    depends_on:
      mysql:
        condition: service_healthy
    restart: unless-stopped
    extra_hosts:
      # Optional: reach WAMP MySQL on the Windows host instead of bundled mysql
      - "host.docker.internal:host-gateway"

  viewer:
    build:
      context: .
      dockerfile: Dockerfile.viewer
    ports:
      - "${BASKIT_VIEW_PORT:-8088}:8088"
    environment:
      BASKIT_MYSQL_HOST: ${BASKIT_MYSQL_HOST:-mysql}
      BASKIT_MYSQL_PORT: ${BASKIT_MYSQL_PORT:-3306}
      BASKIT_MYSQL_USER: ${BASKIT_MYSQL_USER:-root}
      BASKIT_MYSQL_PASSWORD: ${BASKIT_MYSQL_PASSWORD:-baskit}
      BASKIT_MYSQL_DB: ${BASKIT_MYSQL_DB:-baskit}
      BASKIT_VIEW_USER: ${BASKIT_VIEW_USER:-baskit}
      BASKIT_VIEW_PASSWORD: ${BASKIT_VIEW_PASSWORD:-baskit2026}
      BASKIT_VIEW_PORT: "8088"
    restart: unless-stopped

volumes:
  mysql_data:
