#!/bin/bash

ACCOUNTS_FILE="accounts.json"
OUTPUT_DIR="github_repo_scanners/user_repos"

# Check if accounts.json exists
if [ ! -f "$ACCOUNTS_FILE" ]; then
  echo "Error: $ACCOUNTS_FILE not found."
  exit 1
fi

mkdir -p "$OUTPUT_DIR"

# Loop through each account in accounts.json
jq -c '.[]' "$ACCOUNTS_FILE" | while read -r account; do
  USER=$(echo "$account" | jq -r '.user')
  TOKEN=$(echo "$account" | jq -r '.token')

  echo "Fetching repos for account: $USER"

  # Get all repos the user has access to (owner, collaborator, org member)
  # and format the output as a JSON array.
  curl -s -H "Authorization: token $TOKEN" "https://api.github.com/user/repos?type=all&per_page=100" | \
jq -r '[.[] | .full_name]' > "${OUTPUT_DIR}/${USER}.json"

  echo "Successfully created ${OUTPUT_DIR}/${USER}.json"
done

echo "All user repository lists have been created in '${OUTPUT_DIR}'."
