FROM node:20-bookworm

RUN apt-get update && apt-get install -y \
    curl \
    jq \
    s-nail \
    libwww-curl-perl \
    cpanminus \
    && rm -rf /var/lib/apt/lists/*

RUN cpanm --notest WWW::Curl::Simple
RUN cpanm --notest JSON

# Set working directory
WORKDIR /app

# Copy scripts from parent directory
COPY download-roster.js ./
COPY google-civic-api.pl ./
COPY subscribe-members.sh ./
COPY env ./
COPY automation/run-subscription.sh ./
COPY automation/entrypoint.sh ./

# Make scripts executable
RUN chmod +x *.sh *.pl

# Install Node dependencies for download-roster.js
RUN npm install playwright 
RUN npx playwright install chromium --with-deps



# Create work directory for temporary files
RUN mkdir -p /tmp/rosters

# Set environment variables
ENV NODE_ENV=production \
    WORK_DIR=/tmp/rosters \
    PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright

# Use entrypoint to parse env file
ENTRYPOINT ["/app/entrypoint.sh"]

# Keep container running for Ofelia to execute commands
CMD ["tail", "-f", "/dev/null"]
