# syntax=docker/dockerfile:1
# AlterFlixHub — static export (ไฟล์ prerender อยู่ที่ราก repo, ไม่มี build step)
# runtime = nginx alpine · ฟังพอร์ตจาก env PORT (บน Dokploy ตั้ง PORT=30004)
#
# env ที่ใช้
#   PORT            พอร์ตที่ nginx ฟังในคอนเทนเนอร์ (default 30004)
#   PUBLIC_ORIGIN   โดเมนจริงที่เขียนลง canonical/OG/JSON-LD/sitemap/robots (default = โดเมน production)
#   BAKED_ORIGIN    origin ที่อบมากับไฟล์ export — ใช้เป็นต้นทางของการแทนที่ตอน start
#   TZ              timezone ของ log

FROM nginx:1.27-alpine

ENV PORT=30004 \
    PUBLIC_ORIGIN=https://alterflixhub.hobbypj.com \
    BAKED_ORIGIN=https://alterflixhub.hobbypj.com \
    TZ=Asia/Bangkok

# nginx config เป็น template → official entrypoint แทนค่า ${PORT} ให้เองตอน start
COPY docker/default.conf.template /etc/nginx/templates/default.conf.template
# ชุด header ความปลอดภัยที่ทุก location include (nginx ทิ้ง add_header ของ parent เมื่อ location มี add_header เอง)
COPY docker/security-headers.conf /etc/nginx/snippets/security-headers.conf
# สคริปต์แทน origin ในไฟล์ HTML/XML/TXT ถ้า PUBLIC_ORIGIN ไม่ตรงกับที่อบไว้
COPY docker/30-origin.sh /docker-entrypoint.d/30-origin.sh
RUN chmod +x /docker-entrypoint.d/30-origin.sh

# ไฟล์เว็บทั้งหมด (static export) — ไฟล์ของ docker/ ไม่ถูกคัดออกด้วย .dockerignore
COPY . /usr/share/nginx/html

EXPOSE 30004

HEALTHCHECK --interval=30s --timeout=4s --start-period=5s --retries=3 \
  CMD wget -qO- "http://127.0.0.1:${PORT}/healthz" >/dev/null 2>&1 || exit 1

# ใช้ entrypoint/cmd เดิมของอิมเมจ (รัน /docker-entrypoint.d/*.sh แล้วค่อย start nginx)
