# Stage 1: Build dependencies
FROM python:3.13.7 AS builder

WORKDIR /workspace/suno

# Install dependencies via uv -- separate step to allow for better Docker caching
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

COPY uv.lock pyproject.toml ./
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-install-project --no-dev

# Stage 2: Final image
FROM python:3.13.7-slim

# Install system dependencies + build tools FIRST (best caching)
RUN apt-get update && apt-get install -y \
    gdb \
    curl \
    git \
    libpq5 \
    libjpeg62-turbo \
    libgomp1 \
    libglib2.0-0 \
    libjemalloc2 \
    build-essential \
    g++ \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /workspace/suno

# Copy dependencies and downloaded files from the builder stage
COPY --from=builder /workspace/suno /workspace/suno
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# Copy relevant project files into docker image
COPY . .

# Actually sync the project itself, this will maybe change more frequently due to code changes
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev

# Datadog source code integration (MUST be last to avoid breaking cache!)
ARG DD_GIT_REPOSITORY_URL
ARG DD_GIT_COMMIT_SHA

ENV DD_GIT_REPOSITORY_URL=${DD_GIT_REPOSITORY_URL}
ENV DD_GIT_COMMIT_SHA=${DD_GIT_COMMIT_SHA}

CMD ["./run.sh"]