
# quick find out the rule breaking
# output a tsv.

___x_cmd_rule_check(){
    [ $# -gt 0 ]    ||  set -- --help

    local ruleset_dir=""
    local rule_id=""
    local x_=""

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)  ___x_cmd help -m rule check "$@" ;   return 0 ;;
            -r)         ruleset_dir="$2" ;  ___x_cmd_rule_ruleset_check_ "$ruleset_dir" || return $? ; arg:2:shift ;;
            --rule)     rule_id="$2" ;                      arg:2:shift ;;
            --)         shift ;     break ;;
            *)          break ;;
        esac
    done

    local IFS=' '
    local target_root="$1"

    case "$target_root" in
        :*)
            [ -n "$ruleset_dir" ] || {
                x_=""
                ___x_cmd_rule_preset_resolve_ "$target_root" || N=rule M="Preset '$target_root' not found" log:ret:64
                ruleset_dir="$x_"
            }
            target_root=""
            ;;
    esac

    [ -n "$ruleset_dir" ] || {
        x_=""
        ___x_cmd_rule_which_ || N=rule M="Fail to locate any ruleset. You can use -r to specify" log:ret:64
        ruleset_dir="$x_"
    }

    if [ -n "$rule_id" ]; then
        ___x_cmd_rule_check___one_rule "$ruleset_dir" "$rule_id" "$target_root"
    else
        ___x_cmd_rule_check___all "$ruleset_dir" "$target_root"
    fi
}

___x_cmd_rule_check___one_rule(){
    local ruleset_dir="$1"
    local rule_id="$2"
    local target_root="$3"
    local TAB="$___X_CMD_UNSEENCHAR_HT"

    local x_=""
    ___x_cmd_rule_info_ "$rule_id" "$ruleset_dir" || {
        N=rule M="Rule '$rule_id' not found in ruleset" log:ret:64
    }
    local rule_body="$x_"

    rule:info "Check single rule | rule:$rule_id | target:$target_root"

    x_=""; ___x_cmd epoch get_
    local tmpfile="$___X_CMD_ROOT_TMP/rule/check/${x_}-$$.tsv"

    local system_prompt="$___X_CMD_RULE_PROMPT_RULE_FMT
You are a quality checker. You are evaluating ONE specific rule.

## Rule

ID: $rule_id

$rule_body

## Task

1. Read the target: $target_root
2. Evaluate ONLY the rule above against the target.
3. Output the result — include BOTH pass and violate rows (do not filter by score).

## Output

Write the complete TSV result to: $tmpfile

The TSV format is:

# The following are examples for illustration only
root${TAB}target${TAB}ruleid${TAB}score${TAB}hint
${target_root}${TAB}src/hello-world.sh${TAB}${rule_id}${TAB}10${TAB}变量命名不规范

Where:
- root: the root directory the target belongs to
- target: relative path to the source file under the root
- score: integer 0-100
- hint: brief explanation (empty string if score is 100)

## Paths

- Target root: $target_root
- Output file: $tmpfile
"

    ___x_cmd agent request "$system_prompt" || return $?

    [ -s "$tmpfile" ] || return 0

    rule:info "Check completed | rule:$rule_id | output:$tmpfile"
    ___x_cmd_cmds cat "$tmpfile"
}

___x_cmd_rule_check___all(){
    local ruleset_dir="$1"
    local target_root="$2"

    rule:info "Quick check | ruleset:$ruleset_dir | target:$target_root"

    x_=""; ___x_cmd epoch get_
    local tmpfile="$___X_CMD_ROOT_TMP/rule/check/${x_}-$$.tsv"
    local TAB="$___X_CMD_UNSEENCHAR_HT"

    local system_prompt="$___X_CMD_RULE_PROMPT_RULE_FMT
You are a quality checker.

## Task

1. Read all rule files under: $ruleset_dir
   Each .yml file contains rules in the format shown above.
2. Read all files under: $target_root
3. For each rule that is violated, record the target file and a brief hint.

Rules:
- Only include rows where the rule is violated (score < 81).
- ruleid: the rule ID (e.g. P06-var-010).
- target: relative path to the source file.
- score: integer 0-80.
- hint: brief explanation of the violation.
- If all files pass all rules, output just the header row.

## Output

Write the complete TSV result to: $tmpfile

The TSV format is:

# The following are examples for illustration only
root${TAB}target${TAB}ruleid${TAB}score${TAB}hint
${target_root}${TAB}src/hello-world.sh${TAB}P06-var-010${TAB}10${TAB}变量命名不规范
${target_root}${TAB}docs/README.md${TAB}P06-concept-010${TAB}10${TAB}术语使用不当

Where:
- root: the root directory the target belongs to (use the value from Target path above)
- target: relative path to the source file under the root

## Paths

- Rules: $ruleset_dir
- Target root: $target_root
- Output file: $tmpfile
"

    ___x_cmd agent request "$system_prompt" || return $?

    [ -s "$tmpfile" ] || return 0

    rule:info "Quick check completed | output:$tmpfile"
    ___x_cmd_cmds cat "$tmpfile"
}
