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

ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy

WORKDIR /app

# Enable SSH for git dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    ssh-client \
    && rm -rf /var/lib/apt/lists/*

# Add gitea.ext.ben.io to known hosts
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan git.local.ben.io >> ~/.ssh/known_hosts

# 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 \
    --mount=type=secret,id=ssh_key,target=/root/.ssh/id_rsa \
    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 \
    --mount=type=secret,id=ssh_key,target=/root/.ssh/id_rsa \
    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"]
