FROM aiogram/telegram-bot-api:latest AS telegram-bot-api

FROM python:3.12-slim

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    postgresql-client \
    curl \
    ffmpeg \
    git \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /usr/local/share/ca-certificates/russian-trusted \
    && curl -fsSLk https://gu-st.ru/content/lending/russian_trusted_root_ca_pem.crt \
        -o /usr/local/share/ca-certificates/russian-trusted/russian_trusted_root_ca_pem.crt \
    && curl -fsSLk https://gu-st.ru/content/lending/russian_trusted_sub_ca_pem.crt \
        -o /usr/local/share/ca-certificates/russian-trusted/russian_trusted_sub_ca_pem.crt \
    && update-ca-certificates

# Copy the local Telegram Bot API binary and its musl runtime from the official aiogram image.
COPY --from=telegram-bot-api /usr/local/bin/telegram-bot-api /usr/local/bin/telegram-bot-api
COPY --from=telegram-bot-api /lib/ld-musl-x86_64.so.1 /lib/ld-musl-x86_64.so.1
COPY --from=telegram-bot-api /usr/lib/libssl.so.3 /usr/lib/libssl.so.3
COPY --from=telegram-bot-api /usr/lib/libcrypto.so.3 /usr/lib/libcrypto.so.3
COPY --from=telegram-bot-api /usr/lib/libz.so.1 /usr/lib/libz.so.1
COPY --from=telegram-bot-api /usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6
COPY --from=telegram-bot-api /usr/lib/libgcc_s.so.1 /usr/lib/libgcc_s.so.1

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

# Copy source code
COPY src/ src/
COPY scripts/ scripts/
COPY docker-entrypoint.sh /usr/local/bin/vk-parser-entrypoint
RUN chmod +x /usr/local/bin/vk-parser-entrypoint \
    && mkdir -p /var/lib/telegram-bot-api /tmp/telegram-bot-api

COPY favi.png .
COPY .env.example .env

ENV PYTHONPATH=/app/src
ENV PYTHONUNBUFFERED=1
ENV LOCAL_BOT_API_URL=http://127.0.0.1:8081
ENV TELEGRAM_WORK_DIR=/var/lib/telegram-bot-api
ENV TELEGRAM_TEMP_DIR=/tmp/telegram-bot-api
ENV TELEGRAM_HTTP_PORT=8081
ENV TELEGRAM_LOCAL=1

EXPOSE 8000

# Start uvicorn without any workers
ENTRYPOINT ["vk-parser-entrypoint"]
CMD ["uvicorn", "vk_parser_app.admin:app", "--host", "0.0.0.0", "--port", "8000"]
