FROM openresty/openresty:1.21.4.1-alpine

# Install additional dependencies
RUN apk add --no-cache \
    curl \
    jq \
    bash \
    openssl \
    perl

# Install Lua modules using opm
RUN /usr/local/openresty/bin/opm install ledgetech/lua-resty-http

# Create directories
RUN mkdir -p /usr/local/openresty/nginx/lua \
    /etc/nginx/keys \
    /etc/nginx/models \
    /var/log/nginx

# Copy configuration files
COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
COPY conf.d/ /etc/nginx/conf.d/
COPY lua/ /usr/local/openresty/nginx/lua/

# Create cache directories
RUN mkdir -p /var/cache/nginx/auth

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost/health || exit 1

EXPOSE 80

CMD ["/usr/local/openresty/bin/openresty", "-g", "daemon off;"]