#!/bin/bash
set -e

# Configuration
REPO="${GITHUB_REPOSITORY:-cashubtc/cdk}"
DAYS_BACK="${DAYS_BACK:-7}"
MEETING_LINK="https://meet.fulmo.org/cdk-dev"
OUTPUT_DIR="meetings"

# Calculate date range (last 7 days)
SINCE_DATE=$(date -d "$DAYS_BACK days ago" -u +"%Y-%m-%dT%H:%M:%SZ")
MEETING_DATE=$(date -u +"%b %d %Y 15:00 UTC")
FILE_DATE=$(date -u +"%Y-%m-%d")

echo "Generating meeting agenda for $MEETING_DATE"
echo "Fetching data since $SINCE_DATE"

# Function to format PR/issue list
format_list() {
  local items="$1"
  if [ -z "$items" ]; then
    echo "- None"
  else
    echo "$items" | while IFS=$'\t' read -r number title url; do
      echo "- [#$number]($url) - $title"
    done
  fi
}

PR_TYPES=("feat" "fix" "refactor" "chore" "docs" "ci" "test" "backport")
EXCLUDE_PATTERNS=(
  "^\\[Backport"
  "Weekly Meeting Agenda"
)

filter_all_types() {
  local prs="$1"
  local result=""
  local combined_regex=""

  for type in "${PR_TYPES[@]}"; do
    if [ "$type" = "backport" ]; then
      # backports are explicitly done in another section.
      # They still need to be filtered out of the the others grouping.
      regex="^\\[Backport"
      combined_regex+="^$type|"
      continue
    else
      regex="^$type"
      combined_regex+="^$type|"
    fi
    filtered=$(echo "$prs" | jq -r --arg regex "$regex" '
      .[] | select(.title | ltrimstr(" ") | rtrimstr(" ") | test($regex; "i")) | [.number, .title, .url] | @tsv
    ')
    if [[ -n "$filtered" ]]; then
      result+="$filtered"$'\n'
    fi
  done
  combined_regex="${combined_regex%|}"

  # Build exclusion regex from EXCLUDE_PATTERNS
  local exclude_regex=$(
    IFS="|"
    echo "${EXCLUDE_PATTERNS[*]}"
  )

  other=$(echo "$prs" | jq -r --arg combined_regex "$combined_regex" --arg exclude_regex "$exclude_regex" '
  .[] | select((.title | ltrimstr(" ") | rtrimstr(" ") | test($combined_regex; "i") | not) and (.title | ltrimstr(" ") | rtrimstr(" ") | test($exclude_regex; "i") | not)) | [.number, .title, .url] | @tsv
  ')
  # other=$(echo "$prs" | jq -r --arg combined_regex "$combined_regex" '
  #   .[] | select(.title | test($combined_regex; "i") | not) | [.number, .title, .url] | @tsv
  # ')

  if [[ -n "$other" ]]; then
    result+="$other"$'\n'
  fi
  echo "$result"
}

# Fetch merged PRs
echo "Fetching merged PRs..."
MERGED_PRS=$(gh pr list \
  --repo "$REPO" \
  --state merged \
  --search "merged:>=$SINCE_DATE" \
  --json number,title,url \
  2>/dev/null || echo "")

MERGED_PRS_FILTERED=$(filter_all_types "$MERGED_PRS")
BACKPORT_PRS=$(echo "$MERGED_PRS" | jq -r '.[] | select(.title | test("^\\[Backport"; "i")) | [.number, .title, .url] | @tsv')

# Fetch recently active PRs (updated in last week, but not newly created)
echo "Fetching recently active PRs..."
RECENTLY_ACTIVE_PRS=$(gh pr list \
  --repo "$REPO" \
  --state open \
  --search "updated:>=$SINCE_DATE -created:>=$SINCE_DATE" \
  --json number,title,url \
  2>/dev/null || echo "")

RECENTLY_ACTIVE_PRS_FILTERED=$(filter_all_types "$RECENTLY_ACTIVE_PRS")

# Fetch new PRs (opened in the last week)
echo "Fetching new PRs..."
NEW_PRS=$(gh pr list \
  --repo "$REPO" \
  --state open \
  --search "created:>=$SINCE_DATE" \
  --json number,title,url \
  2>/dev/null || echo "")

NEW_PRS_FILTERED=$(filter_all_types "$NEW_PRS")

# Fetch new issues
echo "Fetching new issues..."
NEW_ISSUES=$(gh issue list \
  --repo "$REPO" \
  --state open \
  --search "created:>=$SINCE_DATE" \
  --json number,title,url \
  --jq '.[] | [.number, .title, .url] | @tsv' \
  2>/dev/null || echo "")

# Fetch discussion items (labeled with meeting-discussion)
echo "Fetching discussion items..."
DISCUSSION_PRS=$(gh pr list \
  --repo "$REPO" \
  --state open \
  --label "meeting-discussion" \
  --json number,title,url \
  --jq '.[] | [.number, .title, .url] | @tsv' \
  2>/dev/null || echo "")

DISCUSSION_ISSUES=$(gh issue list \
  --repo "$REPO" \
  --state open \
  --label "meeting-discussion" \
  --json number,title,url \
  --jq '.[] | [.number, .title, .url] | @tsv' \
  2>/dev/null || echo "")

# Combine discussion items (PRs and issues)
DISCUSSION_ITEMS=$(printf "%s\n%s" "$DISCUSSION_PRS" "$DISCUSSION_ISSUES" | grep -v '^$' || echo "")

echo "Fetching merged PRs from the nuts repo..."
MERGED_NUTS=$(gh pr list \
  --repo cashubtc/nuts \
  --state merged \
  --search "merged:>=$SINCE_DATE" \
  --json number,title,url \
  --jq '.[] | [.number, .title, .url] | @tsv' \
  2>/dev/null || echo "")

# Generate markdown
AGENDA=$(
  cat <<EOF
# CDK Development Meeting

$MEETING_DATE

Meeting Link: $MEETING_LINK

## Merged
$(format_list "$MERGED_PRS_FILTERED")

## New

### Issues

$(format_list "$NEW_ISSUES")

### PRs

$(format_list "$NEW_PRS_FILTERED")

## Recently Active

$(format_list "$RECENTLY_ACTIVE_PRS_FILTERED")

## Merged NUTS

$(format_list "$MERGED_NUTS")

## Backports

$(format_list "$BACKPORT_PRS")

## Discussion

$(format_list "$DISCUSSION_ITEMS")
EOF
)

echo "$AGENDA"

# Output to file if requested
if [ "${OUTPUT_TO_FILE:-true}" = "true" ]; then
  mkdir -p "$OUTPUT_DIR"
  OUTPUT_FILE="$OUTPUT_DIR/$FILE_DATE-agenda.md"
  echo "$AGENDA" >"$OUTPUT_FILE"
  echo "Agenda saved to $OUTPUT_FILE"
fi

# Create GitHub Discussion if requested
if [ "${CREATE_DISCUSSION:-false}" = "true" ]; then
  echo "Creating GitHub discussion..."
  DISCUSSION_TITLE="CDK Dev Meeting - $MEETING_DATE"

  # Note: gh CLI doesn't have direct discussion creation yet, so we'd need to use the API
  # For now, we'll just output instructions
  echo "To create discussion manually, use the GitHub web interface or API"
  echo "Title: $DISCUSSION_TITLE"
fi

# Output for GitHub Actions
if [ -n "$GITHUB_OUTPUT" ]; then
  echo "agenda_file=$OUTPUT_FILE" >>"$GITHUB_OUTPUT"
  echo "meeting_date=$MEETING_DATE" >>"$GITHUB_OUTPUT"
fi
