#!/usr/bin/env bash
set -euo pipefail

###############################################################################
# DWRITE — dmenu content opener for krisyotam.com
#
# Maintainer:   Kris Yotam <krisyotam@pm.me>
# License:      MIT
# Created:      2026-02-15
# Updated:      2026-02-22
# Description:  Select content type and file via dmenu, open in Typora.
#               TIL and Now entries open $EDITOR for markdown writing, then
#               auto-create the entry via generateMetadata.js on save.
###############################################################################

CONTENT_DIR="$HOME/content"
PROJECT_DIR="$HOME/dev/krisyotam.com"
GEN_META="$PROJECT_DIR/public/scripts/keep/generateMetadata.js"

# Content types with nerd font icons
declare -A TYPE_ICONS=(
    [blog]="󰬞"
    [diary]="󰃮"
    [essays]="󰬝"
    [fiction]="󰂺"
    [news]="󰎕"
    [notes]="󰏪"
    [ocs]="󰏫"
    [papers]="󰈙"
    [progymnasmata]="󰑴"
    [reviews]="󰓥"
    [verse]="󱗝"
    [til]="󰛄"
    [now]="󰔛"
)

CONTENT_TYPES=(blog diary essays fiction news notes ocs papers progymnasmata reviews verse til now)

# Select content type
type_menu() {
    local entries=""
    for t in "${CONTENT_TYPES[@]}"; do
        entries+="${TYPE_ICONS[$t]:-󰈙}  $t"$'\n'
    done
    echo "$entries" | dmenu -i -l 13 -p "write:" | awk '{print $2}'
}

# Select file from content type
file_menu() {
    local type="$1"
    local dir="$CONTENT_DIR/$type"
    [[ ! -d "$dir" ]] && exit 1

    ls "$dir"/*.mdx 2>/dev/null |
        xargs -I{} basename {} .mdx |
        sort |
        dmenu -i -l 20 -p "$type:"
}

# Write a TIL entry
write_til() {
    local tmpfile
    tmpfile="$(mktemp /tmp/dwrite-til-XXXXXX.md)"

    # Prompt for optional title
    local title
    title="$(echo "" | dmenu -p "TIL title (optional):" 2>/dev/null)" || true

    # Auto-generate title from date if skipped
    if [[ -z "$title" ]]; then
        title="$(date +'%B %d, %Y')"
    fi

    # Open editor
    ${EDITOR:-nano} "$tmpfile"

    # Check if user wrote anything
    if [[ ! -s "$tmpfile" ]]; then
        rm -f "$tmpfile"
        notify-send "dwrite" "TIL entry cancelled (empty file)" 2>/dev/null || true
        exit 0
    fi

    # Create entry via generateMetadata.js
    node "$GEN_META" \
        --type til \
        --title "$title" \
        --content-file "$tmpfile" \
        --status "Finished" \
        --certainty "certain" \
        --importance 9 \
        --category "til" \
        --state "active"

    rm -f "$tmpfile"
    notify-send "dwrite" "TIL entry created: $title" 2>/dev/null || true
}

# Write a Now entry
write_now() {
    local tmpfile
    tmpfile="$(mktemp /tmp/dwrite-now-XXXXXX.md)"

    # Auto-generate title from month
    local title
    title="$(date +'%B %Y')"

    # Open editor
    ${EDITOR:-nano} "$tmpfile"

    # Check if user wrote anything
    if [[ ! -s "$tmpfile" ]]; then
        rm -f "$tmpfile"
        notify-send "dwrite" "Now entry cancelled (empty file)" 2>/dev/null || true
        exit 0
    fi

    # Create entry via generateMetadata.js
    node "$GEN_META" \
        --type now \
        --title "$title" \
        --content-file "$tmpfile" \
        --status "Finished" \
        --certainty "certain" \
        --importance 9 \
        --category "now" \
        --state "active"

    rm -f "$tmpfile"
    notify-send "dwrite" "Now entry created: $title" 2>/dev/null || true
}

# Main
type="$(type_menu)"
[[ -z "$type" ]] && exit 0

case "$type" in
    til)
        write_til
        ;;
    now)
        write_now
        ;;
    *)
        file="$(file_menu "$type")"
        [[ -z "$file" ]] && exit 0
        typora "$CONTENT_DIR/$type/$file.mdx" >/dev/null 2>&1 & disown
        ;;
esac
