#!/usr/bin/env bash
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: MIT

# generate.sh
# Generates a PR reference file with commit history and full diff.
# The output XML is consumed by GitHub Copilot to produce accurate PR descriptions.
#
# Compares the current branch with a specified base branch (default: main)
# and writes an XML document containing commit history and diff information.

set -euo pipefail

# Display usage information
show_usage() {
  echo "Usage: ${0##*/} [OPTIONS]"
  echo ""
  echo "Options:"
  echo "  --no-md-diff       Exclude markdown files (*.md) from the diff output"
  echo "  --base-branch      Specify the base branch to compare against (default: main)"
  echo "  --output           Specify output file path (default: .copilot-tracking/pr/pr-reference.xml)"
  echo "  --help, -h         Show this help message"
  exit 1
}

# Get the repository root directory
REPO_ROOT=$(git rev-parse --show-toplevel)

# Process command line arguments
NO_MD_DIFF=false
BASE_BRANCH="origin/main"
OUTPUT_FILE="${REPO_ROOT}/.copilot-tracking/pr/pr-reference.xml"
while [[ $# -gt 0 ]]; do
  case "$1" in
    --no-md-diff)
      NO_MD_DIFF=true
      shift
      ;;
    --base-branch)
      if [[ -z "${2:-}" || "$2" == --* ]]; then
        echo "Error: --base-branch requires an argument" >&2
        show_usage
      fi
      BASE_BRANCH="$2"
      shift 2
      ;;
    --output)
      if [[ -z "${2:-}" || "$2" == --* ]]; then
        echo "Error: --output requires an argument" >&2
        show_usage
      fi
      OUTPUT_FILE="$2"
      shift 2
      ;;
    --help|-h)
      show_usage
      ;;
    *)
      echo "Unknown option: $1" >&2
      show_usage
      ;;
  esac
done

# Verify the base branch exists
if ! git rev-parse --verify "${BASE_BRANCH}" &>/dev/null; then
  echo "Error: Branch '${BASE_BRANCH}' does not exist or is not accessible" >&2
  exit 1
fi

# Set output file path and ensure parent directory exists
PR_REF_FILE="${OUTPUT_FILE}"
mkdir -p "$(dirname "${PR_REF_FILE}")"

# Create the reference file with commit history using XML tags
{
  echo "<commit_history>"
  echo "  <current_branch>"
  git --no-pager branch --show-current
  echo "  </current_branch>"
  echo ""

  echo "  <base_branch>"
  echo "    ${BASE_BRANCH}"
  echo "  </base_branch>"
  echo ""

  echo "  <commits>"
  # Output commit information including subject and body
  git --no-pager log --pretty=format:"<commit hash=\"%h\" date=\"%cd\"><message><subject><\![CDATA[%s]]></subject><body><\![CDATA[%b]]></body></message></commit>" --date=short "${BASE_BRANCH}"..HEAD
  echo "  </commits>"
  echo ""

  # Add the full diff, excluding specified files
  echo "  <full_diff>"
  if [[ "${NO_MD_DIFF}" == "true" ]]; then
    git --no-pager diff "${BASE_BRANCH}" -- ':!*.md'
  else
    git --no-pager diff "${BASE_BRANCH}"
  fi
  echo "  </full_diff>"
  echo "</commit_history>"
} >"${PR_REF_FILE}"

LINE_COUNT=$(wc -l <"${PR_REF_FILE}" | awk '{print $1}')

echo "Created ${PR_REF_FILE}"
if [[ "${NO_MD_DIFF}" == "true" ]]; then
  echo "Note: Markdown files were excluded from diff output"
fi
echo "Lines: ${LINE_COUNT}"
echo "Base branch: ${BASE_BRANCH}"
echo "File name: ${PR_REF_FILE}"
