# Stage 1: Build Frontend
FROM node:22-alpine AS builder

WORKDIR /app

# Install dependencies
COPY frontend/package*.json ./
RUN npm install

# Copy source and build
COPY frontend/ ./
RUN npm run build

# Stage 2: Runtime
FROM alpine:latest

WORKDIR /pb

# Install dependencies
RUN apk add --no-cache \
    ca-certificates \
    unzip \
    wget

# Download PocketBase
# Using version 0.22.21 as referenced in backend scripts
ARG PB_VERSION=0.22.21
RUN wget https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_amd64.zip \
    && unzip pocketbase_${PB_VERSION}_linux_amd64.zip \
    && rm pocketbase_${PB_VERSION}_linux_amd64.zip

# Make executable
RUN chmod +x /pb/pocketbase

# Copy built frontend assets to PocketBase public directory
# PocketBase checks pb_public by default for static files
COPY --from=builder /app/dist /pb/pb_public

# Copy local migrations to container
COPY backend/pb_migrations /pb/pb_migrations

# Expose PocketBase port
EXPOSE 8090

# Define volume for data persistence (handled in compose, but good practice to document)
VOLUME /pb/pb_data

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