# Builder Stage
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim

# Install necessary dependencies for ECS health checks
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl libgl1 libglib2.0-0 gifsicle \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

ENV UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON_DOWNLOADS=0

# Install the dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --locked --no-install-project --no-dev

# Then, add the rest of the project source code and install it
# Installing separately from its dependencies allows optimal layer caching
COPY ./src /app/src

RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --locked --no-dev

COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh

ENV PATH="/app/.venv/bin:$PATH"
ENV UVICORN_PORT=8080

# Expose the application port
EXPOSE 8080

ENTRYPOINT  ["/docker-entrypoint.sh"]
