#!/bin/bash
# Daily Changelog Generator
# Collects commits from today and formats them for Discord
# Mentions the REQUESTER (not commit author) when available

set -e

CHANGELOG_FILE="/tmp/daily-changelog-$(date +%Y-%m-%d).md"
TODAY=$(date +%Y-%m-%d)

# Discord user ID mapping (Discord username -> Discord ID)
# Full guild mapping - auto-generated 2026-02-20
declare -A DISCORD_IDS=(
  # Leadership
  ["coderaven"]="470619903855034369"
  ["albertpadin"]="404471588621123613"
  ["daveove"]="432963056247767041"
  ["geeque"]="688903298501443625"
  ["jarrhey"]="688651268146855958"
  
  # Agents Team
  ["jim.paano"]="1159411302402371665"
  ["steven.asoy"]="1394109302058389568"
  ["bunsooo"]="688657023927189565"
  
  # Design
  ["edllorada"]="272638638540587008"
  ["han.m"]="658337048087887912"
  ["hanami00"]="741292675478855814"
  
  # Engineering
  ["blurridge"]="270853184820346880"
  ["kenneth_renz"]="229247042109571072"
  ["jude3745"]="757178308264722492"
  ["nickg_06192"]="1166016070117761164"
  ["johnpaul.g"]="749575795126829127"
  ["zeejay09"]="964512173407739904"
  ["zelvillarosa"]="975651162374172712"
  ["dimful"]="998439407436693555"
  ["geoffdiaz"]="986154136632447006"
  ["cyberblaze007"]="715847730253004852"
  
  # Product/Growth
  ["jhamezzzzz1315"]="666944548269981717"
  ["antonio.telimban"]="952770506795384832"
  ["med_aduh"]="778161610329030676"
  ["lenlituy"]="688937180814376992"
  ["pauline00720"]="817359044007559219"
  ["tinmantua"]="930286873065185330"
  ["xian._.niax"]="1242310329963122740"
  ["woplas"]="236842110387617792"
  ["jhaisalcedo"]="1167380197750865930"
  ["gansukii"]="1002785173164785754"
  
  # Others
  ["jeee"]="228821430681206785"
  ["tim.ssss"]="236860572136767488"
  ["dayvough"]="269307879700299777"
  ["jjuril"]="271967098958381056"
  ["tsumugyu"]="349366707829211148"
  ["roneruu"]="354530420949254144"
  ["onlybann"]="361725097901293568"
  ["halw"]="396884184653758468"
  ["nikdelosreyes"]="396994559265079296"
  ["hestice"]="436765348105027596"
  ["jap3935"]="505222208633962496"
  ["marsascends"]="529184380099231745"
  ["shadroid"]="531834175754600458"
  ["vonyumo"]="589151270003146781"
  ["leighlaang"]="688634632815509570"
  ["michael_symph"]="688637381506760723"
  ["badidesu"]="688638131838124034"
  ["blueyonder4709"]="688901999982673936"
  ["key_perino"]="688931323171438627"
  ["nicolempadin"]="688932016179511342"
  ["van.allen"]="688932026786512930"
  ["bryant99"]="689071964526084127"
  ["maryamora"]="696015042306637845"
  ["trish0812"]="699859491646079027"
  ["francesmarie"]="701720623499444245"
  ["ralph.symph"]="709265526773383168"
  ["badg1"]="726059590671925359"
  ["ram_butan"]="736082466401681418"
  ["kneecotaco"]="744774220181143674"
  ["ian.10"]="777809117279813642"
  ["johnreyev"]="778566645429436456"
  ["itsronami"]="818200517749899316"
  ["alesna"]="836957580420186141"
  ["arcubalino"]="877927073862275132"
  ["_shinmae"]="909634648806670337"
  ["chellegray"]="910448465417478155"
  ["edsybitsy"]="990974992193847327"
  ["_louveh_"]="1001870085100671058"
  ["dlmgl_"]="1065858644085854249"
  ["dex.ilagan"]="1153715839435022416"
  ["wag.gallenero"]="1164545996018159657"
  ["vincetapdasan"]="1196811987330539601"
  ["tomgarcia19"]="1247849456058236980"
  ["benjude._.|"]="1295305060363931759"
  ["erica.sembrano"]="1325657868036345957"
  ["xflorer_symph"]="1326932682705473627"
  ["abs_011"]="1348326944776785931"
  ["christopher_symph"]="1351520287790989312"
  ["kim.tiro"]="1376461496871026760"
  ["lester.dc"]="1376952459624316958"
  ["lyragemparo"]="1379332740062777415"
  ["shanebarita"]="1409720095550214196"
  ["vitiu_work"]="1418419612797763615"
  
  # Service accounts (map to ARIA)
  ["aria"]="1467739981966737549"
  ["symphdevtools"]="1467739981966737549"
)

get_discord_mention() {
  local username="$1"
  # Normalize: lowercase, remove spaces
  local normalized=$(echo "$username" | tr '[:upper:]' '[:lower:]' | tr -d ' ')
  local discord_id="${DISCORD_IDS[$normalized]}"
  if [ -n "$discord_id" ]; then
    echo "<@$discord_id>"
  else
    echo "@$username"
  fi
}

# Function to get requester from commit body
get_requester_from_commit() {
  local repo_path="$1"
  local hash="$2"
  cd "$repo_path"
  # Look for "Requested-by: username" in commit body
  local requester=$(git log -1 --format="%b" "$hash" 2>/dev/null | grep -i "^Requested-by:" | head -1 | sed 's/^[Rr]equested-by:[[:space:]]*//')
  echo "$requester"
}

echo "# 📋 Daily Changelog — $TODAY" > "$CHANGELOG_FILE"
echo "" >> "$CHANGELOG_FILE"

HAS_CHANGES=false

# Check Symphony (symph-agency)
if [ -d "/home/aria/projects/symph-agency/.git" ]; then
  cd /home/aria/projects/symph-agency
  
  # Get commits with full info
  COMMITS=$(git log --since="$TODAY 00:00" --until="tomorrow 00:00" --format="%H" 2>/dev/null || true)
  
  if [ -n "$COMMITS" ]; then
    # Filter out deploy commits
    FILTERED=""
    for full_hash in $COMMITS; do
      msg=$(git log -1 --format="%s" "$full_hash")
      if [[ ! "$msg" =~ ^deploy: ]]; then
        FILTERED="$FILTERED $full_hash"
      fi
    done
    
    if [ -n "$(echo $FILTERED | tr -d ' ')" ]; then
      HAS_CHANGES=true
      echo "## 🎵 Symphony (Agency UI)" >> "$CHANGELOG_FILE"
      echo "" >> "$CHANGELOG_FILE"
      
      for full_hash in $FILTERED; do
        hash=$(echo "$full_hash" | cut -c1-7)
        msg=$(git log -1 --format="%s" "$full_hash")
        author=$(git log -1 --format="%an" "$full_hash")
        
        # Check for requester in commit body
        requester=$(get_requester_from_commit "/home/aria/projects/symph-agency" "$full_hash")
        
        if [ -n "$requester" ]; then
          mention=$(get_discord_mention "$requester")
        else
          # Fall back to commit author
          mention=$(get_discord_mention "$author")
        fi
        
        echo "- [\`$hash\`](https://github.com/symphco/symph-agency-ui/commit/$full_hash) — $mention — $msg" >> "$CHANGELOG_FILE"
      done
      echo "" >> "$CHANGELOG_FILE"
    fi
  fi
fi

# Check product repos
for product_dir in /home/aria/products/*/; do
  if [ ! -d "$product_dir" ]; then
    continue
  fi
  
  product_name=$(basename "$product_dir")
  
  # Check apps/main (where most app changes happen)
  if [ -d "$product_dir/apps/main/.git" ]; then
    cd "$product_dir/apps/main"
    COMMITS=$(git log --since="$TODAY 00:00" --until="tomorrow 00:00" --format="%H" 2>/dev/null || true)
    
    if [ -n "$COMMITS" ]; then
      # Filter out deploy commits
      FILTERED=""
      for full_hash in $COMMITS; do
        msg=$(git log -1 --format="%s" "$full_hash")
        if [[ ! "$msg" =~ ^deploy: ]]; then
          FILTERED="$FILTERED $full_hash"
        fi
      done
      
      if [ -n "$(echo $FILTERED | tr -d ' ')" ]; then
        HAS_CHANGES=true
        REMOTE=$(git remote get-url origin 2>/dev/null | sed 's/git@github.com:/https:\/\/github.com\//' | sed 's/\.git$//' || echo "")
        
        echo "## 📦 $product_name" >> "$CHANGELOG_FILE"
        echo "" >> "$CHANGELOG_FILE"
        
        for full_hash in $FILTERED; do
          hash=$(echo "$full_hash" | cut -c1-7)
          msg=$(git log -1 --format="%s" "$full_hash")
          author=$(git log -1 --format="%an" "$full_hash")
          
          # Check for requester
          requester=$(get_requester_from_commit "$product_dir/apps/main" "$full_hash")
          
          if [ -n "$requester" ]; then
            mention=$(get_discord_mention "$requester")
          else
            mention=$(get_discord_mention "$author")
          fi
          
          if [ -n "$REMOTE" ]; then
            echo "- [\`$hash\`]($REMOTE/commit/$full_hash) — $mention — $msg" >> "$CHANGELOG_FILE"
          else
            echo "- \`$hash\` — $mention — $msg" >> "$CHANGELOG_FILE"
          fi
        done
        echo "" >> "$CHANGELOG_FILE"
      fi
    fi
  fi
done

# Check if we have any content
if [ "$HAS_CHANGES" = false ]; then
  echo "_No feature commits found for $TODAY (deploy commits excluded)_" >> "$CHANGELOG_FILE"
fi

echo "$CHANGELOG_FILE"
