import { classifyBlock } from "../../../src/adapters/reader/structure/classify-block";
import { features } from "./feature-fixtures";

describe("classifyBlock", () => {
  describe("nav", () => {
    it("classifies a <nav> tag as nav", () => {
      const r = classifyBlock(features({ tagName: "nav", linkCount: 5, linkDensity: 0.8 }));
      expect(r.kind).toBe("nav");
    });

    it("classifies role=navigation as nav", () => {
      const r = classifyBlock(features({ role: "navigation", linkCount: 5, linkDensity: 0.8 }));
      expect(r.kind).toBe("nav");
    });

    it("classifies a menu-like id/class list as nav", () => {
      const r = classifyBlock(
        features({
          tagName: "div",
          idClass: "main-nav",
          linkCount: 6,
          linkDensity: 0.9,
          listItemCount: 6,
          shortLinkRatio: 1,
        }),
      );
      expect(r.kind).toBe("nav");
    });

    it("does not classify long prose with few links as nav", () => {
      const r = classifyBlock(
        features({ tagName: "div", textLength: 1500, pCount: 5, linkDensity: 0.1, linkCount: 1 }),
      );
      expect(r.kind).not.toBe("nav");
    });
  });

  describe("header", () => {
    it("classifies a <header> tag as header", () => {
      const r = classifyBlock(features({ tagName: "header", domOrderRatio: 0.02 }));
      expect(r.kind).toBe("header");
    });

    it("classifies role=banner as header", () => {
      const r = classifyBlock(features({ role: "banner", domOrderRatio: 0.02 }));
      expect(r.kind).toBe("header");
    });

    it("boosts confidence for a header containing a logo and a nav child", () => {
      const withExtras = classifyBlock(
        features({ tagName: "header", domOrderRatio: 0.02, containsLogoImage: true, containsNavChild: true }),
      );
      const bare = classifyBlock(features({ tagName: "header", domOrderRatio: 0.5 }));
      expect(withExtras.scores.header ?? 0).toBeGreaterThan(bare.scores.header ?? 0);
    });
  });

  describe("footer", () => {
    it("classifies a <footer> tag as footer", () => {
      const r = classifyBlock(features({ tagName: "footer", domOrderRatio: 0.95 }));
      expect(r.kind).toBe("footer");
    });

    it("classifies role=contentinfo as footer", () => {
      const r = classifyBlock(features({ role: "contentinfo", domOrderRatio: 0.95 }));
      expect(r.kind).toBe("footer");
    });

    it("boosts confidence for copyright-shaped id/class text", () => {
      const r = classifyBlock(
        features({ tagName: "footer", idClass: "site-footer copyright", domOrderRatio: 0.95 }),
      );
      expect(r.kind).toBe("footer");
      expect(r.scores.footer ?? 0).toBeGreaterThanOrEqual(85);
    });
  });

  describe("aside", () => {
    it("classifies an <aside> tag as aside", () => {
      const r = classifyBlock(features({ tagName: "aside" }));
      expect(r.kind).toBe("aside");
    });

    it("classifies role=complementary as aside", () => {
      const r = classifyBlock(features({ role: "complementary" }));
      expect(r.kind).toBe("aside");
    });
  });

  describe("main", () => {
    it("classifies a <main> tag as main", () => {
      const r = classifyBlock(features({ tagName: "main", docTextShare: 0.5, linkDensity: 0.1 }));
      expect(r.kind).toBe("main");
    });

    it("classifies role=main as main", () => {
      const r = classifyBlock(features({ role: "main", docTextShare: 0.5, linkDensity: 0.1 }));
      expect(r.kind).toBe("main");
    });

    it("prefers a high doc-text-share, low-link-density div with the h1 as main", () => {
      const r = classifyBlock(
        features({
          tagName: "div",
          idClass: "page-content",
          docTextShare: 0.6,
          linkDensity: 0.1,
          containsDocumentH1: true,
        }),
      );
      expect(r.kind).toBe("main");
    });

    it("does not classify a link-dense div as main even with a big doc share", () => {
      const r = classifyBlock(features({ tagName: "div", docTextShare: 0.6, linkDensity: 0.8 }));
      expect(r.kind).not.toBe("main");
    });
  });

  describe("article", () => {
    it("classifies an <article> tag as article", () => {
      const r = classifyBlock(features({ tagName: "article" }));
      expect(r.kind).toBe("article");
    });

    it("classifies prose-shaped content (headings + paragraphs + commas, low link density) as article", () => {
      const r = classifyBlock(
        features({
          tagName: "div",
          headingCount: 1,
          pCount: 5,
          textLength: 800,
          commaCount: 15,
          linkDensity: 0.1,
          textDensity: 25,
        }),
      );
      expect(r.kind).toBe("article");
    });
  });

  describe("cardList", () => {
    it("classifies a strong repeated-sibling group as cardList", () => {
      const r = classifyBlock(
        features({
          tagName: "div",
          idClass: "product-grid",
          siblingRepeatCount: 6,
          siblingSimilarity: 0.9,
          pCount: 6,
          textLength: 400,
        }),
      );
      expect(r.kind).toBe("cardList");
    });

    it("does not classify a sparse, textless repeat as cardList", () => {
      const r = classifyBlock(
        features({
          tagName: "div",
          siblingRepeatCount: 4,
          siblingSimilarity: 0.85,
          pCount: 0,
          textLength: 20,
        }),
      );
      expect(r.kind).not.toBe("cardList");
    });
  });

  describe("list", () => {
    it("classifies a <ul> with items as list", () => {
      const r = classifyBlock(features({ tagName: "ul", listItemCount: 4 }));
      expect(r.kind).toBe("list");
    });
  });

  describe("table / form / embed", () => {
    it("classifies <table> as table", () => {
      expect(classifyBlock(features({ tagName: "table" })).kind).toBe("table");
    });

    it("classifies <form> with controls as form", () => {
      const r = classifyBlock(features({ tagName: "form", formControlCount: 3, buttonCount: 1 }));
      expect(r.kind).toBe("form");
    });

    it("classifies <iframe> as embed", () => {
      expect(classifyBlock(features({ tagName: "iframe" })).kind).toBe("embed");
    });
  });

  describe("consent", () => {
    it("classifies a cookie banner (id/class carries both topic and action words) as consent", () => {
      const r = classifyBlock(
        features({
          tagName: "div",
          idClass: "cookie-banner accepter refuser",
          buttonCount: 2,
          textLength: 300,
        }),
      );
      expect(r.kind).toBe("consent");
    });

    it("does not classify a long, cookie-worded article as consent", () => {
      const r = classifyBlock(
        features({
          tagName: "article",
          idClass: "cookie-recipe accepter",
          textLength: 5000,
          pCount: 10,
          headingCount: 2,
        }),
      );
      expect(r.kind).not.toBe("consent");
    });
  });

  describe("section", () => {
    it("classifies a <section> with a heading and paragraph as section", () => {
      const r = classifyBlock(features({ tagName: "section", headingCount: 1, pCount: 1 }));
      expect(r.kind).toBe("section");
    });
  });

  describe("contentGroup fallback", () => {
    it("falls back to contentGroup for a featureless div", () => {
      const r = classifyBlock(features());
      expect(r.kind).toBe("contentGroup");
    });

    it("gives the fallback a low confidence", () => {
      const r = classifyBlock(features());
      expect(r.confidence).toBeLessThan(0.5);
    });
  });

  describe("geometry is never load-bearing alone", () => {
    it("a nav with ONLY a geometry hit (no tag/role/idClass/link signal) is not classified as nav", () => {
      const r = classifyBlock(
        features({
          tagName: "div",
          geometry: {
            top: 0,
            left: 0,
            width: 1200,
            height: 40,
            viewportWidth: 1280,
            viewportHeight: 800,
            cssPosition: "sticky",
            overflowX: "visible",
            zIndex: 10,
            isSticky: true,
            isAboveFold: true,
            isFullWidth: true,
            isNarrowColumn: false,
            isRowLayout: false,
            scrollsHorizontally: false,
          },
        }),
      );
      expect(r.kind).not.toBe("nav");
    });
  });
});
