#!/bin/bash

# Drama Ling 專案命令別名設置腳本
# 將常用命令設置為全域可用

PROJECT_PATH="/Users/jettcheng1018/code/dramaling-app"
SHELL_CONFIG=""

# 檢測 shell 類型
if [ -n "$ZSH_VERSION" ]; then
    SHELL_CONFIG="$HOME/.zshrc"
elif [ -n "$BASH_VERSION" ]; then
    SHELL_CONFIG="$HOME/.bash_profile"
else
    echo "無法檢測 shell 類型，請手動設置"
    exit 1
fi

echo "🔧 設置 Drama Ling 專案命令別名"
echo "================================="
echo "Shell 設定檔: $SHELL_CONFIG"
echo "專案路徑: $PROJECT_PATH"
echo ""

# 檢查設定檔是否存在
if [ ! -f "$SHELL_CONFIG" ]; then
    touch "$SHELL_CONFIG"
fi

# 建立別名設定
ALIASES="
# Drama Ling 專案別名 (自動生成 - $(date))
alias dl='cd $PROJECT_PATH && ./drama'
alias dl-issue='cd $PROJECT_PATH && ./drama issue'
alias dl-check='cd $PROJECT_PATH && ./drama check' 
alias dl-report='cd $PROJECT_PATH && ./drama report'
alias dl-consistency='cd $PROJECT_PATH && ./drama consistency'
alias dl-all='cd $PROJECT_PATH && ./drama all'
alias dl-cd='cd $PROJECT_PATH'
"

# 檢查是否已經有設定
if grep -q "Drama Ling 專案別名" "$SHELL_CONFIG"; then
    echo "⚠️ 發現已有別名設定"
    read -p "是否要更新別名設定? (y/N): " update_choice
    
    if [[ $update_choice =~ ^[Yy]$ ]]; then
        # 移除舊設定
        sed -i '' '/# Drama Ling 專案別名/,/^$/d' "$SHELL_CONFIG"
        echo "$ALIASES" >> "$SHELL_CONFIG"
        echo "✅ 別名設定已更新"
    else
        echo "❌ 取消更新"
        exit 0
    fi
else
    # 新增設定
    echo "$ALIASES" >> "$SHELL_CONFIG"
    echo "✅ 別名設定已新增"
fi

echo ""
echo "🎯 可用的命令別名:"
echo "  dl          - 🎭 主入口點（顯示選單）"
echo "  dl-issue    - 📝 記錄/管理問題"
echo "  dl-check    - 📊 查看問題狀態"
echo "  dl-report   - 📊 建立分析報告" 
echo "  dl-consistency - 🔍 執行一致性檢查"
echo "  dl-all      - 🚀 執行全部檢查"
echo "  dl-cd       - 📁 切換到專案目錄"
echo ""
echo "💡 使用方法:"
echo "  1. 重新啟動終端機 或"
echo "  2. 執行: source $SHELL_CONFIG"
echo ""
echo "然後就可以在任何位置使用 'dl' 或 'dl-issue' 等命令了！"