///|
pub(all) enum BookArtifactKind {
  RawMarketData
  RawPromptPacket
  RawModelRequest
  RawModelResponse
  RawRunEvents
  GeneratedProjection
  GeneratedStructureLevels
  GeneratedContextBudget
  GeneratedDecisionPolicy
  GeneratedAnalysisRecord
  GeneratedExperienceSelection
  GeneratedChartFrame
  ImportedLegacyRecord
  ImportedExperienceMemory
  ImportedTradeLog
  GeneratedImportManifest
  GeneratedBookCommit
} derive(Debug, Eq, ToJson, FromJson)

///|
pub(all) struct BookArtifactRef {
  run_id : String
  path : String
  schema_id : String
  kind : BookArtifactKind
  purpose : String
} derive(Debug, Eq, ToJson, FromJson)

///|
pub(all) struct BookWritePlan {
  run_id : String
  artifacts : Array[BookArtifactRef]
} derive(Debug, Eq, ToJson, FromJson)

///|
pub fn book_artifact_ref(
  run_id : String,
  path : String,
  schema_id : String,
  kind : BookArtifactKind,
  purpose : String,
) -> BookArtifactRef {
  { run_id, path, schema_id, kind, purpose }
}

///|
fn run_file(dir : String, run_id : String, file : String) -> String {
  "\{dir}/\{run_id}/\{file}.json"
}

///|
fn run_record(dir : String, run_id : String) -> String {
  "\{dir}/\{run_id}.json"
}

///|
pub fn dry_projection_write_plan(
  projection : @execution.DryExecutionProjection,
) -> BookWritePlan {
  let run_id = projection.request.run_id
  {
    run_id,
    artifacts: [
      book_artifact_ref(
        run_id,
        run_record("raw/market-data", run_id),
        "kline-snapshot",
        RawMarketData,
        "immutable closed-bar snapshot evidence",
      ),
      book_artifact_ref(
        run_id,
        run_file("raw/prompts", run_id, "stage1"),
        "prompt-packet",
        RawPromptPacket,
        "Stage 1 prompt packet sent to the model",
      ),
      book_artifact_ref(
        run_id,
        run_file("raw/prompts", run_id, "stage2"),
        "prompt-packet",
        RawPromptPacket,
        "Stage 2 prompt packet sent to the model",
      ),
      book_artifact_ref(
        run_id,
        run_file("raw/model-replies", run_id, "stage1-request"),
        "model-call-request",
        RawModelRequest,
        "Stage 1 model request routed through MoonGate",
      ),
      book_artifact_ref(
        run_id,
        run_file("raw/model-replies", run_id, "stage1-response"),
        "model-call-response",
        RawModelResponse,
        "Stage 1 raw model response evidence",
      ),
      book_artifact_ref(
        run_id,
        run_file("raw/model-replies", run_id, "stage2-request"),
        "model-call-request",
        RawModelRequest,
        "Stage 2 model request routed through MoonGate",
      ),
      book_artifact_ref(
        run_id,
        run_file("raw/model-replies", run_id, "stage2-response"),
        "model-call-response",
        RawModelResponse,
        "Stage 2 raw model response evidence",
      ),
      book_artifact_ref(
        run_id,
        run_record("raw/run-events", run_id),
        "routine-event-log",
        RawRunEvents,
        "ordered MoonClaw routine event stream",
      ),
      book_artifact_ref(
        run_id,
        run_record("records/projections", run_id),
        "dry-execution-projection",
        GeneratedProjection,
        "offline execution projection for audit and replay",
      ),
      book_artifact_ref(
        run_id,
        run_record("records/structure-levels", run_id),
        "structure-level-frame",
        GeneratedStructureLevels,
        "deterministic support, resistance, and midline levels for operator review",
      ),
      book_artifact_ref(
        run_id,
        run_record("records/context-budgets", run_id),
        "context-budget-report",
        GeneratedContextBudget,
        "model context budget report for Stage 1 and Stage 2 prompts",
      ),
      book_artifact_ref(
        run_id,
        run_record("records/policies", run_id),
        "decision-policy-report",
        GeneratedDecisionPolicy,
        "Stage 2 decision policy report for advisory safety",
      ),
      book_artifact_ref(
        run_id,
        run_record("records/analyses", run_id),
        "analysis-record",
        GeneratedAnalysisRecord,
        "normalized analysis record for review",
      ),
      book_artifact_ref(
        run_id,
        run_record("records/memory-selections", run_id),
        "experience-memory-selection",
        GeneratedExperienceSelection,
        "Stage 2 experience memory selected for prompt evidence",
      ),
      book_artifact_ref(
        run_id,
        run_record("apps/moondesk-price-action/chart-frames", run_id),
        "chart-frame",
        GeneratedChartFrame,
        "operator chart frame projection for Moondesk",
      ),
      book_artifact_ref(
        run_id,
        run_record("records/book-commits", run_id),
        "book-commit-report",
        GeneratedBookCommit,
        "idempotent MoonBook commit report for all run artifacts",
      ),
    ],
  }
}

///|
pub fn BookWritePlan::paths(self : BookWritePlan) -> Array[String] {
  self.artifacts.map(fn(artifact) { artifact.path })
}