name: Update README
on:
  schedule: [{ cron: '0 4,16 * * *' }]
  workflow_dispatch:
jobs:
  update_readme:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Create .env file
        run: |
          echo "GHOST_URL=${{ secrets.GHOST_URL }}" >> .env
          echo "GHOST_KEY=${{ secrets.GHOST_KEY }}" >> .env
          echo "WAKAPI_URL=${{ secrets.WAKAPI_URL }}" >> .env
          echo "WAKAPI_KEY=${{ secrets.WAKAPI_KEY }}" >> .env
          echo "DUOLINGO_URL=${{ secrets.DUOLINGO_URL }}" >> .env

      - name: Run README updater
        run: python readme_updater.py

      - name: Commit changes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add README.md
          git commit -m "Auto-update README [skip ci]"

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
