import { beforeEach, describe, expect, it, vi } from "vitest";

const apiClient = {
  get: vi.fn(),
  post: vi.fn(),
  put: vi.fn(),
  patch: vi.fn(),
  delete: vi.fn(),
};

vi.mock("./apiClient", () => ({
  default: apiClient,
}));

describe("orgfront devApi", () => {
  beforeEach(() => {
    apiClient.get.mockReset();
    apiClient.post.mockReset();
    apiClient.put.mockReset();
    apiClient.patch.mockReset();
    apiClient.delete.mockReset();

    apiClient.get.mockResolvedValue({ data: { ok: true } });
    apiClient.post.mockResolvedValue({ data: { ok: true } });
    apiClient.put.mockResolvedValue({ data: { ok: true } });
    apiClient.patch.mockResolvedValue({ data: { ok: true } });
    apiClient.delete.mockResolvedValue({ data: { ok: true } });
  });

  it("fetches dev resources with expected query parameters", async () => {
    const {
      fetchClients,
      fetchDevStats,
      fetchClient,
      fetchConsents,
      fetchDevAuditLogs,
      fetchMyTenants,
      listIdpConfigsForClient,
    } = await import("./devApi");

    await fetchClients();
    await fetchDevStats();
    await fetchClient("client-a");
    await fetchConsents("user-a", "client-a", "active");
    await fetchDevAuditLogs(10, "cursor-a", {
      action: "client.update",
      client_id: "client-a",
      status: "success",
      tenant_id: "tenant-a",
    });
    await fetchMyTenants();
    await listIdpConfigsForClient("client-a");

    expect(apiClient.get).toHaveBeenCalledWith("/dev/clients");
    expect(apiClient.get).toHaveBeenCalledWith("/dev/stats");
    expect(apiClient.get).toHaveBeenCalledWith("/dev/clients/client-a");
    expect(apiClient.get).toHaveBeenCalledWith("/dev/consents", {
      params: { subject: "user-a", client_id: "client-a", status: "active" },
    });
    expect(apiClient.get).toHaveBeenCalledWith("/dev/audit-logs", {
      params: {
        limit: 10,
        cursor: "cursor-a",
        action: "client.update",
        client_id: "client-a",
        status: "success",
        tenant_id: "tenant-a",
      },
    });
    expect(apiClient.get).toHaveBeenCalledWith("/dev/my-tenants");
    expect(apiClient.get).toHaveBeenCalledWith("/dev/clients/client-a/idps");
  });

  it("omits optional consent filters when they are empty or all", async () => {
    const { fetchConsents, revokeConsent } = await import("./devApi");

    await fetchConsents("user-a", undefined, "all");
    await revokeConsent("user-a");

    expect(apiClient.get).toHaveBeenCalledWith("/dev/consents", {
      params: { subject: "user-a" },
    });
    expect(apiClient.delete).toHaveBeenCalledWith("/dev/consents", {
      params: { subject: "user-a" },
    });
  });

  it("sends mutation requests to the documented dev endpoints", async () => {
    const {
      updateClientStatus,
      createClient,
      updateClient,
      rotateClientSecret,
      refreshHeadlessJwksCache,
      revokeHeadlessJwksCache,
      deleteClient,
      revokeConsent,
      createIdpConfigForClient,
      updateIdpConfig,
      deleteIdpConfig,
    } = await import("./devApi");

    await updateClientStatus("client-a", "inactive");
    await createClient({ id: "client-a", name: "Console App" });
    await updateClient("client-a", { name: "Console App Updated" });
    await rotateClientSecret("client-a");
    await refreshHeadlessJwksCache("client-a");
    await revokeHeadlessJwksCache("client-a");
    await deleteClient("client-a");
    await revokeConsent("user-a", "client-a");
    await createIdpConfigForClient({
      client_id: "client-a",
      provider_type: "oidc",
      display_name: "OIDC Provider",
      status: "active",
    });
    await updateIdpConfig("client-a", "idp-a", { status: "inactive" });
    await deleteIdpConfig("client-a", "idp-a");

    expect(apiClient.patch).toHaveBeenCalledWith(
      "/dev/clients/client-a/status",
      { status: "inactive" },
    );
    expect(apiClient.post).toHaveBeenCalledWith("/dev/clients", {
      id: "client-a",
      name: "Console App",
    });
    expect(apiClient.put).toHaveBeenCalledWith("/dev/clients/client-a", {
      name: "Console App Updated",
    });
    expect(apiClient.delete).toHaveBeenCalledWith("/dev/consents", {
      params: { subject: "user-a", client_id: "client-a" },
    });
    expect(apiClient.put).toHaveBeenCalledWith(
      "/dev/clients/client-a/idps/idp-a",
      { status: "inactive" },
    );
  });
});
