# shellcheck shell=dash disable=2269
# x agent job init "Implement user authentication"     # create job id
# x agent job iter --max-iterations 100 --inrow 10

xrc:mod:lib agent       job/util job/ls job/status job/prompt job/run job/stop


# x agent job bootstrap -- guiding users creating a job.

# x agent job init      -- create       -- no adjust
# x agent job iter      --addition "..."
# x agent job status    # running , stopped-to-be-continued, stopped-considered-done

# using ajob to offload missions.
# using

# File Directory structure.


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

    local op="$1"
    case "$op" in
        ls|status|run|stop)
                    shift; ___x_cmd_agent_job_"$op" "$@"        ;;
        -h|--help)  ___x_cmd help -m agent job; return 0        ;;
        *)          N=agent M="Unknown operation" log:ret:64    ;;
    esac
}

___X_CMD_AGENT_JOB_RECENT=""
___X_CMD_AGENT_JOB_DIR="$___X_CMD_ROOT_DATA/agent/job"

# Writes user's description to DESC.md, then calls AI to generate/update PLAN.md and AGENTS.md.
# Run a complete job: initialize then iterate until completion.
___x_cmd_agent_job_exec(){
    [ $# -gt 0 ]    ||      set -- --help

    local mode="auto"
    local jobdir=""
    local jobid=""
    local harness=""
    local debug=""
    local max_iterations=100
    local inrow=2
    local continuous=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)              ___x_cmd help -m agent job init; return 0 ;;
            --auto)                 mode=auto ;     shift ;;
            -b|--batch)             mode=batch ;    shift ;;
            --job-dir)              jobdir="$2" ;   arg:2:shift ;;
            --job-id)               jobid="$2" ;    arg:2:shift ;;
            --harness)              harness="$2" ;  arg:2:shift ;;
            --debug)                debug=1 ;       shift ;;
            --max-iterations)       max_iterations="$2" ;       arg:2:int; arg:2:shift ;;
            --inrow)                inrow="$2" ;                arg:2:int; arg:2:shift ;;
            --continuous)           continuous=1;               shift ;;
            --)                     shift ; break ;;
            *)                      break ;;
        esac
    done

    local desc="$*"
    [ -n "$desc" ] || N=agent M="Please specify the job description" log:ret:64

    if [ -z "$jobid" ] && [ -z "$jobdir" ] && [ -n "$___X_CMD_AGENT_JOB_RECENT" ]; then
        jobdir="$___X_CMD_AGENT_JOB_RECENT"
    else
        [ -n "$jobid" ] || jobid="$(___x_cmd date vlid)"
        if [ -z "$jobdir" ]; then
            jobdir="$___X_CMD_AGENT_JOB_DIR/$jobid"
        else
            jobdir="$jobdir/$jobid" # Remove trailing slash if any
        fi

        ___X_CMD_AGENT_JOB_RECENT="$jobdir"
    fi

    local x_=""
    if [ -z "$harness" ]; then
        x_=""; ___x_cmd_agent_harness_
        harness="$x_"
    fi

    ___x_cmd_agent_job___init "$jobdir" || return $?
    ___x_cmd_agent_job___iter "$jobdir"
}

# Initialize or re-initialize a job.
___x_cmd_agent_job___init(){
    local jobdir="$1"
    local is_init=0
    local vlid; vlid="$(___x_cmd date vlid)"
    if [ -d "$jobdir" ]; then
        # Re-init existing job: append to DESC.md
        printf "Timestamp: %s\nAdjust Desc: %s\n---\n" "$vlid" "$desc" >> "$jobdir/DESC.md"
    else
        # Init new job
        is_init=1
        ___x_cmd mkdirp "$jobdir"
        printf "Timestamp: %s\nPwd: %s\nDesc: %s\n---\n" "$vlid" "$PWD" "$desc" > "$jobdir/DESC.md"
    fi

    local descfile="$jobdir/DESC.md"
    local planfile="$jobdir/PLAN.md"
    local agentfile="$jobdir/AGENTS.md"

    # Call AI to generate/update PLAN.md and AGENTS.md
    local content;
    descfile="$descfile" planfile="$planfile" agentfile="$agentfile" jobdir="$jobdir" \
    content="$( ___x_cmd_agent_job___get_prompt init )"

    [ -n "$content" ] || {
        agent:error "Failed to load init prompt template"
        return 1
    }

    local debug_opt=""
    [ -z "$debug" ] || debug_opt="--debug"
    ___x_cmd agent request --harness "$harness" $debug_opt -f "$descfile" -f "$planfile" -f "$agentfile" "$content" || return $?

    [ -r "$planfile" ] || {
        agent:error "Failed to read PLAN.md after init. The AI may not have created the task plan properly"
        return 1
    }

    # Remove old DONE.md since the task plan has been adjusted
    if [ -f "$jobdir/DONE.md" ]; then
        ___x_cmd rmrf "$jobdir/DONE.md"
        agent:info "Removed old DONE.md - Task plan changed, previous completion status invalidated"
    fi

    if [ "$is_init" = 1 ]; then
        agent:info "Job created at $jobdir"
    else
        agent:info "Job re-initialized at $jobdir"
    fi
    agent:info "PLAN.md and AGENTS.md updated"
}

# Continue job iterations
___x_cmd_agent_job___iter(){
    local jobdir="$1"
    if [ -z "$jobdir" ]; then
        N=agent M="No job directory found" log:ret:64
    fi

    if [ ! -d "$jobdir" ]; then
        N=agent M="Job directory not found: $jobdir" log:ret:1
    fi

    local descfile="$jobdir/DESC.md"
    local planfile="$jobdir/PLAN.md"
    local agentfile="$jobdir/AGENTS.md"
    local donefile="$jobdir/DONE.md"

    if [ ! -r "$agentfile" ]; then
        N=agent M="AGENTS.md not found in $jobdir" log:ret:1
    fi

    if [ ! -r "$planfile" ]; then
        N=agent M="PLAN.md not found in $jobdir" log:ret:1
    fi

    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
        # Display PLAN content before starting iterations
        ___x_cmd ui sep
        printf "\033[1mPlan:\033[0m\n"
        ___x_cmd_cmds cat "$planfile"
        ___x_cmd ui sep
    fi

    local iter=0
    local no_progress=0
    local content=""
    local vlid=""
    local response_file=""
    local loop_context=""
    local session_id=""

    # Generate a session ID for continuous mode
    if [ -n "$continuous" ]; then
        session_id="$(___x_cmd rand uuid)"
        agent:info "Continuous mode enabled, using session: $session_id"
    fi

    while [ "$iter" -lt "$max_iterations" ] && [ ! -f "$donefile" ]; do
        iter="$((iter + 1))"
        agent:info "Starting iteration $iter (max: $max_iterations)"

        vlid="$(___x_cmd date vlid)"
        response_file="$jobdir/$vlid.response.md"

        loop_context="$(___x_cmd_agent_job___build_loop_context "$jobdir" "$iter" "$max_iterations" "$no_progress" "$inrow")"

        descfile="$descfile" planfile="$planfile" agentfile="$agentfile" donefile="$donefile" jobdir="$jobdir" iter="$iter" vlid="$vlid" inrow="$inrow" max_iterations="$max_iterations" loop_context="$loop_context" response_file="$response_file" \
        content="$( ___x_cmd_agent_job___get_prompt iter )"

        [ -n "$content" ] || {
            agent:error "Failed to load iter prompt template"
            break
        }

        local debug_opt=""
        [ -z "$debug" ] || debug_opt="--debug"
        if [ -n "$session_id" ]; then
            ___x_cmd agent request --harness "$harness" $debug_opt --session "$session_id" --file "$agentfile" --file "$planfile" "$content" || break
        else
            ___x_cmd agent request --harness "$harness" $debug_opt --file "$agentfile" --file "$planfile" "$content" || break
        fi

        ___x_cmd_agent_job_iter___eval_response "$jobdir" "$response_file" "$iter" "$inrow" "$donefile" "$max_iterations" "$vlid" "$harness" || break
    done

    if [ "$iter" -ge "$max_iterations" ]; then
        N=agent M="Maximum iterations reached" log:ret:1
    fi

    if [ -f "$donefile" ]; then
        agent:info "Job completed successfully. Final report: $donefile"
    else
        agent:info "Job stopped (incomplete). Iteration responses saved in $jobdir/*.response.md"
    fi
}
