#!/bin/bash
# Test Claude Code with LaoZhang API
#
# Usage:
#   ./claude_laozhang.sh                    # Interactive mode with Opus Thinking (default)
#   ./claude_laozhang.sh --thinking         # Opus 4.5 with thinking (default)
#   ./claude_laozhang.sh --opus             # Opus 4.5 (no thinking)
#   ./claude_laozhang.sh --sonnet           # Sonnet 4
#   ./test_claude_laozhang.sh --haiku            # Haiku 4.5 (cheapest)
#   ./test_claude_laozhang.sh --model <model_id> # Custom model
#   ./test_claude_laozhang.sh --haiku -p "prompt" # Non-interactive with prompt
#
# Models available via LaoZhang Claude Code group:
#   claude-opus-4-5-20251101           - Opus 4.5 (most powerful)
#   claude-opus-4-5-20251101-thinking  - Opus 4.5 with chain-of-thought
#   claude-sonnet-4-5-20250929         - Sonnet 4.5
#   claude-sonnet-4-20250514           - Sonnet 4 (good for coding)
#   claude-3-7-sonnet-20250219         - Claude 3.7 Sonnet
#   claude-haiku-4-5-20251001          - Haiku 4.5 (fast & cheap)

set -e

# Load API key from .env
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "$SCRIPT_DIR/../.env" ]; then
    export $(grep -E '^LAOZHANG_KEY=' "$SCRIPT_DIR/../.env" | xargs)
fi

if [ -z "$LAOZHANG_KEY" ]; then
    echo "Error: LAOZHANG_KEY not found in .env"
    exit 1
fi

# Default to Sonnet 4.5 - best quality/cost ratio via LaoZhang
# (Can override with --opus, --haiku, etc.)
MODEL="claude-sonnet-4-5-20250929"

# Collect remaining args for claude
CLAUDE_ARGS=()

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --thinking)
            MODEL="claude-opus-4-5-20251101-thinking"
            shift
            ;;
        --opus)
            MODEL="claude-opus-4-5-20251101"
            shift
            ;;
        --sonnet)
            MODEL="claude-sonnet-4-20250514"
            shift
            ;;
        --sonnet45)
            MODEL="claude-sonnet-4-5-20250929"
            shift
            ;;
        --haiku)
            MODEL="claude-haiku-4-5-20251001"
            shift
            ;;
        --model)
            MODEL="$2"
            shift 2
            ;;
        --list)
            echo "Available models:"
            echo "  claude-opus-4-5-20251101-thinking  - Opus 4.5 with thinking (DEFAULT)"
            echo "  claude-opus-4-5-20251101           - Opus 4.5 (no thinking)"
            echo "  claude-sonnet-4-5-20250929         - Sonnet 4.5"
            echo "  claude-sonnet-4-20250514           - Sonnet 4"
            echo "  claude-3-7-sonnet-20250219         - Claude 3.7 Sonnet"
            echo "  claude-haiku-4-5-20251001          - Haiku 4.5 (fast & cheap)"
            exit 0
            ;;
        --help|-h)
            echo "Usage: $0 [--thinking|--opus|--sonnet|--sonnet45|--haiku|--model <id>] [claude args...]"
            echo ""
            echo "Model options:"
            echo "  --thinking   Use Opus 4.5 with thinking (DEFAULT)"
            echo "  --opus       Use Opus 4.5 (no thinking)"
            echo "  --sonnet     Use Sonnet 4"
            echo "  --sonnet45   Use Sonnet 4.5"
            echo "  --haiku      Use Haiku 4.5 (fast & cheap)"
            echo "  --model <id> Use custom model ID"
            echo "  --list       List available models"
            echo ""
            echo "All other arguments are passed to claude code."
            echo "Example: $0 --haiku -p \"What is 2+2?\""
            exit 0
            ;;
        *)
            # Pass through to claude
            CLAUDE_ARGS+=("$1")
            shift
            ;;
    esac
done

echo "=========================================="
echo "Claude Code via LaoZhang API"
echo "=========================================="
if [ -n "$MODEL" ]; then
    echo "Model: $MODEL"
else
    echo "Model: (using Claude default)"
fi
echo "API Base: https://api.laozhang.ai"
echo "=========================================="
echo ""

# Set environment for Claude Code
export ANTHROPIC_BASE_URL="https://api.laozhang.ai"
export ANTHROPIC_AUTH_TOKEN="$LAOZHANG_KEY"
if [ -n "$MODEL" ]; then
    export ANTHROPIC_MODEL="$MODEL"
fi

# Disable non-essential traffic (stats, telemetry)
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC="1"

# Run Claude Code (use full path for service environment)
CLAUDE_BIN="${HOME}/.local/bin/claude"
if [ ! -x "$CLAUDE_BIN" ]; then
    CLAUDE_BIN="claude"  # fallback to PATH
fi

# Enable debug logging to capture errors
# Debug log goes to /tmp/claude-debug-{timestamp}.log
DEBUG_LOG="/tmp/claude-debug-$(date +%s).log"
exec "$CLAUDE_BIN" --debug-file "$DEBUG_LOG" "${CLAUDE_ARGS[@]}"
