volumes:
  database:

services:
  database:
    image: postgres:latest
    volumes:
      - database:/var/lib/postgresql/data:z
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-kycnot}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-kycnot}
      POSTGRES_DB: ${POSTGRES_DATABASE:-kycnot}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-kycnot} -d ${POSTGRES_DATABASE:-kycnot}"]
      interval: 10s
      timeout: 5s
      retries: 5

  pyworker:
    build:
      context: ./pyworker
    restart: always
    environment:
      DATABASE_URL: "postgresql://${POSTGRES_USER:-kycnot}:${POSTGRES_PASSWORD:-kycnot}@database:5432/${POSTGRES_DATABASE:-kycnot}?schema=public"
      CRAWL4AI_BASE_URL: "http://crawl4ai:11235"
      CRAWL4AI_API_TOKEN: ${CRAWL4AI_API_TOKEN:-testing}

  crawl4ai:
    image: unclecode/crawl4ai:basic-amd64
    expose:
      - "11235"
    environment:
      CRAWL4AI_API_TOKEN: ${CRAWL4AI_API_TOKEN:-testing} # Optional API security
      MAX_CONCURRENT_TASKS: 10
    volumes:
      - /dev/shm:/dev/shm
    deploy:
      resources:
        limits:
          memory: 4G
        reservations:
          memory: 1G

  redis:
    image: redis:latest
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  astro:
    build:
      context: ./web
    image: kycnotme/astro:${ASTRO_IMAGE_TAG:-latest}
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-kycnot}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-kycnot}
      POSTGRES_DB: ${POSTGRES_DATABASE:-kycnot}
      DATABASE_URL: "postgresql://${POSTGRES_USER:-kycnot}:${POSTGRES_PASSWORD:-kycnot}@database:5432/${POSTGRES_DATABASE:-kycnot}?schema=public"
      REDIS_URL: "redis://redis:6379"
    depends_on:
      database:
        condition: service_healthy
      redis:
        condition: service_healthy
    expose:
      - 4321
    healthcheck:
      test: ["CMD", "curl", "-k", "--silent", "--fail", "http://localhost:4321/health"]
      interval: 10s
      timeout: 5s
      retries: 5
