#!/bin/bash

# Check if GITHUB_USER_NAME and REPO_FULL_NAME are provided
if [ -z "$1" ] || [ -z "$2" ]; then
  echo "Usage: $0 <GITHUB_USER_NAME> <REPO_FULL_NAME>"
  echo "Example: $0 DARKenergem saravanakumardb/vv-note-mcp-agent-intern-naman"
  exit 1
fi

GITHUB_USER_NAME="$1"
REPO_FULL_NAME="$2"
ACCOUNTS_FILE="accounts.json"

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

# Use the first account in accounts.json for authentication
USER=$(jq -r '.[0].user' "$ACCOUNTS_FILE")
TOKEN=$(jq -r '.[0].token' "$ACCOUNTS_FILE")

echo "Checking access for $GITHUB_USER_NAME on repo $REPO_FULL_NAME using account $USER"

# Get collaborators for the repo
COLLABORATORS=$(curl -s -H "Authorization: token $TOKEN" "https://api.github.com/repos/$REPO_FULL_NAME/collaborators" | jq -r '.[].login')

if echo "$COLLABORATORS" | grep -q "$GITHUB_USER_NAME"; then
  echo "  - $GITHUB_USER_NAME has access to $REPO_FULL_NAME"
else
  echo "  - $GITHUB_USER_NAME does not have access to $REPO_FULL_NAME"
fi
