use anyhow::{Context, Result};
use momentry_core::core::db::{Database, PostgresDb};
use momentry_core::core::text::tokenizer::{contains_chinese, tokenize_chinese_text};
use momentry_core::core::text::{global_synonym_expander, normalize_chinese_query};
use std::env;

#[tokio::main]
async fn main() -> Result<()> {
    env::set_var("RUST_LOG", "info");

    println!("=== 同義詞擴展測試 ===\n");

    // 初始化 PostgreSQL
    let pg = PostgresDb::init()
        .await
        .context("Failed to initialize PostgreSQL database")?;

    let expander = global_synonym_expander();

    // 測試查詢
    let test_queries = vec![
        "電腦",
        "視頻",
        "分析",
        "工作",
        "檔案",
        "電腦工作",
        "工作檔案",
    ];

    for query_str in test_queries {
        println!("\n🔍 測試查詢: '{}'", query_str);

        // 顯示同義詞擴展
        if contains_chinese(query_str) {
            let normalized = normalize_chinese_query(query_str);
            let expanded = expander.expand_chinese_query(&normalized);
            println!("  同義詞擴展: {}", expanded);
        }

        // 顯示轉換後的 tsquery
        match pg.prepare_tsquery(query_str) {
            Ok(tsquery) => println!("  TSQUERY: {}", tsquery),
            Err(e) => println!("  TSQUERY 錯誤: {}", e),
        }

        // 執行搜索（即使沒有結果）
        let results = pg.search_bm25(query_str, None, 2).await?;
        println!("  找到 {} 筆結果", results.len());
        for (i, r) in results.iter().enumerate() {
            println!("    {}. [{}] {}", i + 1, r.uuid, r.text);
        }
    }

    Ok(())
}
