#!/bin/bash

# Colors for output
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NOCOLOR='\033[0m'

# Get the directory where this hook is located
HOOK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Go up from .git/hooks/ to repo root
REPO_ROOT="$(cd "$HOOK_DIR/../.." && pwd)"
CHANGELOG_SCRIPT="$REPO_ROOT/scripts/update_changelog.sh"
PREVIEW_FILE="$REPO_ROOT/.changelog_preview.tmp"
VERSION_FILE="$REPO_ROOT/.changelog_version.tmp"

# Only run changelog update if there are actual code changes (not just changelog files)
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
if [ -z "$STAGED_FILES" ]; then
  # No staged files, exit
  exit 0
fi

# Check if only CHANGELOG.md and/or Makefile are being committed
OTHER_FILES=$(echo "$STAGED_FILES" | grep -v "^CHANGELOG.md$" | grep -v "^Makefile$")
if [ -z "$OTHER_FILES" ]; then
  # Only changelog files are being committed, skip update
  exit 0
fi

# Check for skip flag
# To skip changelog generation, set SKIP_CHANGELOG=1 before committing:
#   SKIP_CHANGELOG=1 git commit -m "your message"
#   SKIP_CHANGELOG=1 git commit
if [ "$SKIP_CHANGELOG" = "1" ] || [ "$SKIP_CHANGELOG" = "true" ]; then
  echo -e "${YELLOW}Skipping changelog update (SKIP_CHANGELOG is set)${NOCOLOR}"
  exit 0
fi

# Update changelog before commit
if [ -f "$CHANGELOG_SCRIPT" ]; then
  echo -e "\n${CYAN}Updating changelog...${NOCOLOR}"
  
  # Set environment variable to indicate we're running from pre-commit
  export CHANGELOG_CONTEXT=pre-commit
  
  bash "$CHANGELOG_SCRIPT"
  changelog_status=$?
  if [ $changelog_status -ne 0 ]; then
    echo -e "${RED}Commit aborted: changelog update failed.${NOCOLOR}"
    exit 1
  fi
  
  # Show preview if changelog was updated
  if [ -f "$PREVIEW_FILE" ] && [ -f "$VERSION_FILE" ]; then
    NEW_VERSION=$(cat "$VERSION_FILE")
    PREVIEW_CONTENT=$(cat "$PREVIEW_FILE")
    
    echo ""
    echo -e "${BLUE}========================================================================${NOCOLOR}"
    echo -e "${CYAN}                    CHANGELOG PREVIEW${NOCOLOR}"
    echo -e "${BLUE}========================================================================${NOCOLOR}"
    echo ""
    echo -e "${GREEN}New Version: ${YELLOW}$NEW_VERSION${NOCOLOR}"
    echo ""
    echo -e "${CYAN}Changelog Entry:${NOCOLOR}"
    echo -e "${BLUE}────────────────────────────────────────────────────────────────────────${NOCOLOR}"
    echo -e "$PREVIEW_CONTENT"
    echo -e "${BLUE}────────────────────────────────────────────────────────────────────────${NOCOLOR}"
    echo ""
    echo -e "${YELLOW}Do you want to proceed with the commit? (yes/no):${NOCOLOR} "
    # Read from /dev/tty to ensure we can read from terminal even in git hook context
    read -r confirmation < /dev/tty
    
    if [ "$confirmation" != "yes" ]; then
      echo -e "${RED}Commit aborted by user.${NOCOLOR}"
      echo -e "${YELLOW}To revert changes, run:${NOCOLOR}"
      echo -e "  git checkout CHANGELOG.md Makefile"
      # Clean up temp files
      rm -f "$PREVIEW_FILE" "$VERSION_FILE"
      exit 1
    fi
    
    echo -e "${GREEN}Proceeding with commit...${NOCOLOR}"
    
    # Add the updated CHANGELOG.md and Makefile to the current commit
    echo -e "${CYAN}Staging CHANGELOG.md and Makefile...${NOCOLOR}"
    git add CHANGELOG.md Makefile
    
    # Clean up temp files
    rm -f "$PREVIEW_FILE" "$VERSION_FILE"
  fi
else
  echo -e "${YELLOW}Warning: changelog update script not found at $CHANGELOG_SCRIPT${NOCOLOR}"
fi

