name: Export Repo Traffic to Google Sheets

on:
  schedule:
    - cron: '0 2 * * *'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install gspread oauth2client requests

      # --- NEW STEP TO DECODE THE SECRET ---
      - name: Write Google credentials to a file
        id: write_creds
        uses: timheuer/base64-to-file@v1.2
        with:
          fileName: 'google-credentials.json'
          encodedString: ${{ secrets.GOOGLE_SHEETS_CREDENTIALS }}

      # --- MODIFIED STEP TO RUN THE SCRIPT ---
      - name: Run the Python script
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
          # Point the environment variable to the file path created in the previous step
          GOOGLE_SHEETS_CREDENTIALS: ${{ steps.write_creds.outputs.filePath }}
        run: python .github/scripts/export_traffic.py
