name: Update Contributors

on:
  schedule:
    - cron: '0 3 * * *'
  workflow_dispatch: # Allow manual trigger

jobs:
  update-contributors:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0 # Full history needed for commit deduplication
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.x'

      - name: Configure Git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Run contributor generation script
        env:
          PROJECT_DIR: ${{ github.workspace }}
        run: |
          chmod +x scripts/generate-contributors.sh
          ./scripts/generate-contributors.sh

      - name: Push changes
        run: |
          git push origin main --force-with-lease

      - name: Sync to Hugging Face
        env:
          HF_API_TOKEN: ${{ secrets.HF_API_TOKEN }}
        run: |
          pip install huggingface_hub
          python3 << 'EOF'
          import os
          from huggingface_hub import HfApi
          
          api = HfApi(token=os.environ["HF_API_TOKEN"])
          api.upload_file(
              path_or_fileobj="prompts.csv",
              path_in_repo="prompts.csv",
              repo_id="fka/prompts.chat",
              repo_type="dataset",
              commit_message="Update prompts.csv"
          )
          print("Successfully synced prompts.csv to Hugging Face")
          EOF
