# Development stage - pocketbase only
FROM alpine:latest AS dev

ARG PB_VERSION=0.36.8

RUN apk add --no-cache \
    unzip \
    ca-certificates

# Download and unzip PocketBase
ADD https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_amd64.zip /tmp/pb.zip
RUN unzip /tmp/pb.zip -d /pb/

EXPOSE 8090 8091

CMD ["/pb/pocketbase", "serve", "--http=0.0.0.0:8090"]

# Build stage
FROM denoland/deno:latest AS build
WORKDIR /app

COPY ./sidecar .

# Install dependencies
RUN deno install

# Production stage
FROM denoland/deno:latest AS prod
WORKDIR /app

# Copy sidecar
COPY --from=build /app ./sidecar

# Copy pocketbase binary
COPY --from=dev /pb/pocketbase ./pocketbase

COPY ./pb_migrations ./pb_migrations
COPY ./pb_hooks ./pb_hooks
COPY ./sidecar ./sidecar
COPY ./start.sh ./start.sh

RUN chmod +x ./start.sh

CMD ["./start.sh"]
