FROM python:3.11-slim

# Set working directory
WORKDIR /app

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

# Copy requirements and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Create necessary directories
RUN mkdir -p /app/data /app/logs

# Copy application files
COPY proxy_bot.py .
COPY channels.txt .

# Copy session files if they exist (optional)
COPY proxy_bot.session* ./
COPY proxy_reader.session* ./

# Copy scripts
COPY start.sh monitor.sh ./

# Make scripts executable
RUN chmod +x start.sh monitor.sh

# Set environment variables
ENV CACHE_FILE=/app/data/proxy_cache.json
ENV PYTHONUNBUFFERED=1

# Create non-root user for security
#RUN groupadd -r botuser && useradd -r -g botuser botuser
#RUN chown -R botuser:botuser /app
#USER botuser

# Health check - check if cache file exists and process is running
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD python3 -c "import os, psutil; exit(0 if os.path.exists('/app/data/proxy_cache.json') else 1)"

# Run the bot
CMD ["python3", "proxy_bot.py"]
