///|
pub(all) enum ExportProfile {
  PublicSummary
  InternalAudit
} derive(Debug, Eq, ToJson, FromJson)

///|
pub(all) enum ExportItemKind {
  EvidenceArtifact
  ReplayPlanArtifact
  OperatorViewArtifact
  ExportManifestArtifact
} derive(Debug, Eq, ToJson, FromJson)

///|
pub(all) struct ExportItem {
  run_id : @domain.RunId
  kind : ExportItemKind
  path : String
  schema_id : String
  public : Bool
  required : Bool
  purpose : String
} derive(Debug, Eq, ToJson, FromJson)

///|
pub(all) struct ExportManifest {
  run_id : @domain.RunId
  profile : ExportProfile
  destination_path : String
  includes_raw_model_exchange : Bool
  item_count : Int
  items : Array[ExportItem]
} derive(Debug, Eq, ToJson, FromJson)

///|
pub fn export_item(
  run_id : @domain.RunId,
  kind : ExportItemKind,
  path : String,
  schema_id : String,
  purpose : String,
  public? : Bool = true,
  required? : Bool = true,
) -> ExportItem {
  { run_id, kind, path, schema_id, public, required, purpose }
}

///|
fn anchor_public(anchor : @followup.FollowUpAnchor) -> Bool {
  match anchor.kind {
    PromptPacket | ModelExchange | BookCommit => false
    _ => true
  }
}

///|
fn include_anchor(
  profile : ExportProfile,
  anchor : @followup.FollowUpAnchor,
) -> Bool {
  profile is InternalAudit || anchor_public(anchor)
}

///|
fn append_anchor_items(
  items : Array[ExportItem],
  context : @followup.FollowUpContext,
  profile : ExportProfile,
) -> Unit {
  for anchor in context.anchors {
    if include_anchor(profile, anchor) {
      items.push(
        export_item(
          context.run_id,
          EvidenceArtifact,
          anchor.path,
          anchor.schema_id,
          anchor.purpose,
          public=anchor_public(anchor),
        ),
      )
    }
  }
}

///|
fn destination_path(run_id : @domain.RunId, profile : ExportProfile) -> String {
  let suffix = match profile {
    PublicSummary => "public-summary"
    InternalAudit => "internal-audit"
  }
  "exports/\{run_id}/\{suffix}.json"
}

///|
pub fn prepare_export_manifest(
  bundle : @workflow.AnalysisRunBundle,
  profile? : ExportProfile = InternalAudit,
) -> ExportManifest {
  let context = @followup.prepare_follow_up_context(bundle)
  let replay_plan = @replay.prepare_replay_plan(bundle)
  let run_id = context.run_id
  let items : Array[ExportItem] = []
  append_anchor_items(items, context, profile)
  items.push(
    export_item(
      run_id,
      ReplayPlanArtifact,
      "records/replays/\{run_id}.json",
      "replay-plan",
      "deterministic replay plan for exported evidence",
    ),
  )
  items.push(
    export_item(
      run_id,
      OperatorViewArtifact,
      "apps/moondesk-price-action/run-views/\{run_id}.json",
      "operator-run-view",
      "Moondesk operator run projection",
    ),
  )
  if replay_plan.can_replay {
    items.push(
      export_item(
        run_id,
        ExportManifestArtifact,
        destination_path(run_id, profile),
        "export-manifest",
        "export manifest for the selected profile",
      ),
    )
  }
  {
    run_id,
    profile,
    destination_path: destination_path(run_id, profile),
    includes_raw_model_exchange: profile is InternalAudit,
    item_count: items.length(),
    items,
  }
}