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

d.make_mode String {
  id      = "write-cfp",
  trigger = "Produce a conference proposal (CFP) grounded in a Project or Practice node and matched to a specific conference opportunity",

  preconditions = [
    ".ontology/core.ncl exports without errors",
    ".ontology/personal.ncl has at least one Opportunity with kind = 'Conference and status in ['Watching, 'Evaluating]",
    "Target conference opportunity has at least one entry in linked_nodes pointing to a Project or Practice node",
  ],

  steps = [
    {
      id     = "resolve_talk_node",
      action = "Load the Project or Practice node(s) referenced in the conference Opportunity's linked_nodes. Extract id, name, description, and all edges. This is the core of what the talk is about.",
      cmd    = "nickel export .ontology/core.ncl | from json",
      actor  = 'Agent,
      on_error = { strategy = 'Stop },
    },
    {
      id     = "resolve_conference",
      action = "Load the target Opportunity node from .ontology/personal.ncl. Note: name, deadline, fit_signals, and note field. The fit_signals should map to gate.ncl signal types that are currently active.",
      cmd    = "nickel export .ontology/personal.ncl | from json | get opportunities | where { |o| $o.kind == 'Conference }",
      actor  = 'Agent,
      depends_on = [{ step = "resolve_talk_node" }],
      on_error = { strategy = 'Stop },
    },
    {
      id     = "extract_narrative",
      action = "From the linked nodes and their edges, build the narrative arc: what tension does this talk address, what practice does it validate, what axiom does it ground in. This becomes the CFP abstract structure.",
      actor  = 'Both,
      depends_on = [{ step = "resolve_conference" }],
      on_error = { strategy = 'Stop },
    },
    {
      id     = "check_fit",
      action = "Verify that the conference's fit_signals align with active signals in gate.ncl. If 'OpportunityAlignment or 'DepthDemonstrated are not in the active membrane, flag the mismatch before writing.",
      cmd    = "nickel export .ontology/gate.ncl | from json | get membranes | where { |m| $m.active }",
      actor  = 'Both,
      depends_on = [{ step = "extract_narrative" }],
      on_error = { strategy = 'Continue },
      note   = "Mismatch is a warning, not a blocker — the operator decides whether to proceed.",
    },
    {
      id     = "render_cfp",
      action = "Write the CFP: title (from node name + tension framing), abstract (from narrative arc, 300-500 words), speaker bio anchored to the Project/Practice node's artifact_paths and ADRs, what the audience will take away.",
      actor  = 'Agent,
      depends_on = [{ step = "check_fit" }],
      on_error = { strategy = 'Stop },
    },
    {
      id     = "review",
      action = "Human reviews for accuracy (does the abstract represent what will actually be said?), fit (does it match the conference's expected depth and audience?), and tone (is it an invitation, not a lecture?).",
      actor  = 'Human,
      depends_on = [{ step = "render_cfp" }],
      on_error = { strategy = 'Stop },
    },
    {
      id     = "update_opportunity",
      action = "If proceeding with submission: update Opportunity status from 'Watching/'Evaluating to 'Active in .ontology/personal.ncl. If rejecting: set to 'Closed with a note explaining why.",
      actor  = 'Human,
      depends_on = [{ step = "review" }],
      on_error = { strategy = 'Continue },
    },
  ],

  postconditions = [
    "A CFP draft exists grounded in a specific Project or Practice node",
    "Conference Opportunity status updated to reflect decision",
    "Fit signal check documented — either confirmed or flagged",
  ],
}
