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

WORKDIR /app/frontend

# Copy frontend package files
COPY frontend/package*.json ./

# Install dependencies
RUN npm ci

# Copy frontend source
COPY frontend/ ./

# Build frontend with production API URL (relative path)
# This ensures the frontend uses relative paths instead of localhost:8081
ENV VITE_API_URL=/api
RUN npm run build

# Stage 2: Build backend
FROM golang:1.25-alpine AS backend-builder

WORKDIR /app

# Install build dependencies for libheif and SQLite FTS5
RUN apk add --no-cache \
    gcc \
    g++ \
    musl-dev \
    libheif-dev

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

# Download dependencies
RUN go mod download

# Copy backend source
COPY *.go ./
COPY internal/*.go internal/

# Build with FTS5 support
# Use CGO for SQLite and libheif
RUN CGO_ENABLED=1 go build -tags "fts5 heic" -o sbv .

# Stage 3: Final runtime image
FROM alpine:3

WORKDIR /app

# Install runtime dependencies
RUN apk add --no-cache \
    ca-certificates \
    wget \
    ffmpeg \
    libheif

# Copy backend binary
COPY --from=backend-builder /app/sbv .

# Copy frontend build
COPY --from=frontend-builder /app/frontend/dist ./frontend/dist

# Create data directory for database
RUN mkdir -p /data

# Set environment variables
ENV PORT=8081
ENV DB_PATH_PREFIX=/data

# Expose port
EXPOSE 8081

# Run the application
CMD ["./sbv"]
