# Kuwait Closed-Loop Prepaid Card — Multi-stage Dockerfile
# Build with:  docker build -t wallet_app .
# Run with:    docker-compose -f docker-compose.prepaid.yml up

# ─── Stage 1: Build ──────────────────────────────────────────────────────────
FROM elixir:1.16-otp-26-alpine AS builder

# Install build dependencies
RUN apk add --no-cache build-base git nodejs npm

WORKDIR /app

# Copy dependency manifests
COPY mix.exs mix.lock ./
COPY apps/wallet_prepaid/mix.exs apps/wallet_prepaid/

# Copy all app mix.exs files (umbrella discovery)
COPY apps/wallet_shared_kernel/mix.exs    apps/wallet_shared_kernel/
COPY apps/wallet_api_contracts/mix.exs    apps/wallet_api_contracts/
COPY apps/wallet_events/mix.exs           apps/wallet_events/
COPY apps/wallet_state/mix.exs            apps/wallet_state/
COPY apps/wallet_observability/mix.exs    apps/wallet_observability/
COPY apps/wallet_auth/mix.exs             apps/wallet_auth/
COPY apps/wallet_accounts/mix.exs         apps/wallet_accounts/
COPY apps/wallet_ledger/mix.exs           apps/wallet_ledger/
COPY apps/wallet_transfers/mix.exs        apps/wallet_transfers/
COPY apps/wallet_limits_fees/mix.exs      apps/wallet_limits_fees/
COPY apps/wallet_journey/mix.exs          apps/wallet_journey/
COPY apps/wallet_settlement/mix.exs       apps/wallet_settlement/
COPY apps/wallet_notifications/mix.exs    apps/wallet_notifications/
COPY apps/wallet_integrations/mix.exs     apps/wallet_integrations/
COPY apps/wallet_compliance/mix.exs       apps/wallet_compliance/
COPY apps/wallet_risk/mix.exs             apps/wallet_risk/
COPY apps/wallet_resilience/mix.exs       apps/wallet_resilience/
COPY apps/wallet_production/mix.exs       apps/wallet_production/
COPY apps/wallet_database/mix.exs         apps/wallet_database/
COPY apps/wallet_cards/mix.exs            apps/wallet_cards/
COPY apps/wallet_rewards/mix.exs          apps/wallet_rewards/
COPY apps/wallet_loans/mix.exs            apps/wallet_loans/
COPY apps/wallet_insurance/mix.exs        apps/wallet_insurance/
COPY apps/wallet_reporting/mix.exs        apps/wallet_reporting/
COPY apps/wallet_disputes/mix.exs         apps/wallet_disputes/
COPY apps/wallet_merchant/mix.exs         apps/wallet_merchant/
COPY apps/wallet_web/mix.exs              apps/wallet_web/

# Fetch dependencies
ENV MIX_ENV=prod
RUN mix local.hex --force && mix local.rebar --force
RUN mix deps.get --only prod
RUN mix deps.compile

# Copy source
COPY . .

# Compile assets
RUN cd apps/wallet_web && npm install --prefix assets
RUN cd apps/wallet_web && mix assets.deploy

# Compile app
RUN mix compile

# Build release
RUN mix release wallet_web

# ─── Stage 2: Runtime ────────────────────────────────────────────────────────
FROM alpine:3.18 AS runtime

# Install runtime dependencies
RUN apk add --no-cache libstdc++ openssl ncurses-libs curl

WORKDIR /app

# Copy release from builder
COPY --from=builder /app/_build/prod/rel/wallet_web ./

# Create non-root user
RUN addgroup -S wallet && adduser -S wallet -G wallet
RUN chown -R wallet:wallet /app
USER wallet

EXPOSE 4000

ENTRYPOINT ["./bin/wallet_web"]
CMD ["start"]
