#!/bin/bash
source ./Global_toolkit.sh
log_and_continue() {
    local errmsg="$1"
    mkdir -p ./cache
    echo "preprocess $(date '+%Y-%m-%d %H:%M:%S') $errmsg" >> ./cache/Preprocess_CrashReport.txt
    echo "preprocess 错误已记录: $errmsg"
}
tag=$1
flag=$2
receiver=$(sqlite3 'cache/OQQWall.db' "SELECT receiver FROM preprocess WHERE tag = '$tag';")
senderid=$(sqlite3 'cache/OQQWall.db' "SELECT senderid FROM preprocess WHERE tag = '$tag';")
waittime=$(grep 'process_waittime' oqqwall.config | cut -d'=' -f2 | tr -d '"')
force_chromium_no_sandbox=$(grep 'force_chromium_no-sandbox' oqqwall.config | cut -d'=' -f2 | tr -d '"')
if [[ $flag == nowaittime || $flag == randeronly ]]; then waittime=0; fi
json_file="./AcountGroupcfg.json"
group_info=$(jq -r --arg receiver "$receiver" '
  to_entries[] | select(.value.mainqqid == $receiver or (.value.minorqqid[]? == $receiver))
' "$json_file")
if [ -z "$group_info" ]; then
  log_and_continue "未找到ID为 $tag 的相关信息。"
  exit 1
fi
echo "开始处理来自$senderid的消息,账号$receiver,内部编号$tag"
groupname=$(echo "$group_info" | jq -r '.key')
groupid=$(echo "$group_info" | jq -r '.value.mangroupid')
mainqqid=$(echo "$group_info" | jq -r '.value.mainqqid')
minorqqid=$(echo "$group_info" | jq -r '.value.minorqqid[]')
mainqq_http_port=$(echo "$group_info" | jq -r '.value.mainqq_http_port')
minorqq_http_ports=$(echo "$group_info" | jq -r '.value.minorqq_http_port[]')


# 拉黑检查：如果 senderid 在 blocklist 表中被拉黑，则删除 preprocess 表中的该 tag 行并退出
is_blocked=$(sqlite3 'cache/OQQWall.db' "SELECT 1 FROM blocklist WHERE senderid = '$senderid' AND ACgroup = '$groupname' LIMIT 1;")
if [[ "$is_blocked" == "1" ]]; then
  echo "发件人 $senderid 在账户组 $groupname 已被拉黑，忽略本次投稿。"
  sqlite3 'cache/OQQWall.db' "DELETE FROM preprocess WHERE tag = '$tag';"
  exit 0
fi

# 初始化端口变量
port=""
# 检查输入ID是否为mainqqid
if [ "$receiver" == "$mainqqid" ]; then
  port=$mainqq_http_port
else
  # 遍历 minorqqid 数组并找到对应的端口
  i=0
  for minorqqid in $minorqqid; do
    if [ "$receiver" == "$minorqqid" ]; then
      port=$(echo "$minorqq_http_ports" | sed -n "$((i+1))p")
      break
    fi
    ((i++))
  done
fi
echo waitingforsender...
sleep $waittime
last_modtime=$(sqlite3 'cache/OQQWall.db' "SELECT modtime FROM sender WHERE senderid = '$senderid';")
if [[ $flag == randeronly ]]; then
  echo "跳过更新processtime（randeronly 模式）"
else
  sqlite3 'cache/OQQWall.db' " update sender SET processtime = '$last_modtime' WHERE senderid = '$senderid';"
fi
if [[ $flag == randeronly ]]; then
  echo "跳过 LLM 处理（randeronly 模式）"
else
  echo process-message-to-jpg...
  ###processinfo###
  # Step 1: Process tag and send to LM
  attempt=0
  max_lm_attempts=3
  success=false
  while [[ $attempt -lt $max_lm_attempts ]]; do
    if getmsgserv/LM_work/progress-lite-json.sh "$tag" "$port"| python3 getmsgserv/LM_work/sendtoLM.py "$tag"; then
      success=true
      break
    else
      ((attempt++))
      echo "Attempt $attempt failed, retrying..."
    fi
  done
  if [ "$success" = false ]; then
    sendmsggroup LLM处理错误，请检查相关信息
  fi
fi

echo '开始处理json到html'
# Step 2: Lock the cache files and process HTML to PDF
{
  flock -x 200  # Acquire exclusive oock
  getmsgserv/HTMLwork/gotohtml.sh $tag > /dev/shm/OQQWall/oqqwallhtmlcache.html

#确保html写完了
wait_stable() { local f="$1" last=0; while :; do
  sz=$(stat -c%s "$f" 2>/dev/null || echo 0)
  [[ $sz -gt 0 && $sz -eq $last ]] && break
  last=$sz; sleep 0.1
done; }
wait_stable /dev/shm/OQQWall/oqqwallhtmlcache.html

echo '开始处理html到pdf'
# Step 3: Process the output into JPG
# ---- 1. 选择可执行文件 ---------------------------------------------------
# 依次查找可用的浏览器可执行文件
for candidate in  google-chrome-stable chrome chromium-browser chromium; do
    if command -v "$candidate" >/dev/null 2>&1; then
        CHROME_BIN=$(command -v "$candidate")
        break
    fi
done

# 如果都没找到就退出
if [[ -z "${CHROME_BIN:-}"  ]]; then
    echo "Error: no suitable Chromium/Chrome binary found in PATH." >&2
    exit 1
fi

# ---- 2. 参数 --------------------------------------------------------
PDF_OUT=/dev/shm/OQQWall/oqqwallpdfcache.pdf
HTML_IN=file:///dev/shm/OQQWall/oqqwallhtmlcache.html

COMMON_ARGS=(
    --headless
    --run-all-compositor-stages-before-draw
    --no-pdf-header-footer
    --virtual-time-budget=20000
    --pdf-page-orientation=portrait
    --no-margins
    --enable-background-graphics
    --print-background=true
    --allow-file-access-from-files 
        --print-to-pdf="$PDF_OUT"
    )

# 根据配置决定是否添加 --no-sandbox 参数
if [[ "$force_chromium_no_sandbox" == "true" ]]; then
    COMMON_ARGS+=(--no-sandbox)
    echo "Chromium 将使用 --no-sandbox 模式运行"
fi

# ---- 3. 执行 --------------------------------------------------------------
"$CHROME_BIN" "${COMMON_ARGS[@]}" "$HTML_IN"


folder=./cache/prepost/${tag}
json_data=$(sqlite3 'cache/OQQWall.db' "SELECT AfterLM FROM preprocess WHERE tag = '$tag';")
if [[ -z "$json_data" ]]; then
    log_and_continue "No data found for tag $tag"
    exit 1
fi
rm -rf $folder
mkdir -p "$folder"
# 使用identify获取PDF页数
pages=$(identify -format "%n\n" /dev/shm/OQQWall/oqqwallpdfcache.pdf | head -n 1)
# 循环处理每一页
for ((i=0; i<$pages; i++)); do
    formatted_index=$(printf "%02d" $i)
    convert -density 360 -quality 90 /dev/shm/OQQWall/oqqwallpdfcache.pdf[$i] $folder/${tag}-${formatted_index}.jpeg
done
existing_files=$(ls "$folder" | wc -l)
next_file_index=$existing_files
echo "$json_data" | jq -r '.messages[].message[] | select(.type == "image" and .data.sub_type == 0) | .data.url' | while read -r url; do
    # 格式化文件索引
    formatted_index=$(printf "%02d" $next_file_index)

    # 下载文件并保存
    curl -o "$folder/$tag-${formatted_index}.jpg" "$url"

    # 增加文件索引
    next_file_index=$((next_file_index + 1))
done
} 200>/dev/shm/OQQWall/oqqwall.lock  # Lock the directory with a lock file
# 保留文件后缀，便于浏览器与前端按 MIME 正确识别与展示
# 之前会去掉后缀，导致 review 页面扩展名缺失、识别困难

LMjson=$(sqlite3 'cache/OQQWall.db' "SELECT AfterLM FROM preprocess WHERE tag = '$tag';")
need_priv=$(echo $LMjson | jq -r '.needpriv' )
isover=$(echo $LMjson | jq -r '.isover')
notregular=$(echo $LMjson | jq -r '.notregular')
safemsg=$(echo $LMjson | jq -r '.safemsg')
MSGcache=有投稿消息
if [ "$isover" = "true" ]; then
  MSGcache+=,AI判断已写完
else
  MSGcache+=,AI判断未写完
fi
if [ "$safemsg" = "true" ]; then
  MSGcache+=，AI审核判定安全
elif [ "$safemsg" = "false" ]; then
  MSGcache+=，AI审核判定不安全
fi
current_mod_time_id=$(sqlite3 'cache/OQQWall.db' "select modtime from sender where senderid=$senderid;")
if [[ "$current_mod_time_id" != "$last_modtime" ]]; then
    MSGcache+=，处理过程中有消息更新（需要更新请用刷新指令）
fi
MSGcache+=，内部编号$tag，请发送指令
folder_path="$(pwd)/cache/prepost/$tag"
# 检查文件夹是否存在
if [ ! -d "$folder_path" ]; then
  log_and_continue "文件夹 $folder_path 不存在"
fi

# 为无后缀的图片文件补充正确的扩展名（提升前端展示与 MIME 识别稳定性）
for f in "$folder_path"/*; do
  [ -f "$f" ] || continue
  name="$(basename -- "$f")"
  case "$name" in
    *.*) continue ;;  # 已有后缀则跳过
  esac
  mime="$(file -b --mime-type "$f" 2>/dev/null || echo '')"
  ext="jpg"
  case "$mime" in
    image/jpeg*) ext="jpg" ;;
    image/png*)  ext="png" ;;
    image/gif*)  ext="gif" ;;
    image/webp*) ext="webp" ;;
    image/bmp*)  ext="bmp" ;;
  esac
  mv -- "$f" "$f.$ext" 2>/dev/null || true
done
echo $MSGcache
for file_path in $(find "$folder_path" -maxdepth 1 -type f | sort); do
    echo "添加文件: $file_path"
    MSGcache+="[CQ:image,file=file://$file_path]"
done
echo $MSGcache
sendmsggroup "$MSGcache"
