version: '3.8'
name: ai-blog-backend
services:
  # PostgreSQL Database
  db:
    image: postgres:15-alpine
    container_name: blog_writer_db
    restart: unless-stopped
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: blog_writer_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5

  # Redis (for Celery broker)
  redis:
    image: redis:7-alpine
    container_name: blog_writer_redis
    restart: unless-stopped
    volumes:
      - redis_data:/data
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 5s
      timeout: 5s
      retries: 5

  # FastAPI Application
  app:
    build:
      context: .
      dockerfile: app/Dockerfile
    container_name: blog_writer_app
    restart: unless-stopped
    ports:
      - "8009:8000"
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql+asyncpg://postgres:postgres@db:5432/blog_writer_db
      REDIS_URL: redis://redis:6379/0
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - .:/app
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload

  # Celery Worker (same image, different command)
  worker:
    build:
      context: .
      dockerfile: app/Dockerfile
    container_name: blog_writer_worker
    restart: unless-stopped
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql+asyncpg://postgres:postgres@db:5432/blog_writer_db
      REDIS_URL: redis://redis:6379/0
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - .:/app
    command: celery -A workers.celery_app worker --loglevel=info

  # Flower (Celery monitoring)
  flower:
    build:
      context: .
      dockerfile: app/Dockerfile
    container_name: blog_writer_flower
    restart: unless-stopped
    env_file:
      - .env
    environment:
      REDIS_URL: redis://redis:6379/0
      CELERY_BROKER_URL: redis://redis:6379/0
    depends_on:
      redis:
        condition: service_healthy
    command: celery -A workers.celery_app flower --port=5555

volumes:
  postgres_data:
  redis_data:
