version: '3.8'

services:
  # FastAPI App - Development
  app-dev:
    build: .
    container_name: trading_app_dev
    profiles: ["dev"]
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app/app
      - ./logs:/app/logs
    env_file: .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    environment:
      ENVIRONMENT: development
      DEBUG: "True"
      POSTGRES_HOST: postgres
      REDIS_HOST: redis
      CELERY_BROKER_URL: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672//
      CELERY_RESULT_BACKEND: redis://:${REDIS_PASSWORD}@redis:6379/0
      LUCKYST_NETWORK: ${LUCKYST_NETWORK:-testnet}

  # FastAPI App - Production
  app-prod:
    build: .
    container_name: trading_app_prod
    profiles: ["prod"]
    command: gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --access-logfile - --error-logfile -
    ports:
      - "127.0.0.1:8000:8000"
    volumes:
      - ./logs:/app/logs
    env_file: .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    environment:
      ENVIRONMENT: production
      DEBUG: "False"
      POSTGRES_HOST: postgres
      REDIS_HOST: redis
      CELERY_BROKER_URL: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672//
      CELERY_RESULT_BACKEND: redis://:${REDIS_PASSWORD}@redis:6379/0
      LUCKYST_NETWORK: ${LUCKYST_NETWORK:-mainnet}
    restart: unless-stopped

  postgres:
    image: postgres:15-alpine
    container_name: trading_postgres
    profiles: ["dev", "prod"]
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "127.0.0.1:5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: trading_pgadmin
    profiles: ["dev"]
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
    ports:
      - "127.0.0.1:5050:80"
    depends_on:
      - postgres

  redis:
    image: redis:7-alpine
    container_name: trading_redis
    profiles: ["dev", "prod"]
    ports:
      - "127.0.0.1:6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD}
    healthcheck:
      test: ["CMD", "redis-cli", "--auth", "${REDIS_PASSWORD}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis-commander:
    image: rediscommander/redis-commander:latest
    container_name: trading_redis_commander
    profiles: ["dev"]
    environment:
      REDIS_HOSTS: local:redis:6379:0:${REDIS_PASSWORD}
      HTTP_USER: ${REDIS_COMMANDER_USER}
      HTTP_PASSWORD: ${REDIS_COMMANDER_PASSWORD}
    ports:
      - "127.0.0.1:8081:8081"
    depends_on:
      - redis

  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: trading_rabbitmq
    profiles: ["dev", "prod"]
    ports:
      - "127.0.0.1:5672:5672"
      - "127.0.0.1:15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD}
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  celery-worker:
    build: .
    container_name: trading_celery
    profiles: ["dev", "prod"]
    command: celery -A app.tasks.trading_tasks worker --loglevel=info
    volumes:
      - ./app:/app/app
      - ./logs:/app/logs
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    env_file:
      - .env
    environment:
      POSTGRES_HOST: postgres
      REDIS_HOST: redis
      CELERY_BROKER_URL: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672//
      CELERY_RESULT_BACKEND: redis://:${REDIS_PASSWORD}@redis:6379/0
      LUCKYST_NETWORK: ${LUCKYST_NETWORK:-testnet}

  flower:
    build: .
    container_name: trading_flower
    profiles: ["dev"]
    command: celery -A app.celery_config.celery_app flower
    ports:
      - "127.0.0.1:5555:5555"
    depends_on:
      - redis
      - rabbitmq
    env_file: .env
    environment:
      CELERY_BROKER_URL: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672//
      CELERY_RESULT_BACKEND: redis://:${REDIS_PASSWORD}@redis:6379/0

volumes:
  postgres_data:
  redis_data: