import { describe, it, expect, vi } from "vitest";
import { markdownToHtml } from "../../lib/content";

describe("Markdown Processing", () => {
  describe("markdownToHtml", () => {
    it("converts basic markdown to HTML", () => {
      const markdown = "# Heading\n\nThis is a paragraph.";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<h1>Heading</h1>");
      expect(result).toContain("This is a paragraph.");
    });

    it("converts bold text", () => {
      const markdown = "This is **bold** text.";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<strong>bold</strong>");
    });

    it("converts italic text", () => {
      const markdown = "This is *italic* text.";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<em>italic</em>");
    });

    it("converts links", () => {
      const markdown = "Visit [Google](https://google.com) for search.";
      const result = markdownToHtml(markdown);

      expect(result).toContain('<a href="https://google.com">Google</a>');
    });

    it("converts line breaks to <br> tags", () => {
      const markdown = "Line 1\nLine 2\nLine 3";
      const result = markdownToHtml(markdown);

      expect(result).toContain("Line 1<br>");
      expect(result).toContain("Line 2<br>");
      expect(result).toContain("Line 3");
    });

    it("converts multiple line breaks to paragraph breaks", () => {
      const markdown = "Paragraph 1\n\nParagraph 2\n\nParagraph 3";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<p>Paragraph 1</p>");
      expect(result).toContain("<p>Paragraph 2</p>");
      expect(result).toContain("<p>Paragraph 3</p>");
    });

    it("adds md-gap class to paragraphs", () => {
      const markdown = "Paragraph 1\n\nParagraph 2";
      const result = markdownToHtml(markdown);

      expect(result).toContain('<p class="md-gap">Paragraph 1</p>');
      expect(result).toContain('<p class="md-gap">Paragraph 2</p>');
    });

    it("converts unordered lists", () => {
      const markdown = "- Item 1\n- Item 2\n- Item 3";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<ul>");
      expect(result).toContain("<li>Item 1</li>");
      expect(result).toContain("<li>Item 2</li>");
      expect(result).toContain("<li>Item 3</li>");
      expect(result).toContain("</ul>");
    });

    it("converts ordered lists", () => {
      const markdown = "1. First item\n2. Second item\n3. Third item";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<ol>");
      expect(result).toContain("<li>First item</li>");
      expect(result).toContain("<li>Second item</li>");
      expect(result).toContain("<li>Third item</li>");
      expect(result).toContain("</ol>");
    });

    it("converts code blocks", () => {
      const markdown = "```javascript\nconst x = 1;\n```";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<pre>");
      expect(result).toContain("<code>");
      expect(result).toContain("const x = 1;");
    });

    it("converts inline code", () => {
      const markdown = "Use `console.log()` to debug.";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<code>console.log()</code>");
    });

    it("converts blockquotes", () => {
      const markdown = "> This is a quote\n> with multiple lines";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<blockquote>");
      expect(result).toContain("This is a quote");
      expect(result).toContain("with multiple lines");
      expect(result).toContain("</blockquote>");
    });

    it("converts horizontal rules", () => {
      const markdown = "Text above\n\n---\n\nText below";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<hr>");
    });

    it("handles mixed content", () => {
      const markdown =
        "# Title\n\nThis is a **bold** paragraph with a [link](https://example.com).\n\n- List item 1\n- List item 2\n\nAnother paragraph with `code`.";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<h1>Title</h1>");
      expect(result).toContain("<strong>bold</strong>");
      expect(result).toContain('<a href="https://example.com">link</a>');
      expect(result).toContain("<ul>");
      expect(result).toContain("<li>List item 1</li>");
      expect(result).toContain("<li>List item 2</li>");
      expect(result).toContain("<code>code</code>");
    });

    it("handles empty input", () => {
      const result = markdownToHtml("");
      expect(result).toBe("");
    });

    it("handles whitespace-only input", () => {
      const result = markdownToHtml("   \n\n   ");
      expect(result).toBe("");
    });

    it("preserves HTML entities", () => {
      const markdown = "Use &lt; and &gt; for HTML tags.";
      const result = markdownToHtml(markdown);

      expect(result).toContain("&lt;");
      expect(result).toContain("&gt;");
    });

    it("handles complex nested structures", () => {
      const markdown =
        "# Main Title\n\n## Subtitle\n\nThis is a paragraph with **bold** and *italic* text.\n\n1. First item with `code`\n2. Second item with [link](https://example.com)\n\n> This is a quote\n> with **bold** text\n\n```javascript\nconst example = 'test';\n```";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<h1>Main Title</h1>");
      expect(result).toContain("<h2>Subtitle</h2>");
      expect(result).toContain("<strong>bold</strong>");
      expect(result).toContain("<em>italic</em>");
      expect(result).toContain("<ol>");
      expect(result).toContain("<code>code</code>");
      expect(result).toContain('<a href="https://example.com">link</a>');
      expect(result).toContain("<blockquote>");
      expect(result).toContain("<pre>");
    });

    it("handles malformed markdown gracefully", () => {
      const markdown = "**Unclosed bold\n\n*Unclosed italic\n\n[Unclosed link";
      const result = markdownToHtml(markdown);

      // Should not throw an error and should handle gracefully
      expect(typeof result).toBe("string");
      expect(result.length).toBeGreaterThan(0);
    });

    it("converts headings of different levels", () => {
      const markdown = "# H1\n## H2\n### H3\n#### H4\n##### H5\n###### H6";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<h1>H1</h1>");
      expect(result).toContain("<h2>H2</h2>");
      expect(result).toContain("<h3>H3</h3>");
      expect(result).toContain("<h4>H4</h4>");
      expect(result).toContain("<h5>H5</h5>");
      expect(result).toContain("<h6>H6</h6>");
    });

    it("handles tables", () => {
      const markdown =
        "| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1   | Cell 2   |";
      const result = markdownToHtml(markdown);

      expect(result).toContain("<table>");
      expect(result).toContain("<thead>");
      expect(result).toContain("<th>Header 1</th>");
      expect(result).toContain("<th>Header 2</th>");
      expect(result).toContain("<tbody>");
      expect(result).toContain("<td>Cell 1</td>");
      expect(result).toContain("<td>Cell 2</td>");
    });
  });
});
