#!/usr/bin/env bash

# Author: Santhosh Siva
# Date Created: 12-02-2025

# Description:
# A script to discard the last commit with optional force mode.

# Resolve script directory (works with symlinks for npm global install)
SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
  DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
source "$SCRIPT_DIR/utils"

# Default Values
force_mode=false

set_flags() {
	while [ $# -gt 0 ]; do
		case "$1" in
		-h | --help)
			echo "g-dlc - discard the last commit"
			echo " "
			echo "g-dlc [options]"
			echo " "
			echo "options:"
			echo "-h, --help      show brief help"
			echo "-f, --force     force discard without checking uncommitted changes"
			exit 0
			;;
		-f=* | --force=*)
			if [ "${1#*=}" = "true" ]; then
				force_mode=true
			fi
			;;
		-f | --force)
			shift
			if [ $# -gt 0 ] && [ "$1" = "true" ]; then
				force_mode=true
			else
				# If no argument or not 'true', treat -f as a flag
				force_mode=true
				continue
			fi
			;;
		*)
			echo ""
			echo "${RED}Unknown option:${NC} $1"
			exit 1
			;;
		esac
		shift
	done
}

prompt_user_confirmation() {
	local step_number=1
	if [ "$force_mode" = "true" ]; then
		confirm=$(prompt_user "false" "Are you sure you want to discard the last commit? (this action cannot be undone)" $step_number)
	else
		confirm=$(prompt_user "false" "Are you sure you want to discard the last commit?" $step_number)
	fi

	if [ "$confirm" = "n" ]; then
		print_message "${RED}Operation cancelled by user.${NC}" 0
		exit 0
	fi
}

verify_no_uncommitted_changes() {
	if [ "$force_mode" = "false" ]; then
		print_message "${BLUE}Checking for uncommitted changes...${NC}" 2
		if has_uncommitted_changes "."; then
			print_message "" -1
			print_message "${RED}Error: There are uncommitted changes in your working directory.${NC}" -1
			print_message "${RED}Please commit or stash your changes before discarding the last commit.${NC}" -1
			exit 1
		fi
		print_message "${GREEN}No uncommitted changes found.${NC}" 0
	fi
}

discard_last_commit() {
	print_message "${BLUE}Discarding the last commit...${NC}" 3

	if [ "$force_mode" = "true" ]; then
		reset_output=$(git -c color.ui=always reset --hard --no-recurse-submodules HEAD~1 2>&1)
	else
		reset_output=$(git -c color.ui=always reset --soft --no-recurse-submodules HEAD~1 2>&1)
	fi
	reset_exit_code=$?

	if [ -n "$reset_output" ]; then
		echo "$reset_output" | indent
	fi
	if [ $reset_exit_code -ne 0 ]; then
		print_message "" -1
		print_message "${RED}Failed to discard the last commit. [Fail]${NC}" -1
		exit 1
	fi

	print_message "${GREEN}Last commit discarded successfully.${NC}" 0
}

main() {
	set_flags "$@"
	validate_dependencies git figlet lolcat
	print_banner

	prompt_user_confirmation
	verify_no_uncommitted_changes
	discard_last_commit
}


# Only run main if script is executed directly (not sourced)
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
	main "$@"
	exit 0
fi
