#!/bin/bash
# ekg-add-note - Command-line interface for adding notes to ekg
#
# Usage: ekg-add-note --title "title" --tag tag1 --tag tag2 --note "note text" --mode org-mode
#
# This script uses emacsclient to communicate with a running Emacs daemon,
# providing fast note creation for agents and scripts.

set -e

# Default values
TAGS=()
NOTE_TEXT=""
NOTE_TITLE=""
MODE="org-mode"
DAEMON_NAME=""

# Parse command-line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --tag)
            TAGS+=("$2")
            shift 2
            ;;
        --note)
            NOTE_TEXT="$2"
            shift 2
            ;;
        --title)
            NOTE_TITLE="$2"
            shift 2
            ;;
        --mode)
            MODE="$2"
            shift 2
            ;;
        --daemon)
            DAEMON_NAME="$2"
            shift 2
            ;;
        --help)
            echo "Usage: ekg-add-note --title \"title\" --tag tag1 --tag tag2 --note \"note text\" --mode org-mode [--daemon name]"
            echo ""
            echo "Options:"
            echo "  --tag TAG       Add a tag to the note (can be specified multiple times)"
            echo "  --note TEXT     The text content of the note (required)"
            echo "  --title TEXT    The title of the note (optional, needed for org tasks)"
            echo "  --mode MODE     The major mode for the note (default: org-mode)."
            echo "                  The --note text MUST be formatted for this mode"
            echo "                  (e.g. org markup for org-mode, markdown for markdown-mode)."
            echo "  --daemon NAME   Name of the Emacs daemon to use (optional)"
            echo "  --help          Show this help message"
            echo ""
            echo "Example:"
            echo "  ekg-add-note --title 'Implementation notes' --tag 'project/ekg' --tag 'agent' --note 'Details here' --mode org-mode"
            exit 0
            ;;
        *)
            echo "Error: Unknown option $1"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

# Validate required arguments
if [[ -z "$NOTE_TEXT" ]]; then
    echo "Error: --note is required"
    echo "Use --help for usage information"
    exit 1
fi

# Title is required for org tasks
if [[ -z "$NOTE_TITLE" ]]; then
    for tag in "${TAGS[@]}"; do
        if [[ "$tag" == "org/task" ]]; then
            echo "Error: --title is required for org tasks"
            echo "Use --help for usage information"
            exit 1
        fi
    done
fi

if [[ ${#TAGS[@]} -eq 0 ]]; then
    echo "Error: At least one --tag is required"
    echo "Use --help for usage information"
    exit 1
fi

# Validate MODE against allowed values
ALLOWED_MODES=("org-mode" "markdown-mode" "text-mode")
MODE_VALID=false
for allowed_mode in "${ALLOWED_MODES[@]}"; do
    if [[ "$MODE" == "$allowed_mode" ]]; then
        MODE_VALID=true
        break
    fi
done

if [[ "$MODE_VALID" != "true" ]]; then
    echo "Error: Invalid mode '$MODE'. Must be one of: ${ALLOWED_MODES[*]}"
    echo "Use --help for usage information"
    exit 1
fi

# Build the tags list for elisp
TAGS_ELISP="("
for tag in "${TAGS[@]}"; do
    # Escape quotes in tags
    escaped_tag="${tag//\"/\\\"}"
    TAGS_ELISP+="\"$escaped_tag\" "
done
TAGS_ELISP="${TAGS_ELISP% })"

# Escape the note text for elisp
# Replace backslashes first, then quotes, then newlines
NOTE_ELISP="${NOTE_TEXT//\\/\\\\}"
NOTE_ELISP="${NOTE_ELISP//\"/\\\"}"
NOTE_ELISP="${NOTE_ELISP//$'\n'/\\n}"

# Build the elisp expression
if [[ -n "$NOTE_TITLE" ]]; then
    TITLE_ELISP="${NOTE_TITLE//\\/\\\\}"
    TITLE_ELISP="${TITLE_ELISP//\"/\\\"}"
    TITLE_ELISP="${TITLE_ELISP//$'\n'/\\n}"

    ELISP_EXPR="(progn
  (require 'ekg-agent)
  (let ((id (ekg-agent-add-note \"$NOTE_ELISP\" '$TAGS_ELISP '$MODE)))
    (let ((note (ekg-get-note-with-id id)))
      (setf (ekg-note-properties note)
            (plist-put (ekg-note-properties note) :titled/title (list \"$TITLE_ELISP\")))
      (ekg-save-note note))
    id))"
else
    ELISP_EXPR="(progn
  (require 'ekg-agent)
  (ekg-agent-add-note \"$NOTE_ELISP\" '$TAGS_ELISP '$MODE))"
fi

# Build emacsclient command
EMACSCLIENT_CMD="emacsclient"
if [[ -n "$DAEMON_NAME" ]]; then
    EMACSCLIENT_CMD+=" -s $DAEMON_NAME"
fi

# Execute via emacsclient
if ! $EMACSCLIENT_CMD --eval "$ELISP_EXPR" 2>&1; then
    echo "Error: Failed to add note. Is the Emacs daemon running?"
    echo "Start a daemon with: emacs --daemon${DAEMON_NAME:+=$DAEMON_NAME}"
    exit 1
fi
