// @vitest-environment jsdom

import { mount } from "@vue/test-utils";
import { describe, expect, it } from "vitest";

import WorkbenchMessageCard from "./WorkbenchMessageCard.vue";

function componentStubs() {
  return {
    TraceTimeline: { template: "<div />" },
    LoadingState: { template: "<div />" },
    MessageMarkdown: { template: "<div />" },
    ApiErrorDiagnostic: { template: "<div />" },
    StatusBadge: { template: "<div />" }
  };
}

describe("WorkbenchMessageCard startup visibility", () => {
  it("shows one startup label before the first Kafka event", () => {
    const wrapper = mount(WorkbenchMessageCard, {
      props: {
        detailEnabled: false,
        message: {
          id: "msg_starting_agent",
          messageId: "msg_starting_agent",
          role: "agent",
          title: "Code Agent",
          text: "",
          status: "running",
          traceId: "trc_starting",
          sessionId: "ses_starting",
          createdAt: "2026-07-20T10:00:00.000Z",
        }
      },
      global: {
        stubs: {
          ...componentStubs(),
          LoadingState: { props: ["label"], template: "<div class='loading-state'>{{ label }}</div>" },
        }
      }
    });
    expect(wrapper.text()).toContain("启动中...");
    expect(wrapper.text()).not.toContain("思考中...");
    expect(wrapper.findAll(".loading-state")).toHaveLength(1);
    wrapper.unmount();
  });
});

describe("WorkbenchMessageCard metadata layout", () => {
  it("renders recent activity at the footer left and elapsed time at the footer right", () => {
    const now = Date.now();
    const wrapper = mount(WorkbenchMessageCard, {
      props: {
        detailEnabled: false,
        message: {
          id: "msg_timing_layout",
          messageId: "msg_timing_layout",
          role: "agent",
          title: "Code Agent",
          text: "",
          status: "running",
          traceId: "trc_timing_layout",
          sessionId: "ses_timing_layout",
          createdAt: new Date(now - 10_000).toISOString(),
          timing: {
            startedAt: new Date(now - 10_000).toISOString(),
            lastEventAt: new Date(now - 4_000).toISOString()
          }
        }
      },
      global: { stubs: componentStubs() }
    });

    const footer = wrapper.get(".message-meta-footer");
    expect(wrapper.find("article > header").exists()).toBe(false);
    expect(footer.get(".message-activity-meta").text()).toMatch(/^最近更新：\d+ 秒前$/u);
    expect(footer.get(".message-duration-meta").text()).toMatch(/^耗时 \d+ 秒$/u);
    expect(footer.get(".message-activity-group").element.firstElementChild).toBe(footer.get(".agent-running-indicator").element);
    expect(footer.get(".message-header-actions").element.lastElementChild).toBe(footer.get(".message-duration-meta").element);
    wrapper.unmount();
  });
});

describe("WorkbenchMessageCard trace projection", () => {
  it("does not duplicate Git source preparation outside the Trace timeline", () => {
    const wrapper = mount(WorkbenchMessageCard, {
      props: {
        detailEnabled: false,
        message: {
          id: "msg_fetch_agent",
          messageId: "msg_fetch_agent",
          role: "agent",
          title: "Code Agent",
          text: "",
          status: "running",
          traceId: "trc_fetch_banner",
          sessionId: "ses_fetch_banner",
          createdAt: "2026-07-20T10:00:00.000Z",
          runnerTrace: {
            traceId: "trc_fetch_banner",
            eventSource: "hwlab-kafka-sse",
            status: "running",
            eventCount: 1,
            events: [{
              sourceEventId: "evt_fetch_banner",
              sourceSeq: 5,
              type: "backend",
              status: "running",
              failureDomain: "infrastructure",
              failureCode: "git-mirror-fetch-in-progress",
              summary: "正在从 Git mirror 获取 Runner 源码",
              retryPhase: "initialAttempt",
              retryAttempt: 1,
              retryMaxAttempts: 3,
              createdAt: new Date().toISOString()
            }]
          }
        }
      },
      global: { stubs: componentStubs() }
    });
    expect(wrapper.find(".semantic-failure-banner").exists()).toBe(false);
    expect(wrapper.findComponent({ name: "TraceTimeline" }).exists()).toBe(true);
    wrapper.unmount();
  });

  it("does not duplicate infrastructure retry outside the Trace timeline", () => {
    const wrapper = mount(WorkbenchMessageCard, {
      props: {
        detailEnabled: false,
        message: {
          id: "msg_retry_agent",
          messageId: "msg_retry_agent",
          role: "agent",
          title: "Code Agent",
          text: "",
          status: "running",
          traceId: "trc_retry_banner",
          sessionId: "ses_retry_banner",
          createdAt: "2026-07-20T10:00:00.000Z",
          runnerTrace: {
            traceId: "trc_retry_banner",
            eventSource: "hwlab-kafka-sse",
            status: "running",
            eventCount: 1,
            events: [{
              sourceEventId: "evt_retry_banner",
              sourceSeq: 5,
              type: "backend",
              status: "retrying",
              failureDomain: "infrastructure",
              failureCode: "runner-image-registry-unreachable",
              summary: "Runner 镜像仓库暂时不可达",
              retryPhase: "retryScheduled",
              retryAttempt: 2,
              retryMaxAttempts: 5,
              nextRetryAt: new Date(Date.now() + 12_000).toISOString(),
              createdAt: new Date().toISOString()
            }]
          }
        }
      },
      global: {
        stubs: componentStubs()
      }
    });

    expect(wrapper.find(".semantic-failure-banner").exists()).toBe(false);
    expect(wrapper.findComponent({ name: "TraceTimeline" }).exists()).toBe(true);
    wrapper.unmount();
  });
});
