#!/bin/bash
# ================= MOMO 系統 - Quick Review / QA 腳本 =================
# 功能：快速觸發 Aider Code Review 或 AI 觀測台 QA
# 使用方法：./quick_review.sh [檔案名稱...]
# 範例：./quick_review.sh app.py routes/sales_routes.py
# 範例：./quick_review.sh --observability-qa
# =======================================================================

# 顏色定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 獲取專案根目錄
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
CODE_REVIEW_SCRIPT="$PROJECT_ROOT/scripts/code_review.py"
OBSERVABILITY_UI_GUARD="$PROJECT_ROOT/scripts/check_observability_ui.py"
OBSERVABILITY_PAGE_SMOKE="$PROJECT_ROOT/scripts/check_observability_pages.py"
OBSERVABILITY_QA_SUITE="$PROJECT_ROOT/scripts/check_observability_suite.sh"
OBSERVABILITY_CSS_SYNC="$PROJECT_ROOT/scripts/sync_observability_css.py"
OBSERVABILITY_VISUAL_CONTRACT="$PROJECT_ROOT/scripts/check_observability_visual_contract.sh"
RESPONSIVE_OVERFLOW_GUARD="$PROJECT_ROOT/scripts/check_responsive_overflow.sh"
SALES_CHARTS_GUARD="$PROJECT_ROOT/scripts/check_sales_charts_runtime.sh"
REVIEW_REPORT_HINT=0

# 顯示標題
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}🔍 MOMO 系統 - Quick Review / QA${NC}"
echo -e "${BLUE}========================================${NC}"

# 檢查Python環境
if ! command -v python3 &> /dev/null; then
    echo -e "${RED}❌ Python3未安裝${NC}"
    exit 1
fi

run_code_review() {
    if [ ! -f "$CODE_REVIEW_SCRIPT" ]; then
        echo -e "${RED}❌ Code Review腳本不存在: $CODE_REVIEW_SCRIPT${NC}"
        exit 1
    fi

    if ! command -v aider &> /dev/null; then
        echo -e "${RED}❌ Aider未安裝或不在PATH中${NC}"
        exit 1
    fi

    python3 "$CODE_REVIEW_SCRIPT" "$@"
}

run_observability_ui_guard() {
    if [ ! -f "$OBSERVABILITY_UI_GUARD" ]; then
        echo -e "${RED}❌ AI觀測台 UI guard 不存在: $OBSERVABILITY_UI_GUARD${NC}"
        exit 1
    fi

    echo -e "${GREEN}🎛️ 開始 AI觀測台 UI/UX 防回歸檢查...${NC}"
    python3 "$OBSERVABILITY_UI_GUARD"
}

run_observability_page_smoke() {
    if [ ! -f "$OBSERVABILITY_PAGE_SMOKE" ]; then
        echo -e "${RED}❌ AI觀測台頁面巡檢不存在: $OBSERVABILITY_PAGE_SMOKE${NC}"
        exit 1
    fi

    echo -e "${GREEN}🌐 開始 AI觀測台 10頁線上巡檢...${NC}"
    python3 "$OBSERVABILITY_PAGE_SMOKE" "$@"
}

run_observability_qa_suite() {
    if [ ! -f "$OBSERVABILITY_QA_SUITE" ]; then
        echo -e "${RED}❌ AI觀測台 QA 套件不存在: $OBSERVABILITY_QA_SUITE${NC}"
        exit 1
    fi

    echo -e "${GREEN}🏛️ 開始 AI觀測台完整 QA 套件...${NC}"
    bash "$OBSERVABILITY_QA_SUITE" "$@"
}

run_observability_css_sync() {
    if [ ! -f "$OBSERVABILITY_CSS_SYNC" ]; then
        echo -e "${RED}❌ AI觀測台 CSS 同步腳本不存在: $OBSERVABILITY_CSS_SYNC${NC}"
        exit 1
    fi

    echo -e "${GREEN}🎨 同步 AI觀測台 CSS 到 Flask static path...${NC}"
    python3 "$OBSERVABILITY_CSS_SYNC" "$@"
}

run_observability_visual_contract() {
    if [ ! -f "$OBSERVABILITY_VISUAL_CONTRACT" ]; then
        echo -e "${RED}❌ AI觀測台渲染後視覺契約不存在: $OBSERVABILITY_VISUAL_CONTRACT${NC}"
        exit 1
    fi

    echo -e "${GREEN}🎨 開始 AI觀測台渲染後視覺契約檢查...${NC}"
    bash "$OBSERVABILITY_VISUAL_CONTRACT" "$@"
}

run_responsive_overflow_guard() {
    if [ ! -f "$RESPONSIVE_OVERFLOW_GUARD" ]; then
        echo -e "${RED}❌ Responsive overflow guard 不存在: $RESPONSIVE_OVERFLOW_GUARD${NC}"
        exit 1
    fi

    echo -e "${GREEN}📱 開始全頁 responsive overflow 檢查...${NC}"
    bash "$RESPONSIVE_OVERFLOW_GUARD" "$@"
}

run_sales_charts_guard() {
    if [ ! -f "$SALES_CHARTS_GUARD" ]; then
        echo -e "${RED}❌ 業績圖表 runtime guard 不存在: $SALES_CHARTS_GUARD${NC}"
        exit 1
    fi

    echo -e "${GREEN}📈 開始業績分析圖表 runtime 檢查...${NC}"
    bash "$SALES_CHARTS_GUARD" "$@"
}

# 非互動入口：給部署腳本、CI、或 Codex session 直接呼叫。
if [ $# -gt 0 ]; then
    case "$1" in
        --observability-ui)
            shift
            run_observability_ui_guard
            exit $?
            ;;
        --observability-smoke)
            shift
            run_observability_page_smoke "$@"
            exit $?
            ;;
        --observability-qa)
            shift
            run_observability_qa_suite "$@"
            exit $?
            ;;
        --sync-observability-css)
            shift
            run_observability_css_sync "$@"
            exit $?
            ;;
        --check-observability-css)
            shift
            run_observability_css_sync --check
            exit $?
            ;;
        --observability-visual)
            shift
            run_observability_visual_contract "$@"
            exit $?
            ;;
        --responsive-overflow)
            shift
            run_responsive_overflow_guard "$@"
            exit $?
            ;;
        --sales-charts)
            shift
            run_sales_charts_guard "$@"
            exit $?
            ;;
        --observability-help)
            cat <<'EOF'
AI observability quick-review flags:
  --sync-observability-css
      Sync static/css/observability-system.css to web/static/css mirror.
  --check-observability-css
      Verify CSS mirror is already committed and in sync.
  --observability-ui
      Run static UI/template/navigation guard only.
  --observability-smoke [--base-url URL] [--timeout SEC]
      Run production page/CSS smoke against the target URL.
  --observability-qa [--base-url URL] [--skip-production]
      Run the full QA suite.
  --observability-visual [--base-url URL] [--timeout SEC]
      Run rendered typography, surface, radius, contrast, and mobile density checks.
  --responsive-overflow [--base-url URL] [--route PATH ...]
      Run desktop/tablet/mobile body horizontal overflow checks for Flask routes.
      Pass --screenshot-dir DIR --screenshot-all to save passing viewport QA shots.
  --sales-charts [--base-url URL] [--timeout SEC]
      Run Chart.js runtime and nonblank canvas checks for /daily_sales and /growth_analysis.
EOF
            exit 0
            ;;
    esac
fi

# 顯示選單
if [ $# -eq 0 ]; then
    echo -e "${YELLOW}請選擇操作：${NC}"
    echo "1) 自動Review暫存檔案 (Git staged)"
    echo "2) Review所有變更檔案"
    echo "3) 指定檔案Review"
    echo "4) 安全檢查 (Security Review)"
    echo "5) 效能檢查 (Performance Review)"
    echo "6) AI觀測台 UI/UX 防回歸檢查"
    echo "7) AI觀測台 10頁線上巡檢"
    echo "8) AI觀測台完整 QA 套件"
    echo "9) 同步 AI觀測台 CSS static mirror"
    echo "10) 全頁 responsive overflow 檢查"
    echo "11) AI觀測台渲染後視覺契約檢查"
    echo "12) 業績分析圖表 runtime 檢查"
    echo ""
    read -p "請輸入選項 (1-12): " choice
    
    case $choice in
        1)
            echo -e "${GREEN}🚀 開始自動Review暫存檔案...${NC}"
            REVIEW_REPORT_HINT=1
            run_code_review --auto --type basic
            ;;
        2)
            echo -e "${GREEN}🚀 開始Review所有變更檔案...${NC}"
            REVIEW_REPORT_HINT=1
            run_code_review --type basic
            ;;
        3)
            echo -e "${YELLOW}請輸入要Review的檔案路徑 (用空格分隔)：${NC}"
            read -r files_input
            if [ -n "$files_input" ]; then
                echo -e "${GREEN}🚀 開始Review指定檔案...${NC}"
                REVIEW_REPORT_HINT=1
                run_code_review --files $files_input --type basic
            else
                echo -e "${RED}❌ 未指定檔案${NC}"
                exit 1
            fi
            ;;
        4)
            echo -e "${GREEN}🛡️ 開始安全檢查...${NC}"
            REVIEW_REPORT_HINT=1
            run_code_review --auto --type security
            ;;
        5)
            echo -e "${GREEN}⚡ 開始效能檢查...${NC}"
            REVIEW_REPORT_HINT=1
            run_code_review --auto --type performance
            ;;
        6)
            run_observability_ui_guard
            ;;
        7)
            run_observability_page_smoke
            ;;
        8)
            run_observability_qa_suite
            ;;
        9)
            run_observability_css_sync
            ;;
        10)
            run_responsive_overflow_guard
            ;;
        11)
            run_observability_visual_contract
            ;;
        12)
            run_sales_charts_guard
            ;;
        *)
            echo -e "${RED}❌ 無效選項${NC}"
            exit 1
            ;;
    esac
else
    # 有指定檔案，直接Review
    echo -e "${GREEN}🚀 開始Review指定檔案...${NC}"
    echo -e "${BLUE}檔案：$@${NC}"
    REVIEW_REPORT_HINT=1
    run_code_review --files "$@" --type basic
fi

# 檢查執行結果
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ Quick Review / QA 完成！${NC}"
    if [ "$REVIEW_REPORT_HINT" -eq 1 ]; then
        echo -e "${BLUE}📄 Review報告位置：$PROJECT_ROOT/logs/${NC}"
    fi
else
    echo -e "${RED}❌ Quick Review / QA 失敗！${NC}"
    exit 1
fi
