version: "3.9"

services:
  # PostgreSQL 16
  postgres:
    image: postgres:16-alpine
    container_name: omnicommerce-db
    environment:
      POSTGRES_DB: omnicommerce
      POSTGRES_USER: omni
      POSTGRES_PASSWORD: omni_secret_dev
      POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=en_US.UTF-8"
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U omni -d omnicommerce"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis 7
  redis:
    image: redis:7-alpine
    container_name: omnicommerce-cache
    command: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      retries: 5

  # Elasticsearch
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.12.0
    container_name: omnicommerce-search
    environment:
      - node.name=es01
      - cluster.name=omnicommerce
      - discovery.type=single-node
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - xpack.security.enabled=false
      - xpack.security.http.ssl.enabled=false
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - es_data:/usr/share/elasticsearch/data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9200/_cluster/health"]
      interval: 30s
      retries: 10

  # Zookeeper (for Kafka)
  zookeeper:
    image: confluentinc/cp-zookeeper:7.5.0
    container_name: omnicommerce-zk
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - "2181:2181"

  # Kafka
  kafka:
    image: confluentinc/cp-kafka:7.5.0
    container_name: omnicommerce-kafka
    depends_on:
      zookeeper:
        condition: service_started
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
    ports:
      - "9092:9092"
    healthcheck:
      test: ["CMD", "kafka-broker-api-versions", "--bootstrap-server", "localhost:9092"]
      interval: 30s
      retries: 5

  # MinIO (S3-compatible storage)
  minio:
    image: minio/minio:latest
    container_name: omnicommerce-storage
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin123
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      retries: 3

  # Backend API (Go)
  backend:
    build:
      context: ../../backend
      dockerfile: Dockerfile
    container_name: omnicommerce-api
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      DATABASE_URL: postgres://omni:omni_secret_dev@postgres:5432/omnicommerce?sslmode=disable
      REDIS_URL: redis://redis:6379
      KAFKA_BROKERS: kafka:9092
      ELASTICSEARCH_URL: http://elasticsearch:9200
      MINIO_ENDPOINT: minio:9000
      JWT_SECRET: dev-secret-change-in-production
    ports:
      - "8080:8080"
    restart: unless-stopped

  # Frontend Web App (Next.js)
  web:
    build:
      context: ../../frontend/web
      dockerfile: Dockerfile
    container_name: omnicommerce-web
    depends_on:
      - backend
    environment:
      NEXT_PUBLIC_API_URL: http://localhost:8080/api/v1
    ports:
      - "3000:3000"
    restart: unless-stopped

  # Admin Panel (Next.js)
  admin:
    build:
      context: ../../frontend/admin
      dockerfile: Dockerfile
    container_name: omnicommerce-admin
    depends_on:
      - backend
    environment:
      NEXT_PUBLIC_API_URL: http://localhost:8080/api/v1
    ports:
      - "3001:3001"
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:
  es_data:
  minio_data:
