// Copyright (C) 2023-2026  Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

const std = @import("std");

const Page = @import("Page.zig");
const URL = @import("URL.zig");
const TreeWalker = @import("webapi/TreeWalker.zig");
const CData = @import("webapi/CData.zig");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const isAllWhitespace = @import("../string.zig").isAllWhitespace;

pub const Opts = struct {
    // Options for future customization (e.g., dialect)
};

const State = struct {
    const ListType = enum { ordered, unordered };
    const ListState = struct {
        type: ListType,
        index: usize,
    };

    list_depth: usize = 0,
    list_stack: [32]ListState = undefined,
    pre_node: ?*Node = null,
    in_code: bool = false,
    in_table: bool = false,
    table_row_index: usize = 0,
    table_col_count: usize = 0,
    last_char_was_newline: bool = true,
};

fn shouldAddSpacing(tag: Element.Tag) bool {
    return switch (tag) {
        .p, .h1, .h2, .h3, .h4, .h5, .h6, .blockquote, .pre, .table => true,
        else => false,
    };
}

fn isLayoutBlock(tag: Element.Tag) bool {
    return switch (tag) {
        .main, .section, .article, .nav, .aside, .header, .footer, .div, .ul, .ol => true,
        else => false,
    };
}

fn isStandaloneAnchor(el: *Element) bool {
    const node = el.asNode();
    const parent = node.parentNode() orelse return false;
    const parent_el = parent.is(Element) orelse return false;

    if (!isLayoutBlock(parent_el.getTag())) return false;

    var prev = node.previousSibling();
    while (prev) |p| : (prev = p.previousSibling()) {
        if (isSignificantText(p)) return false;
        if (p.is(Element)) |pe| {
            if (isVisibleElement(pe)) break;
        }
    }

    var next = node.nextSibling();
    while (next) |n| : (next = n.nextSibling()) {
        if (isSignificantText(n)) return false;
        if (n.is(Element)) |ne| {
            if (isVisibleElement(ne)) break;
        }
    }

    return true;
}

fn isSignificantText(node: *Node) bool {
    const text = node.is(Node.CData.Text) orelse return false;
    return !isAllWhitespace(text.getWholeText());
}

fn isVisibleElement(el: *Element) bool {
    const tag = el.getTag();
    return !tag.isMetadata() and tag != .svg;
}

fn getAnchorLabel(el: *Element) ?[]const u8 {
    return el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title"));
}

fn hasBlockDescendant(root: *Node) bool {
    var tw = TreeWalker.FullExcludeSelf.Elements.init(root, .{});
    while (tw.next()) |el| {
        if (el.getTag().isBlock()) return true;
    }
    return false;
}

fn hasVisibleContent(root: *Node) bool {
    var tw = TreeWalker.FullExcludeSelf.init(root, .{});
    while (tw.next()) |node| {
        if (isSignificantText(node)) return true;
        if (node.is(Element)) |el| {
            if (!isVisibleElement(el)) {
                tw.skipChildren();
            } else if (el.getTag() == .img) {
                return true;
            }
        }
    }
    return false;
}

fn ensureNewline(state: *State, writer: *std.Io.Writer) !void {
    if (!state.last_char_was_newline) {
        try writer.writeByte('\n');
        state.last_char_was_newline = true;
    }
}

pub fn dump(node: *Node, opts: Opts, writer: *std.Io.Writer, page: *Page) !void {
    _ = opts;
    var state = State{};
    try render(node, &state, writer, page);
    if (!state.last_char_was_newline) {
        try writer.writeByte('\n');
    }
}

fn render(node: *Node, state: *State, writer: *std.Io.Writer, page: *Page) error{WriteFailed}!void {
    switch (node._type) {
        .document, .document_fragment => {
            try renderChildren(node, state, writer, page);
        },
        .element => |el| {
            try renderElement(el, state, writer, page);
        },
        .cdata => |cd| {
            if (node.is(Node.CData.Text)) |_| {
                var text = cd.getData().str();
                if (state.pre_node) |pre| {
                    if (node.parentNode() == pre and node.nextSibling() == null) {
                        text = std.mem.trimRight(u8, text, " \t\r\n");
                    }
                }
                try renderText(text, state, writer);
            }
        },
        else => {},
    }
}

fn renderChildren(parent: *Node, state: *State, writer: *std.Io.Writer, page: *Page) !void {
    var it = parent.childrenIterator();
    while (it.next()) |child| {
        try render(child, state, writer, page);
    }
}

fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Page) !void {
    const tag = el.getTag();

    if (!isVisibleElement(el)) return;

    // --- Opening Tag Logic ---

    // Ensure block elements start on a new line (double newline for paragraphs etc)
    if (tag.isBlock() and !state.in_table) {
        try ensureNewline(state, writer);
        if (shouldAddSpacing(tag)) {
            try writer.writeByte('\n');
        }
    } else if (tag == .li or tag == .tr) {
        try ensureNewline(state, writer);
    }

    // Prefixes
    switch (tag) {
        .h1 => try writer.writeAll("# "),
        .h2 => try writer.writeAll("## "),
        .h3 => try writer.writeAll("### "),
        .h4 => try writer.writeAll("#### "),
        .h5 => try writer.writeAll("##### "),
        .h6 => try writer.writeAll("###### "),
        .ul => {
            if (state.list_depth < state.list_stack.len) {
                state.list_stack[state.list_depth] = .{ .type = .unordered, .index = 0 };
                state.list_depth += 1;
            }
        },
        .ol => {
            if (state.list_depth < state.list_stack.len) {
                state.list_stack[state.list_depth] = .{ .type = .ordered, .index = 1 };
                state.list_depth += 1;
            }
        },
        .li => {
            const indent = if (state.list_depth > 0) state.list_depth - 1 else 0;
            for (0..indent) |_| try writer.writeAll("  ");

            if (state.list_depth > 0 and state.list_stack[state.list_depth - 1].type == .ordered) {
                const current_list = &state.list_stack[state.list_depth - 1];
                try writer.print("{d}. ", .{current_list.index});
                current_list.index += 1;
            } else {
                try writer.writeAll("- ");
            }
            state.last_char_was_newline = false;
        },
        .table => {
            state.in_table = true;
            state.table_row_index = 0;
            state.table_col_count = 0;
        },
        .tr => {
            state.table_col_count = 0;
            try writer.writeByte('|');
        },
        .td, .th => {
            // Note: leading pipe handled by previous cell closing or tr opening
            state.last_char_was_newline = false;
            try writer.writeByte(' ');
        },
        .blockquote => {
            try writer.writeAll("> ");
            state.last_char_was_newline = false;
        },
        .pre => {
            try writer.writeAll("```\n");
            state.pre_node = el.asNode();
            state.last_char_was_newline = true;
        },
        .code => {
            if (state.pre_node == null) {
                try writer.writeByte('`');
                state.in_code = true;
                state.last_char_was_newline = false;
            }
        },
        .b, .strong => {
            try writer.writeAll("**");
            state.last_char_was_newline = false;
        },
        .i, .em => {
            try writer.writeAll("*");
            state.last_char_was_newline = false;
        },
        .s, .del => {
            try writer.writeAll("~~");
            state.last_char_was_newline = false;
        },
        .hr => {
            try writer.writeAll("---\n");
            state.last_char_was_newline = true;
            return;
        },
        .br => {
            if (state.in_table) {
                try writer.writeByte(' ');
            } else {
                try writer.writeByte('\n');
                state.last_char_was_newline = true;
            }
            return;
        },
        .img => {
            try writer.writeAll("![");
            if (el.getAttributeSafe(comptime .wrap("alt"))) |alt| {
                try escapeMarkdown(writer, alt);
            }
            try writer.writeAll("](");
            if (el.getAttributeSafe(comptime .wrap("src"))) |src| {
                const absolute_src = URL.resolve(page.call_arena, page.base(), src, .{ .encode = true }) catch src;
                try writer.writeAll(absolute_src);
            }
            try writer.writeAll(")");
            state.last_char_was_newline = false;
            return;
        },
        .anchor => {
            const has_content = hasVisibleContent(el.asNode());
            const label = getAnchorLabel(el);
            const href_raw = el.getAttributeSafe(comptime .wrap("href"));

            if (!has_content and label == null and href_raw == null) return;

            const has_block = hasBlockDescendant(el.asNode());
            const href = if (href_raw) |h| URL.resolve(page.call_arena, page.base(), h, .{ .encode = true }) catch h else null;

            if (has_block) {
                try renderChildren(el.asNode(), state, writer, page);
                if (href) |h| {
                    if (!state.last_char_was_newline) try writer.writeByte('\n');
                    try writer.writeAll("([](");
                    try writer.writeAll(h);
                    try writer.writeAll("))\n");
                    state.last_char_was_newline = true;
                }
                return;
            }

            if (isStandaloneAnchor(el)) {
                if (!state.last_char_was_newline) try writer.writeByte('\n');
                try writer.writeByte('[');
                if (has_content) {
                    try renderChildren(el.asNode(), state, writer, page);
                } else {
                    try writer.writeAll(label orelse "");
                }
                try writer.writeAll("](");
                if (href) |h| {
                    try writer.writeAll(h);
                }
                try writer.writeAll(")\n");
                state.last_char_was_newline = true;
                return;
            }

            try writer.writeByte('[');
            if (has_content) {
                try renderChildren(el.asNode(), state, writer, page);
            } else {
                try writer.writeAll(label orelse "");
            }
            try writer.writeAll("](");
            if (href) |h| {
                try writer.writeAll(h);
            }
            try writer.writeByte(')');
            state.last_char_was_newline = false;
            return;
        },
        .input => {
            const type_attr = el.getAttributeSafe(comptime .wrap("type")) orelse return;
            if (std.ascii.eqlIgnoreCase(type_attr, "checkbox")) {
                const checked = el.getAttributeSafe(comptime .wrap("checked")) != null;
                try writer.writeAll(if (checked) "[x] " else "[ ] ");
                state.last_char_was_newline = false;
            }
            return;
        },
        else => {},
    }

    // --- Render Children ---
    try renderChildren(el.asNode(), state, writer, page);

    // --- Closing Tag Logic ---

    // Suffixes
    switch (tag) {
        .pre => {
            if (!state.last_char_was_newline) {
                try writer.writeByte('\n');
            }
            try writer.writeAll("```\n");
            state.pre_node = null;
            state.last_char_was_newline = true;
        },
        .code => {
            if (state.pre_node == null) {
                try writer.writeByte('`');
                state.in_code = false;
                state.last_char_was_newline = false;
            }
        },
        .b, .strong => {
            try writer.writeAll("**");
            state.last_char_was_newline = false;
        },
        .i, .em => {
            try writer.writeAll("*");
            state.last_char_was_newline = false;
        },
        .s, .del => {
            try writer.writeAll("~~");
            state.last_char_was_newline = false;
        },
        .blockquote => {},
        .ul, .ol => {
            if (state.list_depth > 0) state.list_depth -= 1;
        },
        .table => {
            state.in_table = false;
        },
        .tr => {
            try writer.writeByte('\n');
            if (state.table_row_index == 0) {
                try writer.writeByte('|');
                for (0..state.table_col_count) |_| {
                    try writer.writeAll("---|");
                }
                try writer.writeByte('\n');
            }
            state.table_row_index += 1;
            state.last_char_was_newline = true;
        },
        .td, .th => {
            try writer.writeAll(" |");
            state.table_col_count += 1;
            state.last_char_was_newline = false;
        },
        else => {},
    }

    // Post-block newlines
    if (tag.isBlock() and !state.in_table) {
        try ensureNewline(state, writer);
    }
}

fn renderText(text: []const u8, state: *State, writer: *std.Io.Writer) !void {
    if (text.len == 0) return;

    if (state.pre_node) |_| {
        try writer.writeAll(text);
        state.last_char_was_newline = text[text.len - 1] == '\n';
        return;
    }

    // Check for pure whitespace
    if (isAllWhitespace(text)) {
        if (!state.last_char_was_newline) {
            try writer.writeByte(' ');
        }
        return;
    }

    // Collapse whitespace
    var it = std.mem.tokenizeAny(u8, text, " \t\n\r");
    var first = true;
    while (it.next()) |word| {
        if (!first or (!state.last_char_was_newline and std.ascii.isWhitespace(text[0]))) {
            try writer.writeByte(' ');
        }

        try escapeMarkdown(writer, word);
        state.last_char_was_newline = false;
        first = false;
    }

    // Handle trailing whitespace from the original text
    if (!first and !state.last_char_was_newline and std.ascii.isWhitespace(text[text.len - 1])) {
        try writer.writeByte(' ');
    }
}

fn escapeMarkdown(writer: *std.Io.Writer, text: []const u8) !void {
    for (text) |c| {
        switch (c) {
            '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '!', '|' => {
                try writer.writeByte('\\');
                try writer.writeByte(c);
            },
            else => try writer.writeByte(c),
        }
    }
}

fn testMarkdownHTML(html: []const u8, expected: []const u8) !void {
    const testing = @import("../testing.zig");
    const page = try testing.test_session.createPage();
    defer testing.test_session.removePage();
    page.url = "http://localhost/";

    const doc = page.window._document;

    const div = try doc.createElement("div", null, page);
    try page.parseHtmlAsChildren(div.asNode(), html);

    var aw: std.Io.Writer.Allocating = .init(testing.allocator);
    defer aw.deinit();
    try dump(div.asNode(), .{}, &aw.writer, page);

    try testing.expectString(expected, aw.written());
}

test "browser.markdown: basic" {
    try testMarkdownHTML("Hello world", "Hello world\n");
}

test "browser.markdown: whitespace" {
    try testMarkdownHTML("<span>A</span> <span>B</span>", "A B\n");
}

test "browser.markdown: escaping" {
    try testMarkdownHTML("<p># Not a header</p>", "\n\\# Not a header\n");
}

test "browser.markdown: strikethrough" {
    try testMarkdownHTML("<s>deleted</s>", "~~deleted~~\n");
}

test "browser.markdown: task list" {
    try testMarkdownHTML(
        \\<input type="checkbox" checked><input type="checkbox">
    , "[x] [ ] \n");
}

test "browser.markdown: ordered list" {
    try testMarkdownHTML(
        \\<ol><li>First</li><li>Second</li></ol>
    , "1. First\n2. Second\n");
}

test "browser.markdown: table" {
    try testMarkdownHTML(
        \\<table><thead><tr><th>Head 1</th><th>Head 2</th></tr></thead>
        \\<tbody><tr><td>Cell 1</td><td>Cell 2</td></tr></tbody></table>
    ,
        \\
        \\| Head 1 | Head 2 |
        \\|---|---|
        \\| Cell 1 | Cell 2 |
        \\
    );
}

test "browser.markdown: nested lists" {
    try testMarkdownHTML(
        \\<ul><li>Parent<ul><li>Child</li></ul></li></ul>
    ,
        \\- Parent
        \\  - Child
        \\
    );
}

test "browser.markdown: blockquote" {
    try testMarkdownHTML("<blockquote>Hello world</blockquote>", "\n> Hello world\n");
}

test "browser.markdown: links" {
    try testMarkdownHTML("<a href=\"/relative\">Link</a>", "[Link](http://localhost/relative)\n");
}

test "browser.markdown: images" {
    try testMarkdownHTML("<img src=\"logo.png\" alt=\"Logo\">", "![Logo](http://localhost/logo.png)\n");
}

test "browser.markdown: headings" {
    try testMarkdownHTML("<h1>Title</h1><h2>Subtitle</h2>",
        \\
        \\# Title
        \\
        \\## Subtitle
        \\
    );
}

test "browser.markdown: code" {
    try testMarkdownHTML(
        \\<p>Use git push</p>
        \\<pre><code>line 1
        \\line 2</code></pre>
    ,
        \\
        \\Use git push
        \\
        \\```
        \\line 1
        \\line 2
        \\```
        \\
    );
}

test "browser.markdown: block link" {
    try testMarkdownHTML(
        \\<a href="https://example.com">
        \\  <h3>Title</h3>
        \\  <p>Description</p>
        \\</a>
    ,
        \\
        \\### Title
        \\
        \\Description
        \\([](https://example.com))
        \\
    );
}

test "browser.markdown: inline link" {
    try testMarkdownHTML(
        \\<p>Visit <a href="https://example.com">Example</a>.</p>
    ,
        \\
        \\Visit [Example](https://example.com).
        \\
    );
}

test "browser.markdown: standalone anchors" {
    // Inside main, with whitespace between anchors -> treated as blocks
    try testMarkdownHTML(
        \\<main>
        \\  <a href="1">Link 1</a>
        \\  <a href="2">Link 2</a>
        \\</main>
    ,
        \\[Link 1](http://localhost/1)
        \\[Link 2](http://localhost/2)
        \\
    );
}

test "browser.markdown: mixed anchors in main" {
    // Anchors surrounded by text should remain inline
    try testMarkdownHTML(
        \\<main>
        \\  Welcome <a href="1">Link 1</a>.
        \\</main>
    ,
        \\Welcome [Link 1](http://localhost/1). 
        \\
    );
}

test "browser.markdown: skip empty links" {
    try testMarkdownHTML(
        \\<a href="/"></a>
        \\<a href="/"><svg></svg></a>
    ,
        \\[](http://localhost/)
        \\[](http://localhost/)
        \\
    );
}

test "browser.markdown: resolve links" {
    const testing = @import("../testing.zig");
    const page = try testing.test_session.createPage();
    defer testing.test_session.removePage();
    page.url = "https://example.com/a/index.html";

    const doc = page.window._document;
    const div = try doc.createElement("div", null, page);
    try page.parseHtmlAsChildren(div.asNode(),
        \\<a href="b">Link</a>
        \\<img src="../c.png" alt="Img">
        \\<a href="/my page">Space</a>
    );

    var aw: std.Io.Writer.Allocating = .init(testing.allocator);
    defer aw.deinit();
    try dump(div.asNode(), .{}, &aw.writer, page);

    try testing.expectString(
        \\[Link](https://example.com/a/b)
        \\![Img](https://example.com/c.png) 
        \\[Space](https://example.com/my%20page)
        \\
    , aw.written());
}

test "browser.markdown: anchor fallback label" {
    try testMarkdownHTML(
        \\<a href="/discord" aria-label="Discord Server"><svg></svg></a>
    , "[Discord Server](http://localhost/discord)\n");

    try testMarkdownHTML(
        \\<a href="/search" title="Search Site"><svg></svg></a>
    , "[Search Site](http://localhost/search)\n");

    try testMarkdownHTML(
        \\<a href="/no-label"><svg></svg></a>
    , "[](http://localhost/no-label)\n");
}
