# =============================================================================
# Build context: monorepo root (.)
# This Dockerfile is built with `docker build -f api/Dockerfile .` from root.
# =============================================================================

# ---- Stage 1: Install production dependencies ----
FROM oven/bun:1.3.11 AS deps

WORKDIR /app

# Copy root workspace manifest and lockfile
COPY package.json bun.lock ./

# Copy workspace package manifests (source not needed — just for bun workspace resolution)
COPY api/package.json ./api/package.json
COPY dalpuri/package.json ./dalpuri/package.json
COPY ui/package.json ./ui/package.json

RUN bun install --frozen-lockfile --production

# ---- Stage 2: Build ----
FROM oven/bun:1.3.11 AS build

WORKDIR /app

# Copy root workspace manifest and lockfile, plus workspace package manifests
COPY package.json bun.lock ./
COPY api/package.json ./api/package.json
COPY dalpuri/package.json ./dalpuri/package.json
COPY ui/package.json ./ui/package.json

# Install all deps (including dev) for the full workspace
RUN bun install --frozen-lockfile

# Copy API source and config
COPY api/src/ ./api/src/
COPY api/prisma/ ./api/prisma/
COPY api/prisma.config.ts api/tsconfig.json ./api/
COPY api/logo.png ./api/logo.png

# Copy Dalpuri source and Prisma schema
COPY dalpuri/src/ ./dalpuri/src/
COPY dalpuri/prisma/ ./dalpuri/prisma/
COPY dalpuri/prisma.config.ts ./dalpuri/prisma.config.ts

# Generate Dalpuri Prisma client (ConnectWise MSSQL schema)
# prisma generate does not connect — dummy URL just satisfies the env requirement
WORKDIR /app/dalpuri
RUN DATABASE_URL="sqlserver://localhost:1433;database=dummy;user=dummy;password=dummy;trustServerCertificate=true" \
    bunx prisma generate

# Generate API Prisma client (PostgreSQL schema)
WORKDIR /app/api
RUN DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy" bunx prisma generate

# Compile the API server to a standalone binary
RUN NODE_ENV=production bun build src/index.ts \
  --compile \
  --minify \
  --target=bun-linux-x64 \
  --outfile=server

# Compile the worker process to a standalone binary
RUN NODE_ENV=production bun build src/workert.ts \
  --compile \
  --minify \
  --target=bun-linux-x64 \
  --outfile=worker

# ---- Stage 3: Shared runtime base (API server and worker share the same deps/files) ----
FROM ubuntu:22.04 AS runtime-base

WORKDIR /app

# Install minimal runtime dependencies (CA certs for HTTPS calls)
RUN apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Copy Prisma artifacts needed at runtime
COPY --from=build /app/api/generated/ ./generated/
COPY --from=build /app/api/prisma/ ./prisma/
COPY --from=build /app/api/prisma.config.ts ./prisma.config.ts

# Copy Dalpuri generated Prisma client (worker imports dalpuri which references this)
COPY --from=build /app/dalpuri/generated/ ./dalpuri/generated/

# Copy production node_modules (Prisma adapter needs native bindings)
COPY --from=deps /app/node_modules/ ./node_modules/

ENV NODE_ENV=production

# ---- Stage 4: API server runtime image ----
FROM runtime-base AS runtime

# API-specific: PDF branding asset and sales tax lookup data
COPY --from=build /app/api/server ./server
COPY --from=build /app/api/logo.png ./logo.png
COPY --from=build /app/api/src/modules/sales-utils/salesTaxRates.json ./salesTaxRates.json

EXPOSE 3000
CMD ["./server"]

# ---- Stage 5: Worker runtime image ----
FROM runtime-base AS worker

COPY --from=build /app/api/worker ./worker

# Default to localhost for local dev; override with k8s internal service URL in production
ENV MANAGER_SOCKET_URL=http://localhost:8671

CMD ["./worker"]

# ---- Stage 6: Migration runner ----
FROM oven/bun:1.3.11 AS migration

WORKDIR /app

# Copy workspace manifests for bun workspace resolution
COPY package.json bun.lock ./
COPY api/package.json ./api/package.json
COPY dalpuri/package.json ./dalpuri/package.json
COPY ui/package.json ./ui/package.json

RUN bun install --frozen-lockfile

COPY api/prisma/ ./api/prisma/
COPY api/prisma.config.ts ./api/prisma.config.ts

RUN chmod +x /app/api/prisma/migrate-entrypoint.sh

WORKDIR /app/api
CMD ["sh", "prisma/migrate-entrypoint.sh"]