volumes:
  postgres_data:
  rustfs_data:
  redis_data:

services:
  db:
    image: postgres:18.1-trixie
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: intentkit
    volumes:
      - postgres_data:/var/lib/postgresql
    ports:
      - "15432:5432"
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

  rustfs:
    image: rustfs/rustfs:latest
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      - RUSTFS_ACCESS_KEY=minioadmin
      - RUSTFS_SECRET_KEY=minioadmin
      - RUSTFS_CONSOLE_ENABLE=true
    volumes:
      - rustfs_data:/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/health"]
      interval: 30s
      timeout: 20s
      retries: 3

  redis:
    image: redis:8.4-bookworm
    volumes:
      - redis_data:/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  api:
    build:
      context: .
      args:
        - RELEASE=${RELEASE:-latest}
    volumes:
      - ./.env:/app/.env
      - ./intentkit:/app/intentkit
      - ./app:/app/app
      - ./public_agents:/app/public_agents
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      - DB_USERNAME=postgres
      - DB_PASSWORD=postgres
      - DB_HOST=db
      - DB_PORT=5432
      - DB_NAME=intentkit
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_DB=0
      - AWS_S3_CDN_URL=http://localhost:9000/static
      - AWS_S3_ENDPOINT_URL=http://rustfs:9000
      - AWS_S3_ACCESS_KEY_ID=minioadmin
      - AWS_S3_SECRET_ACCESS_KEY=minioadmin
      - AWS_S3_BUCKET=static
      - AWS_S3_REGION_NAME=us-east-1
    ports:
      - "8000:8000"
    healthcheck:
      test:
        [
          "CMD",
          "python",
          "-c",
          "import requests; requests.get('http://localhost:8000/health').raise_for_status()",
        ]
      interval: 30s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    command: uvicorn app.api:app --host 0.0.0.0 --port 8000 --reload

  autonomous:
    build:
      context: .
      args:
        - RELEASE=${RELEASE:-latest}
    volumes:
      - ./.env:/app/.env
      - ./intentkit:/app/intentkit
      - ./app:/app/app
      - ./public_agents:/app/public_agents
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      api:
        condition: service_healthy
    environment:
      - DB_USERNAME=postgres
      - DB_PASSWORD=postgres
      - DB_HOST=db
      - DB_PORT=5432
      - DB_NAME=intentkit
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_DB=0
      - AWS_S3_CDN_URL=http://localhost:9000/static
      - AWS_S3_ENDPOINT_URL=http://rustfs:9000
      - AWS_S3_ACCESS_KEY_ID=minioadmin
      - AWS_S3_SECRET_ACCESS_KEY=minioadmin
      - AWS_S3_BUCKET=static
      - AWS_S3_REGION_NAME=us-east-1
      - INTERNAL_BASE_URL=http://api:8000
    healthcheck:
      test: ["CMD", "python", "scripts/check_heartbeat.py", "autonomous"]
      interval: 60s
      timeout: 5s
      retries: 3
    restart: unless-stopped
    command: python -m app.autonomous

  scheduler:
    build:
      context: .
      args:
        - RELEASE=${RELEASE:-latest}
    volumes:
      - ./.env:/app/.env
      - ./intentkit:/app/intentkit
      - ./app:/app/app
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      - DB_USERNAME=postgres
      - DB_PASSWORD=postgres
      - DB_HOST=db
      - DB_PORT=5432
      - DB_NAME=intentkit
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_DB=0
      - AWS_S3_CDN_URL=http://localhost:9000/static
      - AWS_S3_ENDPOINT_URL=http://rustfs:9000
      - AWS_S3_ACCESS_KEY_ID=minioadmin
      - AWS_S3_SECRET_ACCESS_KEY=minioadmin
      - AWS_S3_BUCKET=static
      - AWS_S3_REGION_NAME=us-east-1
    healthcheck:
      test: ["CMD", "python", "scripts/check_heartbeat.py", "scheduler"]
      interval: 60s
      timeout: 5s
      retries: 3
    restart: unless-stopped
    command: python -m app.scheduler

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.dev
    volumes:
      - ./frontend/src:/app/src
      - ./frontend/public:/app/public
      - ./frontend/tailwind.config.ts:/app/tailwind.config.ts
      - ./frontend/postcss.config.mjs:/app/postcss.config.mjs
      - ./frontend/next.config.ts:/app/next.config.ts
      - ./frontend/tsconfig.json:/app/tsconfig.json
      - ./frontend/components.json:/app/components.json
    depends_on:
      api:
        condition: service_healthy
    environment:
      - NEXT_PUBLIC_API_BASE_URL=http://localhost:8000
      - NEXT_PUBLIC_AWS_S3_CDN_URL=http://localhost:9000/static
      - NODE_ENV=development
      - NODE_OPTIONS=--max-old-space-size=2048
      - WATCHPACK_POLLING=1000
    ports:
      - "3000:3000"
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost:3000"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 10s
    restart: unless-stopped

  telegram:
    build:
      context: ./integrations
      dockerfile: Dockerfile.dev
      args:
        SERVICE: telegram
    volumes:
      - ./integrations:/app
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      api:
        condition: service_healthy
    env_file:
      - .env
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USERNAME=postgres
      - DB_PASSWORD=postgres
      - DB_NAME=intentkit
      - REDIS_HOST=redis
      - INTERNAL_BASE_URL=http://api:8000
      - TG_NEW_AGENT_POLL_INTERVAL=10
    healthcheck:
      test: ["CMD", "find", "/tmp/healthy", "-mmin", "-2"]
      interval: 30s
      timeout: 5s
      retries: 3
    restart: unless-stopped

  wechat:
    build:
      context: ./integrations
      dockerfile: Dockerfile.dev
      args:
        SERVICE: wechat
    volumes:
      - ./integrations:/app
    depends_on:
      db:
        condition: service_healthy
      api:
        condition: service_healthy
    env_file:
      - .env
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USERNAME=postgres
      - DB_PASSWORD=postgres
      - DB_NAME=intentkit
      - INTERNAL_BASE_URL=http://api:8000
      - WX_NEW_CHANNEL_POLL_INTERVAL=10
    healthcheck:
      test: ["CMD", "find", "/tmp/healthy", "-mmin", "-2"]
      interval: 30s
      timeout: 5s
      retries: 3
    restart: unless-stopped
