// The Lucia project.
// Copyright 2026-2026 DSP, inc.  All rights reserved.
// Authors:
//   imacat.yang@dsp.im (imacat), 2026/03/05

import { describe, it, expect, beforeEach, vi } from "vitest";
import { setActivePinia, createPinia } from "pinia";
import { http, HttpResponse } from "msw";
import { server } from "@/mocks/node.js";
import { findRequest, captureRequest } from "@/mocks/request-log.js";

vi.mock("@/module/apiError.js", () => ({
  default: vi.fn(),
}));

import { usePerformanceStore } from "@/stores/performance";

describe("performanceStore", () => {
  let store;

  beforeEach(() => {
    setActivePinia(createPinia());
    store = usePerformanceStore();
    document.cookie = "luciaToken=fake-test-token";
  });

  it("has correct default state", () => {
    expect(store.allPerformanceData).toBeNull();
    expect(store.freqChartData).toBeNull();
  });

  it("performanceData getter returns allPerformanceData", () => {
    store.allPerformanceData = { time: {}, freq: {} };
    expect(store.performanceData).toEqual({ time: {}, freq: {} });
  });

  describe("getPerformance", () => {
    it("fetches log performance data", async () => {
      const mockData = { time: { charts: [] }, freq: { charts: [] } };
      server.use(
        http.get("/api/logs/:id/performance", ({ request }) => {
          captureRequest("GET", new URL(request.url).pathname);
          return HttpResponse.json(mockData);
        }),
      );

      await store.getPerformance("log", 1);

      const reqs = findRequest("GET", "/api/logs/1/performance");
      expect(reqs).toHaveLength(1);
      expect(store.allPerformanceData).toEqual(mockData);
    });

    it("fetches filter performance data", async () => {
      server.use(
        http.get("/api/filters/:id/performance", ({ request }) => {
          captureRequest("GET", new URL(request.url).pathname);
          return HttpResponse.json({ time: {} });
        }),
      );

      await store.getPerformance("filter", 5);

      const reqs = findRequest("GET", "/api/filters/5/performance");
      expect(reqs).toHaveLength(1);
    });

    it("does not throw on API failure", async () => {
      server.use(
        http.get("/api/logs/:id/performance", () =>
          new HttpResponse(null, { status: 500 }),
        ),
      );

      // Should not throw - apiError handles it
      await expect(store.getPerformance("log", 1)).resolves.not.toThrow();
      expect(store.allPerformanceData).toBeNull();
    });
  });

  it("setFreqChartData sets data", () => {
    const data = { labels: [], datasets: [] };
    store.setFreqChartData(data);
    expect(store.freqChartData).toEqual(data);
  });

  it("setFreqChartOptions sets options", () => {
    const opts = { responsive: true };
    store.setFreqChartOptions(opts);
    expect(store.freqChartOptions).toEqual(opts);
  });

  it("setFreqChartXData sets x data", () => {
    const xData = { minX: 0, maxX: 100 };
    store.setFreqChartXData(xData);
    expect(store.freqChartXData).toEqual(xData);
  });
});
