# Build stage
FROM docker.io/library/golang:1.26-alpine AS build-env

# Сборочных зависимостей нет: хранилище ходит в SQLite через modernc.org/sqlite,
# и CGO больше не требуется.

# Set up the working directory
WORKDIR /app

# Copy go mod files
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy source code
COPY . .

# Build the application
RUN CGO_ENABLED=0 go build -o transcriber .

# ----------------
# Production stage
# ----------------

FROM docker.io/library/alpine:latest

LABEL maintainer="anton@vakhrushev.me"

# Install runtime dependencies
RUN apk --no-cache add \
    ca-certificates \
    ffmpeg \
    && rm -rf /var/cache/apk/*

# Create user and group
RUN addgroup -S -g 1000 transcriber && \
    adduser -S -H -D \
    -h /data/transcriber \
    -s /bin/sh \
    -u 1000 \
    -G transcriber \
    transcriber

# Set environment variables
ENV USER=transcriber

# Create necessary directories
RUN mkdir -p /data && chown -R transcriber:transcriber /data
RUN mkdir -p /config && chown -R transcriber:transcriber /config

# Set working directory
WORKDIR /app

# Copy the binary from build stage
COPY --from=build-env /app/transcriber .

# Copy entrypoint script
COPY docker/entrypoint.sh /usr/bin/entrypoint
RUN chmod 755 /usr/bin/entrypoint

# Set user
USER transcriber

EXPOSE 8080

# Set entrypoint and default command
# ENTRYPOINT ["/usr/bin/entrypoint"]
CMD ["./transcriber"]
