name: JobSpy Scraper Dynamic Workflow

on:
  workflow_dispatch:
    inputs:
      user_email:
        description: 'Email of user'
        required: true
      run_id:
        description: 'Run ID from Power Automate'
        required: true

permissions:
  contents: read
  id-token: write

jobs:
  scrape_jobs:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repo
        uses: actions/checkout@v3

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

      - name: Sanitize Email
        id: vars
        run: |
          raw_email="${{ github.event.inputs.user_email }}"
          safe_email=$(echo "$raw_email" | sed 's/@/_at_/g; s/\./_/g')
          echo "safe_email=$safe_email" >> $GITHUB_OUTPUT

      - name: Run Job Scraper
        run: |
          python job_scraper_dynamic.py "${{ github.event.inputs.user_email }}" "${{ github.event.inputs.run_id }}"

      - name: Upload Output Artifact
        uses: actions/upload-artifact@v4
        with:
          name: jobspy_output_${{ steps.vars.outputs.safe_email }}_${{ github.event.inputs.run_id }}
          path: outputs/jobspy_output_${{ steps.vars.outputs.safe_email }}_${{ github.event.inputs.run_id }}.csv