
___x_cmd_ccal_tiaoxiu(){
    local mode=auto
    local count=50
    local exclude=0

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)          ___x_cmd help -m ccal tiaoxiu "$@"; return 0 ;;
            --tsv|--csv|--tty|--yml|--auto)
                                    mode="${1#--}" ;;
            -n|--count)
                                    count="$2"; shift ;;
            --hide-plain-rest)
                                    exclude=1 ;;
            *)                      break ;;
        esac
        shift
    done

    local start_date="${1:-today}"

    # Auto-detect mode
    if [ "$mode" = auto ]; then
        if ___x_cmd_is_stdout2tty; then
            mode=tty
        else
            mode=tsv
        fi
    fi

    if [ "$start_date" = today ]; then
        start_date="$(___x_cmd_cmds date +%Y-%m-%d)"
    fi

    ___x_cmd_ccal_lunar_prepare || return $?
    ___x_cmd_ccal_lunar_prepare_index || return $?

    local y="${start_date%%-*}"
    local m="${start_date#*-}"; m="${m%-*}"
    local d="${start_date##*-}"

    m="${m#0}"
    d="${d#0}"

    if [ "$y" -lt 100 ] && [ "$y" -ge 0 ]; then
        y="$(($y+2000))"
    fi

    # Calculate end date (start_date + count days)
    local end_date
    end_date="$(___x_cmd_ccal_tiaoxiu___add_days "$start_date" "$count")"

    # Collect all data from start to end
    ___x_cmd_ccal_tiaoxiu___collect_data "$start_date" "$end_date" "$mode" "$exclude"
}

___x_cmd_ccal_tiaoxiu___add_days(){
    local ymd="$1"
    local n="$2"

    local y="${ymd%%-*}"
    local m="${ymd#*-}"; m="${m%-*}"
    local d="${ymd##*-}"

    m="${m#0}"
    d="${d#0}"

    awk -v "y=$y" -v "m=$m" -v "d=$d" -v "n=$n" '
    function daysInMonth(y, m) {
        if (m == 2) return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) ? 29 : 28
        if (m == 4 || m == 6 || m == 9 || m == 11) return 30
        return 31
    }
    BEGIN {
        nd = d + n
        nm = m
        ny = y
        while (nd > daysInMonth(ny, nm)) {
            nd -= daysInMonth(ny, nm)
            nm++
            if (nm > 12) { ny++; nm = 1 }
        }
        printf "%04d-%02d-%02d\n", ny, nm, nd
    }'
}

___x_cmd_ccal_tiaoxiu___collect_data(){
    local start_ymd="$1"
    local end_ymd="$2"
    local mode="$3"
    local exclude="$4"

    local start_y="${start_ymd%%-*}"
    local start_m="${start_ymd#*-}"; start_m="${start_m%-*}"
    local start_d="${start_ymd##*-}"
    start_m="${start_m#0}"
    start_d="${start_d#0}"

    local end_y="${end_ymd%%-*}"
    local end_m="${end_ymd#*-}"; end_m="${end_m%-*}"
    local end_d="${end_ymd##*-}"
    end_m="${end_m#0}"
    end_d="${end_d#0}"

    # Get data from lunar_cat for relevant months
    local sy="$start_y"
    local sm="$start_m"
    while [ "$sy" -lt "$end_y" ] || [ "$sy" -eq "$end_y" -a "$sm" -le "$end_m" ]; do
        ___x_cmd_ccal_lunar_cat "$sy" "$sm" --tsv 2>/dev/null
        sm=$((sm + 1))
        if [ "$sm" -gt 12 ]; then
            sm=1
            sy=$((sy + 1))
        fi
    done | awk -F'\t' -v "sy=$start_y" -v "sm=$start_m" -v "sd=$start_d" -v "ey=$end_y" -v "em=$end_m" -v "ed=$end_d" -v "mode=$mode" -v "exclude=$exclude" '
    function date2int(s, a) {
        split(s, a, "-")
        return a[1]*10000 + a[2]*100 + a[3]
    }
    NR==1 { next }
    {
        di = date2int($1)
        si = date2int(sprintf("%04d-%02d-%02d", sy, sm, sd))
        ei = date2int(sprintf("%04d-%02d-%02d", ey, em, ed))
        if (di >= si && di <= ei) {
            xiuxi = $10
            weekday = $5
            reason = $11
            is_weekend = (weekday == "六" || weekday == "日")
            # Plain rest = weekend (not 工) AND no reason (not holiday-adjacent)
            is_plain_rest = is_weekend && xiuxi != "工" && (reason == "" || reason == "无")
            # If exclude mode and it is a plain rest day
            if (exclude == 1 && is_plain_rest) {
                next
            }
            # Include if: xiuxi is 休/工, OR (weekend and NOT 休/工 - i.e. regular weekend)
            if (xiuxi == "休" || xiuxi == "工" || (xiuxi != "休" && xiuxi != "工" && is_weekend)) {
                print
            }
        }
    }
    ' | awk -F'\t' -v "mode=$mode" '
    function output_tsv() {
        xiuxi_disp = ($10=="休") ? "假" : ($10=="工" ? "工" : "休")
        reason_val = ($11=="" || $11=="无") ? "-" : $11
        printf "%s\t%s\t%s\t%s\n", $1, $5, xiuxi_disp, reason_val
    }
    function output_csv() {
        gsub(/"/, "\"\"", $11)
        xiuxi_disp = ($10=="休") ? "假" : ($10=="工" ? "工" : "休")
        reason_val = ($11=="" || $11=="无") ? "-" : $11
        printf "\"%s\",\"%s\",\"%s\",\"%s\"\n", $1, $5, xiuxi_disp, reason_val
    }
    function output_yml() {
        printf "- date: %s\n", $1
        printf "  weekday: %s\n", $5
        xiuxi_disp = ($10=="休") ? "假" : ($10=="工" ? "工" : "休")
        printf "  xiuxi: %s\n", xiuxi_disp
        if ($11!="" && $11!="无") printf "  reason: %s\n", $11
    }
    function output_tty() {
        xiuxi_disp = ($10=="休") ? "假" : ($10=="工" ? "工" : "休")
        reason = ($11=="" || $11=="无") ? "-" : $11
        style = "\033[0m"
        if (xiuxi_disp == "假") style = "\033[31m"
        else if (xiuxi_disp == "工") style = "\033[36m"
        else if (xiuxi_disp == "休") style = "\033[32m"
        printf "%s%-14s %-5s %-5s %s\033[0m\n", style, $1, $5, xiuxi_disp, reason
    }
    BEGIN { if (mode == "tty") printf "%-14s %-5s %-5s %s\n", "Date", "Week", "Xiuxi", "Reason" }
    {
        if (mode == "tsv") output_tsv()
        else if (mode == "csv") output_csv()
        else if (mode == "yml") output_yml()
        else if (mode == "tty") output_tty()
    }
    '
}
