import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
  getProviderUsageMocks,
  getRunEmbeddedPiAgentMock,
  makeCfg,
  requireSessionStorePath,
  withTempHome,
} from "./reply.triggers.trigger-handling.test-harness.js";

type GetReplyFromConfig = typeof import("./reply.js").getReplyFromConfig;

const usageMocks = getProviderUsageMocks();

async function readSessionStore(storePath: string): Promise<Record<string, unknown>> {
  const raw = await readFile(storePath, "utf-8");
  return JSON.parse(raw) as Record<string, unknown>;
}

function pickFirstStoreEntry<T>(store: Record<string, unknown>): T | undefined {
  const entries = Object.values(store) as T[];
  return entries[0];
}

function getReplyFromConfigNow(getReplyFromConfig: () => GetReplyFromConfig): GetReplyFromConfig {
  return getReplyFromConfig();
}

function seedUsageSummary(): void {
  usageMocks.loadProviderUsageSummary.mockClear();
  usageMocks.loadProviderUsageSummary.mockResolvedValue({
    updatedAt: 0,
    providers: [
      {
        provider: "anthropic",
        displayName: "Anthropic",
        windows: [
          {
            label: "5h",
            usedPercent: 20,
          },
        ],
      },
    ],
  });
}

export function registerTriggerHandlingUsageSummaryCases(params: {
  getReplyFromConfig: () => GetReplyFromConfig;
}): void {
  describe("usage and status command handling", () => {
    it("shows status without invoking the agent", async () => {
      await withTempHome(async (home) => {
        const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();
        const getReplyFromConfig = getReplyFromConfigNow(params.getReplyFromConfig);
        seedUsageSummary();

        const res = await getReplyFromConfig(
          {
            Body: "/status",
            From: "+1000",
            To: "+2000",
            Provider: "whatsapp",
            SenderE164: "+1000",
            CommandAuthorized: true,
          },
          {},
          makeCfg(home),
        );

        const text = Array.isArray(res) ? res[0]?.text : res?.text;
        expect(text).toContain("Model:");
        expect(text).toContain("OpenClaw");
        expect(runEmbeddedPiAgentMock).not.toHaveBeenCalled();
      });
    });

    it("cycles usage footer modes and persists the final selection", async () => {
      await withTempHome(async (home) => {
        const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();
        const getReplyFromConfig = getReplyFromConfigNow(params.getReplyFromConfig);
        const cfg = makeCfg(home);
        cfg.session = { ...cfg.session, store: join(home, "usage-cycle.sessions.json") };
        const usageStorePath = requireSessionStorePath(cfg);

        const r0 = await getReplyFromConfig(
          {
            Body: "/usage on",
            From: "+1000",
            To: "+2000",
            Provider: "whatsapp",
            SenderE164: "+1000",
            CommandAuthorized: true,
          },
          undefined,
          cfg,
        );
        expect(String((Array.isArray(r0) ? r0[0]?.text : r0?.text) ?? "")).toContain(
          "Usage footer: tokens",
        );

        const r1 = await getReplyFromConfig(
          {
            Body: "/usage",
            From: "+1000",
            To: "+2000",
            Provider: "whatsapp",
            SenderE164: "+1000",
            CommandAuthorized: true,
          },
          undefined,
          cfg,
        );
        expect(String((Array.isArray(r1) ? r1[0]?.text : r1?.text) ?? "")).toContain(
          "Usage footer: full",
        );

        const r2 = await getReplyFromConfig(
          {
            Body: "/usage",
            From: "+1000",
            To: "+2000",
            Provider: "whatsapp",
            SenderE164: "+1000",
            CommandAuthorized: true,
          },
          undefined,
          cfg,
        );
        expect(String((Array.isArray(r2) ? r2[0]?.text : r2?.text) ?? "")).toContain(
          "Usage footer: off",
        );

        const r3 = await getReplyFromConfig(
          {
            Body: "/usage",
            From: "+1000",
            To: "+2000",
            Provider: "whatsapp",
            SenderE164: "+1000",
            CommandAuthorized: true,
          },
          undefined,
          cfg,
        );
        expect(String((Array.isArray(r3) ? r3[0]?.text : r3?.text) ?? "")).toContain(
          "Usage footer: tokens",
        );

        const finalStore = await readSessionStore(usageStorePath);
        expect(pickFirstStoreEntry<{ responseUsage?: string }>(finalStore)?.responseUsage).toBe(
          "tokens",
        );
        expect(runEmbeddedPiAgentMock).not.toHaveBeenCalled();
      });
    });
  });
}
