#!/usr/bin/env bash
# Notion CLI wrapper - quick access to Notion operations

set -e

# Get the directory of this script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TOOLS_DIR="$(dirname "$SCRIPT_DIR")"

# Load environment variables from .env.keys
if [ -f "$TOOLS_DIR/.env.keys" ]; then
    export $(grep -v '^#' "$TOOLS_DIR/.env.keys" | xargs)
fi

# Show usage if no arguments
if [ $# -eq 0 ]; then
    cat << 'EOF'
Notion CLI - Quick access to Notion operations

Usage: notion <command> [options]

Commands:
  get-page <id>              Get page content
  create-page <title>        Create page under parent
  create-db                  Create database from CSV with registry
  search <query>             Search Notion workspace
  query-db <id>              Query database
  get-db <id>                Get database schema
  update-page <id>           Update page properties
  append <id>                Append content blocks
  list-teamspaces            List all accessible teamspaces/workspaces

Examples:
  notion get-page ea9e6565c8384c75a40ccfd49de0973e
  notion create-page --parent_id febbd37372d54dd9bfb844a74757c441 --title "Test" --parent_type page
  notion create-db --registry runs/xxx/notion.json --csv file.csv --parent-id ID --key partners --db-name "Partners"
  notion search "321-AI"
  notion list-teamspaces
  notion list-teamspaces --output teamspaces.json

EOF
    exit 0
fi

# Map commands to scripts
case "$1" in
    get-page)
        shift
        # Convert positional arg to --page_id if needed
        if [ $# -gt 0 ] && [[ ! "$1" =~ ^- ]]; then
            python3 "$SCRIPT_DIR/get_page.py" --page_id "$1" "${@:2}"
        else
            python3 "$SCRIPT_DIR/get_page.py" "$@"
        fi
        ;;
    create-page)
        shift
        python3 "$SCRIPT_DIR/create_page.py" "$@"
        ;;
    create-db)
        shift
        python3 "$SCRIPT_DIR/create_database_from_csv.py" "$@"
        ;;
    search)
        shift
        # Convert positional arg to --query if needed
        if [ $# -gt 0 ] && [[ ! "$1" =~ ^- ]]; then
            python3 "$SCRIPT_DIR/search.py" --query "$1" "${@:2}"
        else
            python3 "$SCRIPT_DIR/search.py" "$@"
        fi
        ;;
    query-db)
        shift
        # Convert positional arg to --database_id if needed
        if [ $# -gt 0 ] && [[ ! "$1" =~ ^- ]]; then
            python3 "$SCRIPT_DIR/query_database.py" --database_id "$1" "${@:2}"
        else
            python3 "$SCRIPT_DIR/query_database.py" "$@"
        fi
        ;;
    get-db)
        shift
        # Convert positional arg to --database_id if needed
        if [ $# -gt 0 ] && [[ ! "$1" =~ ^- ]]; then
            python3 "$SCRIPT_DIR/get_database.py" --database_id "$1" "${@:2}"
        else
            python3 "$SCRIPT_DIR/get_database.py" "$@"
        fi
        ;;
    update-page)
        shift
        # Convert positional arg to --page_id if needed
        if [ $# -gt 0 ] && [[ ! "$1" =~ ^- ]]; then
            python3 "$SCRIPT_DIR/update_page.py" --page_id "$1" "${@:2}"
        else
            python3 "$SCRIPT_DIR/update_page.py" "$@"
        fi
        ;;
    append)
        shift
        # Convert positional args to --page_id and --markdown-file if needed
        if [ $# -ge 2 ] && [[ ! "$1" =~ ^- ]] && [[ ! "$2" =~ ^- ]]; then
            # Pattern: notion append PAGE_ID FILE.md [--flags]
            python3 "$SCRIPT_DIR/append_blocks.py" --page_id "$1" --markdown-file "$2" "${@:3}"
        elif [ $# -gt 0 ] && [[ ! "$1" =~ ^- ]]; then
            # Pattern: notion append PAGE_ID --flags
            python3 "$SCRIPT_DIR/append_blocks.py" --page_id "$1" "${@:2}"
        else
            python3 "$SCRIPT_DIR/append_blocks.py" "$@"
        fi
        ;;
    list-teamspaces)
        shift
        python3 "$SCRIPT_DIR/list_teamspaces.py" "$@"
        ;;
    *)
        echo "Unknown command: $1"
        echo "Run 'notion' for help"
        exit 1
        ;;
esac
