import { describe, expect, test, vi } from "vitest";
import { screen } from "@testing-library/react";
import { renderWithProviders as render } from "../utils/test-utils";
import PrivacyPage from "../../app/(marketing)/privacy/page";
import TermsPage from "../../app/(marketing)/terms/page";
import CookiesPage from "../../app/(marketing)/cookies/page";
import messages from "../../messages/en/index";

vi.mock("../../app/components/sections/ContentBanner", () => ({
  default: ({ post, variant, updatedLabel }) => (
    <section data-testid="content-banner" data-variant={variant}>
      <h1>{post.frontmatter.title}</h1>
      <p>{post.frontmatter.description}</p>
      {updatedLabel ? (
        <div>
          <span>{post.frontmatter.author}</span>
          <span>{updatedLabel}</span>
        </div>
      ) : null}
    </section>
  ),
}));

describe("legal document pages", () => {
  test("privacy page covers hosting, recommendations, and campus privacy", () => {
    render(<PrivacyPage />);
    const page = messages.pages.privacy;

    expect(screen.getByTestId("content-banner")).toHaveAttribute(
      "data-variant",
      "guide",
    );
    expect(
      screen.getByRole("heading", { level: 1, name: page.banner.title }),
    ).toBeInTheDocument();
    expect(screen.getByText(page.banner.description)).toBeInTheDocument();
    expect(screen.getByText(page.updated)).toBeInTheDocument();
    expect(
      screen.getByRole("heading", { level: 2, name: "What we collect" }),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("heading", { level: 2, name: "Where it is stored" }),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("heading", { level: 2, name: "Recommendations" }),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("heading", { level: 2, name: "Your account" }),
    ).toBeInTheDocument();
    expect(screen.getByText(/servers MEDLab operates/)).toBeInTheDocument();
    expect(screen.getByText(/Amazon SES/)).toBeInTheDocument();
    expect(
      screen.getByText(/not an AI or machine-learning model/),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("link", { name: "Media Economies Design Lab" }),
    ).toHaveAttribute("href", "https://www.colorado.edu/lab/medlab/");
    expect(
      screen.getByRole("link", { name: "Privacy Statement" }),
    ).toHaveAttribute(
      "href",
      "https://www.colorado.edu/compliance/policies/privacy-statement",
    );
  });

  test("terms page covers licenses and the templates API, not ranking", () => {
    render(<TermsPage />);
    const page = messages.pages.terms;

    expect(
      screen.getByRole("heading", { level: 1, name: page.banner.title }),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("heading", { level: 2, name: "Licenses" }),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("heading", { level: 2, name: "Templates API" }),
    ).toBeInTheDocument();
    expect(
      screen.queryByRole("heading", { name: "Recommendations" }),
    ).not.toBeInTheDocument();
    expect(
      screen.getByRole("link", { name: "CC BY-SA 4.0" }),
    ).toHaveAttribute(
      "href",
      "https://creativecommons.org/licenses/by-sa/4.0/",
    );
    expect(
      screen.getByRole("link", { name: "GPL-3.0 or later" }),
    ).toHaveAttribute("href", "https://www.gnu.org/licenses/gpl-3.0.html");
    expect(screen.getByRole("link", { name: "Gitea" })).toHaveAttribute(
      "href",
      "https://git.medlab.host/CommunityRule/community-rule",
    );
    expect(
      screen.getByRole("link", { name: "GET /api/templates" }),
    ).toHaveAttribute("href", "/api/templates");
  });

  test("cookies settings page explains there are no optional cookies", () => {
    render(<CookiesPage />);
    const page = messages.pages.cookies;

    expect(
      screen.getByRole("heading", { level: 1, name: page.banner.title }),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("heading", { level: 2, name: "The session cookie" }),
    ).toBeInTheDocument();
    expect(
      screen.getByText(/There are no advertising or analytics cookies to turn off/),
    ).toBeInTheDocument();
    expect(screen.getByText(/cr_session/)).toBeInTheDocument();
    expect(screen.getByText(/cr_rule_claim/)).toBeInTheDocument();
    expect(
      screen.getAllByRole("link", { name: "Privacy Policy" })[0],
    ).toHaveAttribute("href", "/privacy");
  });
});
