#!/bin/bash
# -*- coding: utf-8 -*-
# Timestamp: "2025-11-12 (ywatanabe)"
# File: ./scripts/shell/modules/add_diff_signature.sh
# Description: Add metadata signature to diff TeX files

# Logging functions (if not already defined)
if ! command -v echo_info &> /dev/null; then
    GRAY='\033[0;90m'
    GREEN='\033[0;32m'
    YELLOW='\033[0;33m'
    RED='\033[0;31m'
    NC='\033[0m'
    echo_info() { echo -e "${GRAY}INFO: $1${NC}"; }
    echo_success() { echo -e "${GREEN}SUCC: $1${NC}"; }
    echo_warning() { echo -e "${YELLOW}WARN: $1${NC}"; }
    echo_error() { echo -e "${RED}ERRO: $1${NC}"; }
fi

add_diff_signature() {
    local diff_tex_file="$1"
    local old_version="$2"
    local new_version="$3"

    if [ ! -f "$diff_tex_file" ]; then
        echo_warning "    Diff TeX file not found: $diff_tex_file"
        return 1
    fi

    echo_info "    Adding signature to diff document..."

    # Create signature block
    local signature="
%% =============================================================================
%% Diff Document Metadata (Auto-generated)
%% =============================================================================
%% Comparison: v${old_version} → v${new_version}
%% Generated: $(date '+%Y-%m-%d %H:%M:%S')
%% User: $(git config user.name 2>/dev/null || echo 'unknown') <$(git config user.email 2>/dev/null || echo 'unknown')>
%% Document: ${SCITEX_WRITER_DOC_TYPE}
%% Git commit: $(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')
%% Git branch: $(git branch --show-current 2>/dev/null || echo 'unknown')
%% =============================================================================

"

    # Add signature header at the beginning of the document (after \documentclass)
    # This will appear in the PDF as metadata and can be seen in the source

    # Find the first \begin{document} and insert signature before it
    local temp_file="${diff_tex_file}.tmp"
    awk -v sig="$signature" '
        /\\begin\{document\}/ {
            print sig
        }
        { print }
    ' "$diff_tex_file" > "$temp_file"

    mv "$temp_file" "$diff_tex_file"

    # Also add visible signature in the PDF footer (optional)
    # Add to preamble before \begin{document}
    # Use cat with heredoc to avoid AWK escape sequence issues
    local temp_file="${diff_tex_file}.tmp"

    # Find the line number of \begin{document}
    local begin_doc_line=$(grep -n '\\begin{document}' "$diff_tex_file" | head -1 | cut -d: -f1)

    if [ -n "$begin_doc_line" ]; then
        # Insert signature before \begin{document}
        {
            head -n $((begin_doc_line - 1)) "$diff_tex_file"
            cat << EOF
\\usepackage{fancyhdr}
\\usepackage{lastpage}

\\fancypagestyle{diffstyle}{
    \\fancyhf{}
    \\renewcommand{\\headrulewidth}{0.4pt}
    \\renewcommand{\\footrulewidth}{0.4pt}
    \\fancyhead[L]{\\small\\textit{Diff: v${old_version} → v${new_version}}}
    \\fancyhead[R]{\\small\\textit{${SCITEX_WRITER_DOC_TYPE}}}
    \\fancyfoot[L]{\\small Generated: $(date '+%Y-%m-%d %H:%M')}
    \\fancyfoot[C]{\\small Page \\thepage\\ of \\pageref{LastPage}}
    \\fancyfoot[R]{\\small $(git config user.name 2>/dev/null || echo 'Auto-generated')}
}

\\AtBeginDocument{\\pagestyle{diffstyle}}
EOF
            tail -n +$begin_doc_line "$diff_tex_file"
        } > "$temp_file"

        mv "$temp_file" "$diff_tex_file"
    fi

    echo_success "    Diff signature added (header + footer metadata)"
}

# Export function for use in other scripts
export -f add_diff_signature

# EOF
