# Build stage
FROM golang:1.23-alpine AS builder

RUN apk add --no-cache git

WORKDIR /app

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

# Copy source code
COPY server/ ./
COPY agent/ ../agent/

# Build server
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /nuyue-server ./cmd/server

# Build agent
RUN cd ../agent && CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /nuyue-agent ./cmd/agent

# Runtime stage
FROM alpine:3.19

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

WORKDIR /app

# Copy binaries
COPY --from=builder /nuyue-server /app/
COPY --from=builder /nuyue-agent /app/

# Copy frontend (built by CI)
COPY server/embed/dist /app/embed/dist

# Expose ports
EXPOSE 8080 9090

# Run server
CMD ["/app/nuyue-server"]
