import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
  addSubagentRunForTests,
  resetSubagentRegistryForTests,
} from "../../agents/subagent-registry.js";
import type { OpenClawConfig } from "../../config/config.js";
import { buildStatusReply } from "./commands-status.js";
import { buildCommandTestParams } from "./commands.test-harness.js";

describe("buildStatusReply subagent summary", () => {
  beforeEach(() => {
    resetSubagentRegistryForTests();
  });

  afterEach(() => {
    resetSubagentRegistryForTests();
  });

  it("counts ended orchestrators with active descendants as active", async () => {
    const parentKey = "agent:main:subagent:status-ended-parent";
    addSubagentRunForTests({
      runId: "run-status-ended-parent",
      childSessionKey: parentKey,
      requesterSessionKey: "agent:main:main",
      requesterDisplayKey: "main",
      task: "status orchestrator",
      cleanup: "keep",
      createdAt: Date.now() - 120_000,
      startedAt: Date.now() - 120_000,
      endedAt: Date.now() - 110_000,
      outcome: { status: "ok" },
    });
    addSubagentRunForTests({
      runId: "run-status-active-child",
      childSessionKey: "agent:main:subagent:status-ended-parent:subagent:child",
      requesterSessionKey: parentKey,
      requesterDisplayKey: "subagent:status-ended-parent",
      task: "status child still running",
      cleanup: "keep",
      createdAt: Date.now() - 60_000,
      startedAt: Date.now() - 60_000,
    });

    const cfg = {
      commands: { text: true },
      channels: { whatsapp: { allowFrom: ["*"] } },
      session: { mainKey: "main", scope: "per-sender" },
    } as OpenClawConfig;
    const params = buildCommandTestParams("/status", cfg);
    const reply = await buildStatusReply({
      cfg,
      command: params.command,
      sessionEntry: params.sessionEntry,
      sessionKey: params.sessionKey,
      parentSessionKey: params.sessionKey,
      sessionScope: params.sessionScope,
      storePath: params.storePath,
      provider: "anthropic",
      model: "claude-opus-4-5",
      contextTokens: 0,
      resolvedThinkLevel: params.resolvedThinkLevel,
      resolvedFastMode: false,
      resolvedVerboseLevel: params.resolvedVerboseLevel,
      resolvedReasoningLevel: params.resolvedReasoningLevel,
      resolvedElevatedLevel: params.resolvedElevatedLevel,
      resolveDefaultThinkingLevel: params.resolveDefaultThinkingLevel,
      isGroup: params.isGroup,
      defaultGroupActivation: params.defaultGroupActivation,
    });

    expect(reply?.text).toContain("🤖 Subagents: 1 active");
  });

  it("dedupes stale rows in the verbose subagent status summary", async () => {
    const childSessionKey = "agent:main:subagent:status-dedupe-worker";
    addSubagentRunForTests({
      runId: "run-status-current",
      childSessionKey,
      requesterSessionKey: "agent:main:main",
      requesterDisplayKey: "main",
      task: "current status worker",
      cleanup: "keep",
      createdAt: Date.now() - 60_000,
      startedAt: Date.now() - 60_000,
    });
    addSubagentRunForTests({
      runId: "run-status-stale",
      childSessionKey,
      requesterSessionKey: "agent:main:main",
      requesterDisplayKey: "main",
      task: "stale status worker",
      cleanup: "keep",
      createdAt: Date.now() - 120_000,
      startedAt: Date.now() - 120_000,
      endedAt: Date.now() - 90_000,
      outcome: { status: "ok" },
    });

    const cfg = {
      commands: { text: true },
      channels: { whatsapp: { allowFrom: ["*"] } },
      session: { mainKey: "main", scope: "per-sender" },
    } as OpenClawConfig;
    const params = buildCommandTestParams("/status", cfg);
    const reply = await buildStatusReply({
      cfg,
      command: params.command,
      sessionEntry: params.sessionEntry,
      sessionKey: params.sessionKey,
      parentSessionKey: params.sessionKey,
      sessionScope: params.sessionScope,
      storePath: params.storePath,
      provider: "anthropic",
      model: "claude-opus-4-5",
      contextTokens: 0,
      resolvedThinkLevel: params.resolvedThinkLevel,
      resolvedFastMode: false,
      resolvedVerboseLevel: "on",
      resolvedReasoningLevel: params.resolvedReasoningLevel,
      resolvedElevatedLevel: params.resolvedElevatedLevel,
      resolveDefaultThinkingLevel: params.resolveDefaultThinkingLevel,
      isGroup: params.isGroup,
      defaultGroupActivation: params.defaultGroupActivation,
    });

    expect(reply?.text).toContain("🤖 Subagents: 1 active");
    expect(reply?.text).not.toContain("· 1 done");
  });

  it("does not count a child session that moved to a newer parent in the old parent's status", async () => {
    const oldParentKey = "agent:main:subagent:status-old-parent";
    const newParentKey = "agent:main:subagent:status-new-parent";
    const childSessionKey = "agent:main:subagent:status-shared-child";
    addSubagentRunForTests({
      runId: "run-status-old-parent",
      childSessionKey: oldParentKey,
      requesterSessionKey: "agent:main:main",
      requesterDisplayKey: "main",
      task: "old parent",
      cleanup: "keep",
      createdAt: Date.now() - 120_000,
      startedAt: Date.now() - 120_000,
    });
    addSubagentRunForTests({
      runId: "run-status-new-parent",
      childSessionKey: newParentKey,
      requesterSessionKey: "agent:main:main",
      requesterDisplayKey: "main",
      task: "new parent",
      cleanup: "keep",
      createdAt: Date.now() - 90_000,
      startedAt: Date.now() - 90_000,
    });
    addSubagentRunForTests({
      runId: "run-status-child-stale-old-parent",
      childSessionKey,
      requesterSessionKey: oldParentKey,
      requesterDisplayKey: oldParentKey,
      controllerSessionKey: oldParentKey,
      task: "stale old parent child",
      cleanup: "keep",
      createdAt: Date.now() - 60_000,
      startedAt: Date.now() - 60_000,
    });
    addSubagentRunForTests({
      runId: "run-status-child-current-new-parent",
      childSessionKey,
      requesterSessionKey: newParentKey,
      requesterDisplayKey: newParentKey,
      controllerSessionKey: newParentKey,
      task: "current new parent child",
      cleanup: "keep",
      createdAt: Date.now() - 30_000,
      startedAt: Date.now() - 30_000,
    });

    const cfg = {
      commands: { text: true },
      channels: { whatsapp: { allowFrom: ["*"] } },
      session: { mainKey: "main", scope: "per-sender" },
    } as OpenClawConfig;
    const params = buildCommandTestParams("/status", cfg);
    const reply = await buildStatusReply({
      cfg,
      command: params.command,
      sessionEntry: params.sessionEntry,
      sessionKey: oldParentKey,
      parentSessionKey: oldParentKey,
      sessionScope: params.sessionScope,
      storePath: params.storePath,
      provider: "anthropic",
      model: "claude-opus-4-5",
      contextTokens: 0,
      resolvedThinkLevel: params.resolvedThinkLevel,
      resolvedFastMode: false,
      resolvedVerboseLevel: "on",
      resolvedReasoningLevel: params.resolvedReasoningLevel,
      resolvedElevatedLevel: params.resolvedElevatedLevel,
      resolveDefaultThinkingLevel: params.resolveDefaultThinkingLevel,
      isGroup: params.isGroup,
      defaultGroupActivation: params.defaultGroupActivation,
    });

    expect(reply?.text).not.toContain("🤖 Subagents: 1 active");
    expect(reply?.text).not.toContain("stale old parent child");
  });
});
