services:
  # PostgreSQL Database
  db:
    image: postgres:17-alpine
    container_name: chatbot-db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: chatbot_db
      POSTGRES_USER: chatbot_user
      POSTGRES_PASSWORD: chatbot_pass
    ports:
      - "5433:5432"  # Host:Container - avoiding conflict with local PostgreSQL
    networks:
      - chatbot-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U chatbot_user -d chatbot_db"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Django Backend
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: chatbot-backend
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./backend:/app
      - static_volume:/app/staticfiles
      - media_volume:/app/media
    ports:
      - "8000:8000"
    environment:
      - DEBUG=1
      - DJANGO_SETTINGS_MODULE=config.settings.development
      - SECRET_KEY=dev-secret-key-change-in-production
      - DATABASE_URL=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - PGVECTOR_CONNECTION_STRING=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - PG_CHECKPOINT_URI=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - DB_NAME=chatbot_db
      - DB_USER=chatbot_user
      - DB_PASSWORD=chatbot_pass
      - DB_HOST=db
      - DB_PORT=5432
      - ALLOWED_HOSTS=localhost,127.0.0.1,backend
      - CORS_ALLOWED_ORIGINS=http://localhost:3000,http://frontend:3000
      - OPENAI_API_KEY=${OPENAI_API_KEY:-your-openai-api-key-here}
      - TAVILY_API_KEY=${TAVILY_API_KEY:-}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - chatbot-network
    develop:
      watch:
        - action: sync
          path: ./backend
          target: /app
          ignore:
            - __pycache__/
            - "*.pyc"
            - "*.pyo"
            - "*.pyd"
            - .pytest_cache/
            - .git/
        - action: sync+restart
          path: ./backend/requirements.txt
          target: /app/requirements.txt

  # Next.js Frontend
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: chatbot-frontend
    volumes:
      - ./frontend:/app
      - /app/node_modules
      - /app/.next
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=development
      - NEXT_PUBLIC_API_URL=http://localhost:8000
      - WATCHPACK_POLLING=true
    depends_on:
      - backend
    networks:
      - chatbot-network
    develop:
      watch:
        - action: sync
          path: ./frontend
          target: /app
          ignore:
            - node_modules/
            - .next/
            - .git/
        - action: rebuild
          path: ./frontend/package.json
          target: /app/package.json

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: chatbot-redis
    ports:
      - "6380:6379"  # Host:Container - avoiding conflict with local Redis
    volumes:
      - redis_data:/data
    networks:
      - chatbot-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Celery Worker
  celery:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: chatbot-celery
    entrypoint: ["/app/entrypoint-celery.sh"]
    command: celery -A config worker --loglevel=info
    volumes:
      - ./backend:/app
    environment:
      - DEBUG=1
      - DJANGO_SETTINGS_MODULE=config.settings.development
      - SECRET_KEY=dev-secret-key-change-in-production
      - DATABASE_URL=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - PGVECTOR_CONNECTION_STRING=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - PG_CHECKPOINT_URI=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - DB_NAME=chatbot_db
      - DB_USER=chatbot_user
      - DB_PASSWORD=chatbot_pass
      - DB_HOST=db
      - DB_PORT=5432
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
      - OPENAI_API_KEY=${OPENAI_API_KEY:-your-openai-api-key-here}
      - TAVILY_API_KEY=${TAVILY_API_KEY:-}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      backend:
        condition: service_started
    networks:
      - chatbot-network
    develop:
      watch:
        - action: sync+restart
          path: ./backend
          target: /app
          ignore:
            - __pycache__/
            - "*.pyc"
            - "*.pyo"
            - "*.pyd"
            - .pytest_cache/
            - .git/

  # Celery Beat (Scheduler)
  celery-beat:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: chatbot-celery-beat
    entrypoint: ["/app/entrypoint-celery.sh"]
    command: celery -A config beat --loglevel=info
    volumes:
      - ./backend:/app
    environment:
      - DEBUG=1
      - DJANGO_SETTINGS_MODULE=config.settings.development
      - SECRET_KEY=dev-secret-key-change-in-production
      - DATABASE_URL=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - PGVECTOR_CONNECTION_STRING=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - PG_CHECKPOINT_URI=postgresql://chatbot_user:chatbot_pass@db:5432/chatbot_db
      - DB_NAME=chatbot_db
      - DB_USER=chatbot_user
      - DB_PASSWORD=chatbot_pass
      - DB_HOST=db
      - DB_PORT=5432
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
      - OPENAI_API_KEY=${OPENAI_API_KEY:-your-openai-api-key-here}
      - TAVILY_API_KEY=${TAVILY_API_KEY:-}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      backend:
        condition: service_started
    networks:
      - chatbot-network
    develop:
      watch:
        - action: sync+restart
          path: ./backend
          target: /app
          ignore:
            - __pycache__/
            - "*.pyc"
            - "*.pyo"
            - "*.pyd"
            - .pytest_cache/
            - .git/

networks:
  chatbot-network:
    driver: bridge

volumes:
  postgres_data:
  redis_data:
  static_volume:
  media_volume:
