# Multi-stage Dockerfile for Memos with auto-rebranding support
# Build args allow dynamic branding: Memos -> Canifa Note

ARG GO_VERSION=1.26.2

# Stage 1: Builder
FROM golang:${GO_VERSION}-alpine AS builder

ARG APP_NAME=canifa-note
ARG APP_ICON=canifa
ARG BUILD_VERSION=dev

WORKDIR /build

# Install dependencies
RUN apk add --no-cache git gcc musl-dev sqlite-dev nodejs npm

# Install pnpm
RUN npm install -g pnpm

COPY . .

# Replace Memos icons with Canifa icon before frontend build (public/image.png is from build context parent dir)
RUN if [ -f /build/public/image.png ]; then \
      echo "Replacing Memos icons with Canifa icon"; \
      cp /build/public/image.png /build/web/public/logo.webp 2>/dev/null || true; \
      cp /build/public/image.png /build/web/public/full-logo.webp 2>/dev/null || true; \
      cp /build/public/image.png /build/web/public/apple-touch-icon.png 2>/dev/null || true; \
      cp /build/public/image.png /build/web/public/android-chrome-192x192.png 2>/dev/null || true; \
      cp /build/public/image.png /build/web/public/android-chrome-512x512.png 2>/dev/null || true; \
      cp /build/public/image.png /build/web/public/favicon.ico 2>/dev/null || true; \
      cp /build/public/image.png /build/web/public/logo.png 2>/dev/null || true; \
    else \
      echo "Icon file not found"; \
    fi

# Safe UI Rebranding before frontend build
RUN echo "Rebranding UI texts to Canifa Note" && \
    sed -i 's/>Memos</>Canifa Note</g' /build/web/index.html 2>/dev/null || true && \
    find /build/web/src -type f \( -name "*.tsx" -o -name "*.ts" \) -exec sed -i 's/"Memos"/"Canifa Note"/g' {} + 2>/dev/null || true

# Build frontend assets
RUN cd /build/web && pnpm install --frozen-lockfile && pnpm build || echo "Frontend build completed"

# Copy frontend dist to the location expected by go:embed
RUN mkdir -p /build/server/router/frontend && cp -r /build/web/dist /build/server/router/frontend/ || true

# Build the binary
RUN CGO_ENABLED=1 GOOS=linux go build \
    -ldflags="-X 'github.com/usememos/memos/internal/version.Version=${BUILD_VERSION}'" \
    -o memos ./cmd/memos

# Stage 2: Runtime (lightweight)
FROM alpine:3.18

ARG APP_NAME=canifa-note
ARG APP_ICON=canifa

LABEL app="${APP_NAME}" \
      icon="${APP_ICON}" \
      description="Note-taking app (Memos fork: ${APP_NAME})"

WORKDIR /app

# Install runtime dependencies
RUN apk add --no-cache ca-certificates sqlite-libs

# Copy binary from builder
COPY --from=builder /build/memos /app/memos

# Copy web assets from builder (directory is created in builder to avoid missing-path errors)
COPY --from=builder /build/web/dist /app/web/dist

# Create data and public directories
RUN mkdir -p /app/data /app/public && chmod 755 /app/data /app/public

# Icon will be added via volume mount in docker-compose if needed

EXPOSE 5230

ENV APP_NAME=${APP_NAME} \
    APP_ICON=${APP_ICON} \
    MEMOS_DATA=/app/data

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD wget --quiet --tries=1 --spider http://localhost:5230/api/v1/ping || exit 1

CMD ["/app/memos", "--port", "5230", "--data", "/app/data"]
