# Build stage for Elm frontend
FROM node:25-alpine AS elm-build

WORKDIR /frontend

# Install Elm
RUN npm install -g elm@latest-0.19.1

# Copy Elm files
COPY frontend/elm.json .
COPY frontend/src ./src

# Build Elm app
RUN elm make src/Main.elm --optimize --output=elm.js

# Build stage for Go backend
FROM golang:1.25.3-alpine AS go-build

WORKDIR /app

# Copy go mod files
COPY backend/go.mod backend/go.sum ./
RUN go mod download

# Copy backend source
COPY backend/ ./

# Build Go binary
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Final stage
FROM alpine:latest

RUN apk --no-cache add ca-certificates tzdata

WORKDIR /root/

# Copy Go binary from build stage
COPY --from=go-build /app/main .

# Create static directory
RUN mkdir -p /root/static

# Copy Elm build artifacts
COPY --from=elm-build /frontend/elm.js /root/static/
COPY frontend/public/index.html /root/static/

# Create volume for database
VOLUME ["/data"]

ENV PORT=8080
ENV DB_PATH=/data/timetracking.db

EXPOSE 8080

CMD ["./main"]
