# Step 1: Use an official lightweight Python image
FROM python:3.11-slim

# Step 2: Install required system utilities
RUN apt-get update && apt-get install -y \
    wget \
    tar \
    git \
    && rm -rf /var/lib/apt/lists/*

# Step 3: Set up the working directory inside the container
WORKDIR /app

# Step 4: Download and extract Stockfish 18 safely
RUN mkdir -p engines && \
    wget https://github.com/official-stockfish/Stockfish/releases/download/sf_18/stockfish-ubuntu-x86-64-avx2.tar && \
    tar -xf stockfish-ubuntu-x86-64-avx2.tar -C engines && \
    rm -rf stockfish-ubuntu-x86-64-avx2.tar

# Step 5: Copy all your repository files into the container
COPY . /app

# Step 5.5: Fix permissions for the fairy stockfish engine
RUN chmod +x /app/fairy/stockfish

# Step 6: Install the Python requirements
RUN pip install --no-cache-dir -r requirements.txt

# Step 6.5: Inform Hugging Face that we are opening port 7860
EXPOSE 7860

# Step 7: Start a dummy web server on port 7860 in the background, then launch your Lichess bot
CMD python -m http.server 7860 & python lichess-bot-master/lichess-bot.py
RUN chmod +x ./fairy/stockfish.py