# Self-contained build for EagleCast. Builds the email-builder and admin
# frontend, compiles the Go backend, and stuffs all static assets into a
# single binary. Used by PaaS platforms like Coolify that build from git.
# (Release builds via goreleaser use Dockerfile.goreleaser instead.)

ARG APP_VERSION=v1.0.0

# ---- email-builder (React) ----
FROM node:22-alpine AS email-builder
WORKDIR /build/email-builder
COPY frontend/email-builder/package.json frontend/email-builder/yarn.lock ./
RUN yarn install --frozen-lockfile
COPY frontend/email-builder/ ./
RUN yarn build

# ---- admin frontend (Vue) ----
FROM node:22-alpine AS frontend
ARG APP_VERSION
WORKDIR /build/frontend
COPY frontend/package.json frontend/yarn.lock ./
RUN yarn install --frozen-lockfile
COPY frontend/ ./
COPY --from=email-builder /build/email-builder/dist ./public/static/email-builder
ENV VUE_APP_VERSION=${APP_VERSION}
RUN yarn build

# ---- backend (Go) ----
FROM golang:1.26-alpine AS backend
ARG APP_VERSION
WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
RUN go install github.com/knadh/stuffbin/...@latest
COPY . .
COPY --from=frontend /build/frontend/dist ./frontend/dist
RUN CGO_ENABLED=0 go build -o eaglecast \
    -ldflags="-s -w -X 'main.buildString=${APP_VERSION} (docker)' -X 'main.versionString=${APP_VERSION}'" \
    ./cmd
RUN /go/bin/stuffbin -a stuff -in eaglecast -out eaglecast \
    config.toml.sample \
    schema.sql queries:/queries permissions.json \
    static/public:/public \
    static/email-templates \
    frontend/dist:/admin \
    i18n:/i18n

# ---- runtime ----
FROM alpine:latest

# Install dependencies
RUN apk --no-cache add ca-certificates tzdata shadow su-exec

# Set the working directory
WORKDIR /eaglecast

# Copy only the necessary files
COPY --from=backend /build/eaglecast .
COPY config.toml.sample config.toml

# Copy the entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/

# Make the entrypoint script executable
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

# Expose the application port
EXPOSE 9000

# Set the entrypoint
ENTRYPOINT ["docker-entrypoint.sh"]

# Define the command to run the application
CMD ["./eaglecast"]
