name: Daily Release

on:
  schedule:
    - cron: "0 0 * * *" # Runs at midnight UTC daily
  workflow_dispatch: # Allows manual triggering

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: 'https://registry.npmjs.org/'

      - name: Install dependencies
        run: npm install

      - name: Update Date and Version
        run: node updatePackage.js

      - name: Commit Updated Files
        run: |
          git config --global user.name 'arimet'
          git config --global user.email 'anthony.rimet03@gmail.com'
          git add package.json index.js package-lock.json
          if ! git diff-index --quiet HEAD --; then
            git commit -m "Auto-update for $(date +'%Y-%m-%d')"
            git push
          else
            echo "No changes to commit"
          fi

      - name: Verify NPM User
        run: npm whoami
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}    

      - name: Publish Package
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}