# syntax=docker/dockerfile:1

# =========================================================
# Base
# =========================================================
FROM node:20-bookworm-slim AS base

WORKDIR /app

ENV NEXT_TELEMETRY_DISABLED=1
ENV HUSKY=0


# =========================================================
# Dependencies
# ติดตั้งทั้ง dependencies และ devDependencies
# เพราะ builder และ migrator ต้องใช้ TypeScript, tsx, drizzle-kit
# =========================================================
FROM base AS deps

COPY package.json ./

RUN npm i --no-audit --no-fund


# =========================================================
# Next.js Builder
# =========================================================
FROM base AS builder

ENV NODE_ENV=production
ENV BUILD_STANDALONE=true
ENV NEXT_TELEMETRY_DISABLED=1
ENV HUSKY=0

ARG NEXT_PUBLIC_SENTRY_DISABLED=true
ENV NEXT_PUBLIC_SENTRY_DISABLED=${NEXT_PUBLIC_SENTRY_DISABLED}

COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npm run build


# =========================================================
# Migration / Seed Runner
# =========================================================
FROM base AS migrator

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV HUSKY=0

COPY --from=deps /app/node_modules ./node_modules

COPY package.json ./
COPY drizzle.config.* ./
COPY drizzle ./drizzle
COPY src ./src
COPY scripts ./scripts
COPY tsconfig.json ./

# เปิดใช้หาก seed script import ไฟล์จาก directory อื่น
# COPY data ./data
# COPY seed ./seed

CMD ["sh", "-c", "\
    set -eu; \
    echo '========================================'; \
    echo 'Running database migrations'; \
    echo '========================================'; \
    npm run migrate; \
    if [ \"${RUN_MASTER_DATA_SEED:-false}\" = \"true\" ]; then \
    echo '========================================'; \
    echo 'Running master-data seed'; \
    echo '========================================'; \
    npm run seed:master-data; \
    else \
    echo 'Skipping master-data seed'; \
    fi; \
    if [ \"${RUN_SUPER_ADMIN_SEED:-false}\" = \"true\" ]; then \
    echo '========================================'; \
    echo 'Running super-admin seed'; \
    echo '========================================'; \
    npm run seed:super-admin; \
    else \
    echo 'Skipping super-admin seed'; \
    fi; \
    echo '========================================'; \
    echo 'Database initialization completed'; \
    echo '========================================'; \
    tail -f /dev/null \
    "]


# =========================================================
# Next.js Production Runner
# =========================================================
FROM node:20-bookworm-slim AS runner

WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
ENV HOME=/home/nextjs

RUN groupadd --system --gid 1001 nodejs \
    && useradd \
    --system \
    --uid 1001 \
    --gid nodejs \
    --create-home \
    nextjs

COPY --from=builder --chown=nextjs:nodejs /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

RUN mkdir -p /app/storage \
    && chown -R nextjs:nodejs /app/storage /home/nextjs

USER nextjs

EXPOSE 3000

CMD ["node", "server.js"]