# Stage 1: Build Go binary
FROM golang:1.26-alpine AS builder

RUN apk add --no-cache git ca-certificates

WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ytdlp-navidrome .

# Stage 2: Install yt-dlp and ffmpeg
FROM alpine:latest AS tools

RUN apk add --no-cache \
    python3 \
    py3-pip \
    ffmpeg \
    ca-certificates

RUN pip3 install --break-system-packages yt-dlp

# Stage 3: Final image
FROM alpine:latest

RUN apk add --no-cache \
    ca-certificates \
    ffmpeg \
    python3 \
    libc6-compat

# Copy Python + packages from tools stage
COPY --from=tools /usr/lib/python3.12 /usr/lib/python3.12
COPY --from=tools /usr/bin/yt-dlp /usr/bin/yt-dlp
COPY --from=tools /usr/bin/ffmpeg /usr/bin/ffmpeg
COPY --from=tools /usr/bin/ffprobe /usr/bin/ffprobe

# Copy Go binary
COPY --from=builder /build/ytdlp-navidrome /usr/local/bin/ytdlp-navidrome

# Create directories
RUN mkdir -p /music/ytdlp /tmp/ytdlp-previews

ENTRYPOINT ["/usr/local/bin/ytdlp-navidrome"]
