services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: mockupaws-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mockupaws
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - mockupaws-network

  # Redis Cache & Message Broker
  redis:
    image: redis:7-alpine
    container_name: mockupaws-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf:ro
    command: redis-server /usr/local/etc/redis/redis.conf
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
    networks:
      - mockupaws-network

  # Celery Worker
  celery-worker:
    build:
      context: .
      dockerfile: Dockerfile.backend
    container_name: mockupaws-celery-worker
    restart: unless-stopped
    command: uv run celery -A src.core.celery_app worker --loglevel=info --concurrency=4
    environment:
      DATABASE_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws
      REDIS_URL: redis://redis:6379/0
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./storage:/app/storage
    networks:
      - mockupaws-network

  # Celery Beat (Scheduler)
  celery-beat:
    build:
      context: .
      dockerfile: Dockerfile.backend
    container_name: mockupaws-celery-beat
    restart: unless-stopped
    command: uv run celery -A src.core.celery_app beat --loglevel=info
    environment:
      DATABASE_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws
      REDIS_URL: redis://redis:6379/0
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - celery_data:/app/celery
    networks:
      - mockupaws-network

  # Flower (Celery Monitoring)
  flower:
    build:
      context: .
      dockerfile: Dockerfile.backend
    container_name: mockupaws-flower
    restart: unless-stopped
    command: uv run celery -A src.core.celery_app flower --port=5555 --url_prefix=flower
    environment:
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
    ports:
      - "5555:5555"
    depends_on:
      - celery-worker
      - redis
    networks:
      - mockupaws-network

  # Backend API (Production)
  backend:
    build:
      context: .
      dockerfile: Dockerfile.backend
    container_name: mockupaws-backend
    restart: unless-stopped
    environment:
      DATABASE_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws
      REDIS_URL: redis://redis:6379/0
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
      APP_VERSION: "1.0.0"
      DEBUG: "false"
      LOG_LEVEL: "INFO"
      JSON_LOGGING: "true"
      AUDIT_LOGGING_ENABLED: "true"
    ports:
      - "8000:8000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./storage:/app/storage
      - ./logs:/app/logs
    networks:
      - mockupaws-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Frontend React (Production)
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: mockupaws-frontend
    restart: unless-stopped
    environment:
      VITE_API_URL: http://localhost:8000
    ports:
      - "8888:80"
    depends_on:
      - backend
    networks:
      - mockupaws-network

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  celery_data:
    driver: local

networks:
  mockupaws-network:
    driver: bridge
