//! Top-level workflow executor and builtin registration.

use crate::stdlib::harn_entry::register_harn_entrypoint_category;
use crate::stdlib::macros::VmBuiltinDef;
use crate::vm::Vm;

use super::compact::{
    ESTIMATE_TOKENS_BUILTIN_DEF, MICROCOMPACT_BUILTIN_DEF, SELECT_ARTIFACTS_ADAPTIVE_BUILTIN_DEF,
    TRANSCRIPT_AUTO_COMPACT_BUILTIN_DEF,
};
use super::hooks::{
    CLEAR_PERSONA_HOOKS_BUILTIN_DEF, CLEAR_REMINDER_PROVIDERS_BUILTIN_DEF,
    CLEAR_SESSION_HOOKS_BUILTIN_DEF, CLEAR_TOOL_HOOKS_BUILTIN_DEF, DRAIN_FILE_EDITS_BUILTIN_DEF,
    FIRE_SESSION_HOOK_BUILTIN_DEF, NOTIFY_FILE_EDITED_BUILTIN_DEF,
    PIPELINE_LIFECYCLE_AUDIT_LOG_SNAPSHOT_BUILTIN_DEF,
    PIPELINE_LIFECYCLE_AUDIT_LOG_TAKE_BUILTIN_DEF, PIPELINE_ON_FINISH_BUILTIN_DEF,
    REGISTER_CHECKPOINT_HOOK_BUILTIN_DEF, REGISTER_PERSONA_HOOK_BUILTIN_DEF,
    REGISTER_REMINDER_PROVIDER_BUILTIN_DEF, REGISTER_SESSION_HOOK_BUILTIN_DEF,
    REGISTER_STEP_HOOK_BUILTIN_DEF, REGISTER_TOOL_HOOK_BUILTIN_DEF,
    SETTLEMENT_AGENT_ACTIVE_BUILTIN_DEF,
};
use super::host::{
    HOST_WORKFLOW_FINALIZE_RUN_BUILTIN_DEF, HOST_WORKFLOW_MAP_BRANCH_ARTIFACT_BUILTIN_DEF,
    HOST_WORKFLOW_MAP_EXECUTE_BRANCH_BUILTIN_DEF, HOST_WORKFLOW_MAP_FINALIZE_BUILTIN_DEF,
    HOST_WORKFLOW_MAP_PLAN_BUILTIN_DEF, HOST_WORKFLOW_PREPARE_RUN_BUILTIN_DEF,
    HOST_WORKFLOW_RECORD_TRANSITIONS_BUILTIN_DEF, HOST_WORKFLOW_STAGE_COMPLETE_BUILTIN_DEF,
    HOST_WORKFLOW_STAGE_PREPARE_BUILTIN_DEF,
};
use super::inspect::{
    WORKFLOW_CLONE_BUILTIN_DEF, WORKFLOW_COMMIT_BUILTIN_DEF, WORKFLOW_DIFF_BUILTIN_DEF,
    WORKFLOW_GRAPH_BUILTIN_DEF, WORKFLOW_INSERT_NODE_BUILTIN_DEF, WORKFLOW_INSPECT_BUILTIN_DEF,
    WORKFLOW_POLICY_REPORT_BUILTIN_DEF, WORKFLOW_REPLACE_NODE_BUILTIN_DEF,
    WORKFLOW_REWIRE_BUILTIN_DEF, WORKFLOW_SET_AUTO_COMPACT_BUILTIN_DEF,
    WORKFLOW_SET_CONTEXT_POLICY_BUILTIN_DEF, WORKFLOW_SET_MODEL_POLICY_BUILTIN_DEF,
    WORKFLOW_SET_OUTPUT_VISIBILITY_BUILTIN_DEF, WORKFLOW_VALIDATE_BUILTIN_DEF,
};

const WORKFLOW_STDLIB_ENTRYPOINT_CATEGORY: &str = "workflow.stdlib";

pub(crate) const MODULE_BUILTINS: &[&VmBuiltinDef] = &[
    // compact (token estimation, microcompaction, transcript auto-compact).
    &SELECT_ARTIFACTS_ADAPTIVE_BUILTIN_DEF,
    &ESTIMATE_TOKENS_BUILTIN_DEF,
    &MICROCOMPACT_BUILTIN_DEF,
    &TRANSCRIPT_AUTO_COMPACT_BUILTIN_DEF,
    // inspect (graph-shape builders + structural manipulation).
    &WORKFLOW_GRAPH_BUILTIN_DEF,
    &WORKFLOW_VALIDATE_BUILTIN_DEF,
    &WORKFLOW_INSPECT_BUILTIN_DEF,
    &WORKFLOW_POLICY_REPORT_BUILTIN_DEF,
    &WORKFLOW_CLONE_BUILTIN_DEF,
    &WORKFLOW_INSERT_NODE_BUILTIN_DEF,
    &WORKFLOW_REPLACE_NODE_BUILTIN_DEF,
    &WORKFLOW_REWIRE_BUILTIN_DEF,
    &WORKFLOW_SET_MODEL_POLICY_BUILTIN_DEF,
    &WORKFLOW_SET_CONTEXT_POLICY_BUILTIN_DEF,
    &WORKFLOW_SET_AUTO_COMPACT_BUILTIN_DEF,
    &WORKFLOW_SET_OUTPUT_VISIBILITY_BUILTIN_DEF,
    &WORKFLOW_DIFF_BUILTIN_DEF,
    &WORKFLOW_COMMIT_BUILTIN_DEF,
    // host (low-level workflow runtime helpers, all runtime_only).
    &HOST_WORKFLOW_PREPARE_RUN_BUILTIN_DEF,
    &HOST_WORKFLOW_RECORD_TRANSITIONS_BUILTIN_DEF,
    &HOST_WORKFLOW_FINALIZE_RUN_BUILTIN_DEF,
    &HOST_WORKFLOW_MAP_BRANCH_ARTIFACT_BUILTIN_DEF,
    &HOST_WORKFLOW_STAGE_PREPARE_BUILTIN_DEF,
    &HOST_WORKFLOW_STAGE_COMPLETE_BUILTIN_DEF,
    &HOST_WORKFLOW_MAP_PLAN_BUILTIN_DEF,
    &HOST_WORKFLOW_MAP_EXECUTE_BRANCH_BUILTIN_DEF,
    &HOST_WORKFLOW_MAP_FINALIZE_BUILTIN_DEF,
    // hooks (tool / persona / step / session / checkpoint lifecycle).
    &REGISTER_TOOL_HOOK_BUILTIN_DEF,
    &CLEAR_TOOL_HOOKS_BUILTIN_DEF,
    &REGISTER_PERSONA_HOOK_BUILTIN_DEF,
    &REGISTER_STEP_HOOK_BUILTIN_DEF,
    &CLEAR_PERSONA_HOOKS_BUILTIN_DEF,
    &REGISTER_SESSION_HOOK_BUILTIN_DEF,
    &CLEAR_SESSION_HOOKS_BUILTIN_DEF,
    &REGISTER_CHECKPOINT_HOOK_BUILTIN_DEF,
    &REGISTER_REMINDER_PROVIDER_BUILTIN_DEF,
    &CLEAR_REMINDER_PROVIDERS_BUILTIN_DEF,
    &PIPELINE_ON_FINISH_BUILTIN_DEF,
    &PIPELINE_LIFECYCLE_AUDIT_LOG_TAKE_BUILTIN_DEF,
    &PIPELINE_LIFECYCLE_AUDIT_LOG_SNAPSHOT_BUILTIN_DEF,
    &SETTLEMENT_AGENT_ACTIVE_BUILTIN_DEF,
    &NOTIFY_FILE_EDITED_BUILTIN_DEF,
    &FIRE_SESSION_HOOK_BUILTIN_DEF,
    &DRAIN_FILE_EDITS_BUILTIN_DEF,
];

pub(crate) fn register_workflow_builtins(vm: &mut Vm) {
    for def in MODULE_BUILTINS {
        vm.register_builtin_def(def);
    }
    register_harn_entrypoint_category(vm, WORKFLOW_STDLIB_ENTRYPOINT_CATEGORY);
}
