# Use Python 3.11 slim image for smaller size
FROM python:3.11-slim

# Set maintainer
LABEL maintainer="fuh@fuhlig.de"
LABEL description="PY_ChatBot - Modern Flask-based Chatbot Application"

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    FLASK_APP=main.py \
    FLASK_ENV=production \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

# Set work directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    sqlite3 \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user for security
RUN addgroup --system --gid 1001 chatbot && \
    adduser --system --uid 1001 --gid 1001 --no-create-home --disabled-password chatbot

# Create necessary directories with proper permissions
RUN mkdir -p /app/data /app/logs /app/static && \
    chown -R chatbot:chatbot /app

# Copy requirements first for better Docker layer caching
COPY requirements.txt .
RUN chown chatbot:chatbot requirements.txt

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .
RUN chown -R chatbot:chatbot .

# Copy entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh && \
    chown chatbot:chatbot /usr/local/bin/docker-entrypoint.sh

# Switch to non-root user
USER chatbot

# Expose port
EXPOSE 8080

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD curl -f http://localhost:8080/ || exit 1

# Set entrypoint
ENTRYPOINT ["docker-entrypoint.sh"]

# Default command
CMD ["python", "main.py"]
