// @vitest-environment jsdom

import { screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";

import { renderWithProviders } from "test-utils/render-with-providers";
import { expectBlockValue } from "test-utils/widget-assertions";

const { useWidgetAPI } = vi.hoisted(() => ({ useWidgetAPI: vi.fn() }));
vi.mock("utils/proxy/use-widget-api", () => ({ default: useWidgetAPI }));

import Component from "./component";

describe("widgets/linkwarden/component", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("renders placeholders while loading", () => {
    useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });

    const { container } = renderWithProviders(<Component service={{ widget: { type: "linkwarden" } }} />, {
      settings: { hideErrors: false },
    });

    expect(container.querySelectorAll(".service-block")).toHaveLength(3);
    expect(screen.getByText("linkwarden.links")).toBeInTheDocument();
    expect(screen.getByText("linkwarden.collections")).toBeInTheDocument();
    expect(screen.getByText("linkwarden.tags")).toBeInTheDocument();
  });

  it("renders error UI when either endpoint errors", () => {
    useWidgetAPI.mockImplementation((_widget, endpoint) => {
      if (endpoint === "tags") return { data: undefined, error: { message: "nope" } };
      return { data: undefined, error: undefined };
    });

    renderWithProviders(<Component service={{ widget: { type: "linkwarden" } }} />, {
      settings: { hideErrors: false },
    });

    expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
    expect(screen.getByText("nope")).toBeInTheDocument();
  });

  it("computes totals from collections + tags arrays", async () => {
    useWidgetAPI.mockImplementation((_widget, endpoint) => {
      if (endpoint === "collections") {
        return {
          data: [{ _count: { links: 2 } }, { _count: { links: 3 } }],
          error: undefined,
        };
      }

      if (endpoint === "tags") {
        return { data: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }], error: undefined };
      }

      return { data: undefined, error: undefined };
    });

    const { container } = renderWithProviders(<Component service={{ widget: { type: "linkwarden" } }} />, {
      settings: { hideErrors: false },
    });

    expectBlockValue(container, "linkwarden.links", 5);
    expectBlockValue(container, "linkwarden.collections", 2);
    expectBlockValue(container, "linkwarden.tags", 4);
  });

  it("computes the tags total from the nested Linkwarden API response", () => {
    useWidgetAPI.mockImplementation((_widget, endpoint) => {
      if (endpoint === "collections") {
        return { data: { response: [{ _count: { links: 2 } }] }, error: undefined };
      }

      if (endpoint === "tags") {
        return { data: { data: { tags: [{ id: 1 }, { id: 2 }, { id: 3 }] } }, error: undefined };
      }

      return { data: undefined, error: undefined };
    });

    const { container } = renderWithProviders(<Component service={{ widget: { type: "linkwarden" } }} />, {
      settings: { hideErrors: false },
    });

    expectBlockValue(container, "linkwarden.links", 2);
    expectBlockValue(container, "linkwarden.collections", 1);
    expectBlockValue(container, "linkwarden.tags", 3);
  });
});
