# Use official Microsoft Playwright image with Python & browser dependencies
FROM mcr.microsoft.com/playwright/python:v1.49.0-noble

# Prevent interactive prompts during apt package installation
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1

# Install Tor daemon and curl
RUN apt-get update && apt-get install -y --no-install-recommends \
    tor \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Configure Tor (SOCKS5 proxy on 9050, ControlPort on 9051 with open localhost authentication)
RUN printf "SocksPort 127.0.0.1:9050\nControlPort 127.0.0.1:9051\nCookieAuthentication 0\nHashedControlPassword \"\"\n" > /etc/tor/torrc

WORKDIR /app

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

# Ensure Chromium browser is ready
RUN playwright install chromium

# Copy application source code
COPY . .

# Prepare entrypoint script (strip potential Windows CRLF endings and mark executable)
COPY entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r$//' /entrypoint.sh && chmod +x /entrypoint.sh

# Expose Web Dashboard port
EXPOSE 8000

ENTRYPOINT ["/entrypoint.sh"]
CMD ["python", "web.py", "--host", "0.0.0.0", "--port", "8000"]
