FROM node:20-alpine AS builder

WORKDIR /build

COPY input.css tailwind.config.js ./
COPY *.html ./

RUN --mount=type=cache,target=/root/.npm \
    npx tailwindcss@3 -i input.css -o output.css --minify

FROM nginx:alpine

ARG APP_URL=https://app.batida.io

RUN addgroup -g 1000 -S nginx-user && \
    adduser -u 1000 -S nginx-user -G nginx-user

COPY --from=builder /build/output.css /usr/share/nginx/html/output.css
COPY favicon.png /usr/share/nginx/html/favicon.png
COPY og-image.png /usr/share/nginx/html/og-image.png
COPY robots.txt /usr/share/nginx/html/robots.txt
COPY sitemap.xml /usr/share/nginx/html/sitemap.xml
COPY index.html /tmp/index.html
COPY index.pt-br.html /tmp/index.pt-br.html
COPY vs.html /tmp/vs.html
COPY vs.pt-br.html /tmp/vs.pt-br.html

RUN sed -i "s|{{APP_URL}}|${APP_URL}|g" /tmp/index.html && \
    sed -i "s|{{APP_URL}}|${APP_URL}|g" /tmp/index.pt-br.html && \
    sed -i "s|{{APP_URL}}|${APP_URL}|g" /tmp/vs.html && \
    sed -i "s|{{APP_URL}}|${APP_URL}|g" /tmp/vs.pt-br.html && \
    mkdir -p /usr/share/nginx/html/pt-br && \
    mv /tmp/index.html /usr/share/nginx/html/index.html && \
    mv /tmp/index.pt-br.html /usr/share/nginx/html/pt-br/index.html && \
    mv /tmp/vs.html /usr/share/nginx/html/vs.html && \
    mv /tmp/vs.pt-br.html /usr/share/nginx/html/pt-br/vs.html && \
    chown -R nginx-user:nginx-user /usr/share/nginx/html && \
    chown -R nginx-user:nginx-user /var/cache/nginx && \
    chown -R nginx-user:nginx-user /var/log/nginx && \
    chown -R nginx-user:nginx-user /etc/nginx/conf.d && \
    touch /run/nginx.pid && \
    chown -R nginx-user:nginx-user /run/nginx.pid

COPY nginx.conf /etc/nginx/conf.d/default.conf

USER nginx-user

EXPOSE 8080

CMD ["nginx", "-g", "daemon off;"]
