version: '3.8'

services:
  web:
    build: .
    container_name: hwPaymentApi
    ports:
      - "${APP_PORT}:${FAST_API_PORT}"
    env_file:
      - .env
    depends_on:
      db:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - .:/app
      - uploads_data:/app/uploads
    networks:
      - fastapi_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:${FAST_API_PORT}/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  db:
    image: postgres:15-alpine
    container_name: fastapi_db
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
    ports:
      - "${DB_PORT}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - fastapi_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-fastapi_db}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # RabbitMQ - Message Broker for Celery
  rabbitmq:
    image: rabbitmq:3.12-management-alpine
    container_name: hwPaymentRabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD}
      RABBITMQ_DEFAULT_VHOST: /
    ports:
      - "5673:5672"    # AMQP port
      - "15673:15672"  # Management UI port
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    networks:
      - fastapi_network
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis - Result Backend and Cache for Celery
  redis:
    image: redis:7-alpine
    container_name: hwPaymentRedis
    ports:
      - "6380:6379"
    volumes:
      - redis_data:/data
    networks:
      - fastapi_network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    command: redis-server --appendonly yes

  # Celery Worker - Background Task Processing
  celery_worker:
    build: .
    container_name: hwPaymentCeleryWorker
    command: celery -A src.worker.celery_app:celery_app worker --loglevel=info
    env_file:
      - .env
    depends_on:
      db:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - .:/app
      - uploads_data:/app/uploads
    networks:
      - fastapi_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "celery", "-A", "src.worker.celery_app:celery_app", "inspect", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Uncomment to add pgAdmin for database management
  # pgadmin:
  #   image: dpage/pgadmin4:latest
  #   environment:
  #     PGADMIN_DEFAULT_EMAIL: admin@example.com
  #     PGADMIN_DEFAULT_PASSWORD: admin
  #   ports:
  #     - "5050:80"
  #   depends_on:
  #     - db
  #   networks:
  #     - fastapi_network

volumes:
  postgres_data:
    driver: local
  uploads_data:
    driver: local
  rabbitmq_data:
    driver: local
  redis_data:
    driver: local

networks:
  fastapi_network:
    driver: bridge
