FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder

ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy

WORKDIR /app

# Install git for dependency installation
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    && rm -rf /var/lib/apt/lists/*

# Use Gitea PAT for private dependencies if provided
ARG GITEA_TOKEN
RUN if [ -n "$GITEA_TOKEN" ]; then \
    git config --global url."https://b3nw:${GITEA_TOKEN}@gitea.ext.ben.io/".insteadOf "https://gitea.ext.ben.io/"; \
    fi

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

# Copy the rest of the application
COPY . /app

# Install the project
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev

FROM python:3.12-slim-bookworm

WORKDIR /app

# Copy the environment from the builder
COPY --from=builder /app /app

# Set up environment variables
ENV PATH="/app/.venv/bin:$PATH"
ENV PORT=8000

# Expose the port
EXPOSE 8000

# Run the server
CMD ["python", "server.py"]
