services:
  postgres:
    image: postgres:18
    container_name: textiq-postgres
    env_file:
      - ../.env
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-textiq}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-textiq_dev_password}
      POSTGRES_DB: ${POSTGRES_DB:-textiq}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql
      - ./init-db.sh:/docker-entrypoint-initdb.d/init-db.sh
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - textiq-network

  redis:
    image: redis:7
    container_name: textiq-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 5s
    networks:
      - textiq-network

  etcd:
    image: quay.io/coreos/etcd:v3.5.25
    container_name: textiq-etcd
    environment:
      - ETCD_AUTO_COMPACTION_MODE=revision
      - ETCD_AUTO_COMPACTION_RETENTION=1000
      - ETCD_QUOTA_BACKEND_BYTES=4294967296
      - ETCD_SNAPSHOT_COUNT=50000
    volumes:
      - etcd_data:/etcd
    command: etcd -advertise-client-urls=http://etcd:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd
    healthcheck:
      test: ["CMD", "etcdctl", "endpoint", "health"]
      interval: 30s
      timeout: 20s
      retries: 3
    networks:
      - textiq-network

  milvus:
    image: milvusdb/milvus:v2.6.13
    container_name: textiq-milvus
    command: ["milvus", "run", "standalone"]
    security_opt:
      - seccomp:unconfined
    env_file:
      - ../.env
    environment:
      ETCD_ENDPOINTS: etcd:2379
      MINIO_ADDRESS: seaweedfs:8333
      MINIO_ACCESS_KEY_ID: ${SEAWEEDFS_ACCESS_KEY_ID:-dummy}
      MINIO_SECRET_ACCESS_KEY: ${SEAWEEDFS_SECRET_ACCESS_KEY:-dummy}
      MINIO_USE_SSL: "false"
      MINIO_BUCKET_NAME: milvus-bucket
      MQ_TYPE: rocksmq
    ports:
      - "19530:19530"  # gRPC
      - "9091:9091"    # HTTP
    volumes:
      - milvus_data:/var/lib/milvus
    depends_on:
      etcd:
        condition: service_healthy
      seaweedfs:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9091/healthz"]
      interval: 30s
      start_period: 90s
      timeout: 20s
      retries: 3
    networks:
      - textiq-network

  attu:
    image: zilliz/attu:v2.6
    container_name: textiq-attu
    environment:
      MILVUS_URL: milvus:19530
    ports:
      - "3000:3000"
    depends_on:
      milvus:
        condition: service_healthy
    networks:
      - textiq-network

  seaweedfs:
    image: chrislusf/seaweedfs:latest
    container_name: textiq-seaweedfs
    entrypoint: /bin/sh
    command:
      - -c
      - |
        cat > /tmp/s3.json <<EOF
        {"identities":[{"name":"admin","credentials":[{"accessKey":"$$SEAWEEDFS_ACCESS_KEY_ID","secretKey":"$$SEAWEEDFS_SECRET_ACCESS_KEY"}],"actions":["Admin","Read","List","Tagging","Write"]}]}
        EOF
        exec weed server -s3 -s3.config=/tmp/s3.json -filer -dir=/data -master.volumeSizeLimitMB=100 -volume.max=0
    environment:
      SEAWEEDFS_ACCESS_KEY_ID: ${SEAWEEDFS_ACCESS_KEY_ID:-dummy}
      SEAWEEDFS_SECRET_ACCESS_KEY: ${SEAWEEDFS_SECRET_ACCESS_KEY:-dummy}
    ports:
      - "8333:8333"  # S3 API
      - "9333:9333"  # Master
      - "8080:8080"  # Volume server
      - "8888:8888"  # Filer (HTTP API for file operations)
    volumes:
      - seaweedfs_data:/data
    healthcheck:
      # Check master endpoint (S3 port requires auth now)
      test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1:9333/"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    networks:
      - textiq-network

networks:
  textiq-network:
    driver: bridge

volumes:
  postgres_data:
    name: textiq_postgres_data
  redis_data:
    name: textiq_redis_data
  etcd_data:
    name: textiq_etcd_data
  milvus_data:
    name: textiq_milvus_data
  seaweedfs_data:
    name: textiq_seaweedfs_data
