# docker-compose.yml
services:
  db:
    image: postgres:15
    restart: always
    environment:
      POSTGRES_DB: wizamart_db
      POSTGRES_USER: wizamart_user
      POSTGRES_PASSWORD: secure_password
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U wizamart_user -d wizamart_db"]
      interval: 30s
      timeout: 10s
      retries: 3

  redis:
    image: redis:7-alpine
    restart: always
    ports:
      - "6380:6379"  # Use 6380 to avoid conflict with host Redis
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  api:
    build: .
    restart: always
    profiles:
      - full  # Only start with: docker compose --profile full up -d
    ports:
      - "8001:8000"  # Use 8001 to avoid conflict with local dev server
    environment:
      DATABASE_URL: postgresql://wizamart_user:secure_password@db:5432/wizamart_db
      JWT_SECRET_KEY: ${JWT_SECRET_KEY:-your-super-secret-key}
      REDIS_URL: redis://redis:6379/0
      USE_CELERY: "true"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./logs:/app/logs
      - ./uploads:/app/uploads
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Celery worker for processing background tasks
  celery-worker:
    build: .
    restart: always
    profiles:
      - full  # Only start with: docker compose --profile full up -d
    command: celery -A app.core.celery_config worker --loglevel=info -Q default,long_running,scheduled
    environment:
      DATABASE_URL: postgresql://wizamart_user:secure_password@db:5432/wizamart_db
      REDIS_URL: redis://redis:6379/0
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./logs:/app/logs
      - ./exports:/app/exports

  # Celery beat for scheduled tasks
  celery-beat:
    build: .
    restart: always
    profiles:
      - full  # Only start with: docker compose --profile full up -d
    command: celery -A app.core.celery_config beat --loglevel=info
    environment:
      REDIS_URL: redis://redis:6379/0
    depends_on:
      redis:
        condition: service_healthy

  # Flower monitoring dashboard
  flower:
    build: .
    restart: always
    profiles:
      - full  # Only start with: docker compose --profile full up -d
    command: celery -A app.core.celery_config flower --port=5555
    ports:
      - "5555:5555"
    environment:
      REDIS_URL: redis://redis:6379/0
      FLOWER_BASIC_AUTH: ${FLOWER_USER:-admin}:${FLOWER_PASSWORD:-changeme}
    depends_on:
      redis:
        condition: service_healthy

volumes:
  postgres_data:
