/**
 * Generated-canvas demo fixture — models an agent creating a Markdown artifact
 * so mock mode can demonstrate chat-to-drawer file navigation without an LLM.
 */
import type {
  ActionEvent,
  MessageEvent,
  ObservationEvent,
  OpenHandsEvent,
} from "#/types/agent-server/core";
import { SecurityRisk } from "#/types/agent-server/core";
import type { FileEditorAction } from "#/types/agent-server/core/base/action";
import type { FileEditorObservation } from "#/types/agent-server/core/base/observation";

export const CANVAS_DEMO_CONVERSATION_ID = "canvas-demo";
export const CANVAS_DEMO_FILE_PATH = "canvas.md";

export const CANVAS_DEMO_MARKDOWN = [
  "# Agent Canvas Demo",
  "",
  "This report was generated by the agent and opened from the file chip in chat.",
  "",
  "## Release snapshot",
  "",
  "| Area | Status | Next step |",
  "| --- | --- | --- |",
  "| Conversation UX | Ready | Validate the drawer flow |",
  "| Markdown preview | Ready | Review rich rendering |",
  "| Accessibility | Ready | Confirm keyboard navigation |",
  "",
  "## Highlights",
  "",
  "- The artifact stays in the conversation workspace.",
  "- The chat chip is the compact entry point.",
  "- The Files drawer provides the full rich Markdown view.",
].join("\n");

const BASE_TIME = Date.UTC(2026, 6, 29, 12, 0, 0);
const timestamp = (offsetSeconds: number) =>
  new Date(BASE_TIME + offsetSeconds * 1000).toISOString();

/**
 * Creates a minimal message event with the same SDK shape used by history.
 */
function createMessage(
  id: string,
  role: "user" | "assistant",
  text: string,
  offsetSeconds: number,
): MessageEvent {
  return {
    id,
    timestamp: timestamp(offsetSeconds),
    source: role === "user" ? "user" : "agent",
    llm_message: {
      role,
      content: [{ type: "text", text }],
    },
    activated_skills: [],
    extended_content: [],
  };
}

const createCanvasAction: ActionEvent<FileEditorAction> = {
  id: "canvas-demo-action",
  timestamp: timestamp(1),
  source: "agent",
  thought: [],
  thinking_blocks: [],
  action: {
    kind: "FileEditorAction",
    command: "create",
    path: CANVAS_DEMO_FILE_PATH,
    file_text: CANVAS_DEMO_MARKDOWN,
    old_str: null,
    new_str: null,
    insert_line: null,
    view_range: null,
  },
  tool_name: "file_editor",
  tool_call_id: "canvas-demo-tool-call",
  tool_call: {
    id: "canvas-demo-tool-call",
    type: "function",
    function: {
      name: "file_editor",
      arguments: JSON.stringify({
        command: "create",
        path: CANVAS_DEMO_FILE_PATH,
      }),
    },
  },
  llm_response_id: "canvas-demo-response",
  security_risk: SecurityRisk.LOW,
};

const createCanvasObservation: ObservationEvent<FileEditorObservation> = {
  id: "canvas-demo-observation",
  timestamp: timestamp(2),
  source: "environment",
  tool_name: "file_editor",
  tool_call_id: "canvas-demo-tool-call",
  action_id: createCanvasAction.id,
  observation: {
    kind: "FileEditorObservation",
    command: "create",
    output: `Created ${CANVAS_DEMO_FILE_PATH}`,
    path: CANVAS_DEMO_FILE_PATH,
    prev_exist: false,
    old_content: null,
    new_content: CANVAS_DEMO_MARKDOWN,
    error: null,
  },
};

export const CANVAS_DEMO_EVENTS: OpenHandsEvent[] = [
  createMessage(
    "canvas-demo-user",
    "user",
    "Create a concise project canvas as a Markdown report.",
    0,
  ),
  createCanvasAction,
  createCanvasObservation,
  createMessage(
    "canvas-demo-agent",
    "assistant",
    "I generated the project canvas. Open it from the file chip above.",
    3,
  ),
];
