# Build Stage
FROM node:22-alpine AS builder
WORKDIR /usr/app

# 1) 의존성 레이어 — package*.json 변경 시에만 재실행 (소스만 바뀌면 캐시 재사용)
COPY package*.json ./
RUN apk add --no-cache python3 make g++ cmake && \
    npm ci && \
    npm install @nestjs/cli koffi

# 2) 소스 복사 후 빌드 (이 레이어만 소스 변경에 깨짐)
COPY tsconfig*.json ./
COPY drizzle/ ./drizzle/
COPY apps/api/ ./apps/api/
COPY libs/ ./libs/
COPY mok_keyInfo*.dat .

RUN npm run build && npm prune --production

FROM node:22-alpine
WORKDIR /usr/app

# 청구서 양식 PDF 압축용 (ghostscript) — 팩스 업로드 한도 대비 다운샘플
RUN apk add --no-cache ghostscript

# 보장·청구 분석 워커 — 글자가 있는 PDF 읽기 · 약관(pdftotext) · 분석 · AI 읽기(env CASE_REPORT_WIF 로 인증)를 넣는다. 판본은 workers/case-report/requirements.txt 와 같게 둔다
# 스캔 쪽 글자 인식은 Lambda(workers/case-report/ocr-lambda)로 보낸다 — env CASE_REPORT_OCR_LAMBDA 가 있을 때만, boto3 · 태스크 역할로. 없으면 OCR_UNAVAILABLE
RUN apk add --no-cache python3 poppler-utils && \
    python3 -m venv /opt/case-report && \
    /opt/case-report/bin/pip install --no-cache-dir pdfplumber==0.11.10 pdfminer.six==20260107 pypdfium2==5.14.0 cryptography==50.0.2 Pillow==12.3.0 anthropic==2.3.0 boto3==1.43.125
ENV CASE_REPORT_PYTHON=/opt/case-report/bin/python

# 필요한 파일만 복사
COPY --from=builder /usr/app/dist ./dist
COPY --from=builder /usr/app/node_modules ./node_modules
COPY --from=builder /usr/app/drizzle ./drizzle
COPY --from=builder /usr/app/mok_keyInfo*.dat .
COPY workers/case-report/case_report ./workers/case-report/case_report

EXPOSE 7200

# 실행 모드를 위한 ARG 추가
ARG RUN_MODE
ARG SCHEDULER_TYPE
ENV RUN_MODE=${RUN_MODE}
ENV SCHEDULER_TYPE=${SCHEDULER_TYPE}

CMD ["sh", "-c", "if [ \"$RUN_MODE\" = \"scheduler\" ]; then \
    case \"$SCHEDULER_TYPE\" in \
    \"regular-subscription\") node dist/apps/api/src/schedules/regular-subscription.schedule.js ;; \
    \"regular-agency-subscription\") node dist/apps/api/src/schedules/regular-agency-subscription.schedule.js ;; \
    \"alimtalk-status\") node dist/apps/api/src/schedules/alimtalk-status.schedule.js ;; \
    \"analysis-status\") node dist/apps/api/src/schedules/analysis-status.schedule.js ;; \
    \"analysis-stall\") node dist/apps/api/src/schedules/analysis-stall.schedule.js ;; \
    \"delete-treat\") node dist/apps/api/src/schedules/delete-treat.schedule.js ;; \
    \"disable-subscription\") node dist/apps/api/src/schedules/disable-subscription.schedule.js ;; \
    \"settlement-agency\") node dist/apps/api/src/schedules/settlement-agency.schedule.js ;; \
    \"settlement-client\") node dist/apps/api/src/schedules/settlement-client.schedule.js ;; \
    \"delete-analysis\") node dist/apps/api/src/schedules/delete-analysis.schedule.js ;; \
    \"delete-fc\") node dist/apps/api/src/schedules/delete-fc.schdule.js ;; \
    \"sales-statistics\") node dist/apps/api/src/schedules/sales-statistics.schedule.js ;; \
    \"samsung-s2-report-preview\") node dist/apps/api/src/schedules/samsung-s2-report.schedule.js preview ;; \
    \"samsung-s2-report-final\") node dist/apps/api/src/schedules/samsung-s2-report.schedule.js final ;; \
    \"expired-money\") node dist/apps/api/src/schedules/expired-money.schedule.js ;; \
    \"delete-invoice-files\") node dist/apps/api/src/schedules/delete-invoice-files.schedule.js ;; \
    \"fax-status\") node dist/apps/api/src/schedules/fax-status.schedule.js ;; \
    \"ddx-verdict\") node dist/apps/api/src/schedules/ddx-verdict.schedule.js ;; \
    *) echo unknown-SCHEDULER_TYPE >&2; exit 1 ;; \
    esac \
    else node dist/apps/api/src/main.js; fi"]
