let d = import "../defaults.ncl" in

d.make_mode String {
  id      = "draft-application",
  trigger = "Draft a job, grant, or collaboration application anchored in the personal ontology — projects, practices, and active tensions as evidence of fit",

  preconditions = [
    ".ontology/core.ncl and .ontology/personal.ncl export without errors",
    "A target Opportunity node exists in personal.ncl with kind in ['Job, 'Grant, 'Collaboration] and status in ['Watching, 'Evaluating, 'Active]",
    "Opportunity has at least one entry in linked_nodes or fit_signals",
  ],

  steps = [
    {
      id     = "resolve_opportunity",
      action = "Load the target Opportunity node: kind, name, fit_signals, linked_nodes, deadline, note. The fit_signals declare what the opportunity cares about — they drive node selection in subsequent steps.",
      cmd    = "nickel export .ontology/personal.ncl | from json | get opportunities",
      actor  = 'Agent,
      on_error = { strategy = 'Stop },
    },
    {
      id     = "check_gate_alignment",
      action = "Compare the Opportunity's fit_signals against signals accepted by active membranes in gate.ncl. 'OpportunityAlignment and 'IdentityReinforcement are the canonical fit signals. If neither active membrane accepts them, flag: this opportunity may not be the right entry point.",
      cmd    = "nickel export .ontology/gate.ncl | from json | get membranes | where { |m| $m.active }",
      actor  = 'Both,
      depends_on = [{ step = "resolve_opportunity" }],
      on_error = { strategy = 'Continue },
      note   = "Gate check is advisory. Proceeding despite mismatch is valid but should be explicit.",
    },
    {
      id     = "select_narrative_nodes",
      action = "From core.ncl, select nodes that best answer the opportunity's implicit questions: (1) What have you built? → Project nodes with artifact_paths. (2) Why does it matter? → Tension nodes showing what problem you are navigating. (3) How do you work? → Practice nodes. (4) What do you believe? → Axiom nodes with invariant = true.",
      actor  = 'Both,
      depends_on = [
        { step = "resolve_opportunity" },
        { step = "check_gate_alignment" },
      ],
      on_error = { strategy = 'Stop },
    },
    {
      id     = "resolve_career_trajectory",
      action = "From state.ncl career dimension, extract current_state → desired_state trajectory and its active blockers/catalysts. This becomes the 'why now' and 'where I am going' section of the application.",
      cmd    = "nickel export .ontology/state.ncl | from json | get dimensions | where { |d| $d.id == 'career }",
      actor  = 'Agent,
      depends_on = [{ step = "select_narrative_nodes" }],
      on_error = { strategy = 'Continue },
    },
    {
      id     = "render_draft",
      action = "Write the application: opening (why this opportunity from gate alignment check), evidence section (project nodes + artifact_paths as proof), methodology (practices), trajectory (career state), closing (what changes if accepted). Keep each section traceable to a node.",
      actor  = 'Agent,
      depends_on = [{ step = "resolve_career_trajectory" }],
      on_error = { strategy = 'Stop },
    },
    {
      id     = "review",
      action = "Human reviews for: honest representation (does each claim link to real work?), alignment (does it answer what the opportunity actually asks?), coherence (does the narrative arc hold from opening to closing?). Revise or reject.",
      actor  = 'Human,
      depends_on = [{ step = "render_draft" }],
      on_error = { strategy = 'Stop },
    },
    {
      id     = "update_status",
      action = "Update Opportunity status in .ontology/personal.ncl: 'Active if submitting, 'Closed if rejecting. Add a note with the decision rationale — this becomes institutional memory for future fit evaluations.",
      actor  = 'Human,
      depends_on = [{ step = "review" }],
      on_error = { strategy = 'Continue },
    },
  ],

  postconditions = [
    "Application draft exists and is traceable to ontology nodes",
    "Gate alignment check is documented regardless of outcome",
    "Opportunity status updated with decision rationale in note field",
  ],
}
