# Build frontend
FROM node:20-alpine AS frontend
WORKDIR /app/frontend
COPY frontend/package*.json ./
RUN if [ -f package-lock.json ]; then npm ci; else npm install; fi
COPY frontend ./
RUN npm run build

# Build backend
FROM golang:1.23-alpine AS backend
WORKDIR /app/backend
RUN apk add --no-cache git
COPY backend/go.mod ./
COPY backend/go.sum ./
RUN go mod download
COPY backend ./
RUN CGO_ENABLED=0 go build -o /app/bin/server ./cmd/server

# Final image
FROM alpine:3.20
RUN adduser -D -H -u 10001 app && apk add --no-cache ca-certificates \
  && mkdir -p /data /app \
  && chown -R app:app /data /app
WORKDIR /app
COPY --from=backend /app/bin/server /app/server
COPY --from=frontend /app/frontend/dist /app/frontend/dist
EXPOSE 8088
ENV PORT=8088
CMD ["/app/server"]
