"""Tests for block_parser module."""

from notion_bulk_export.block_parser import blocks_to_text, render_block


class TestRenderBlock:
    """Test rendering individual Notion API block objects."""

    def test_paragraph(self):
        block = {
            "type": "paragraph",
            "paragraph": {
                "rich_text": [{"plain_text": "Hello world"}],
            },
        }
        assert render_block(block) == "Hello world"

    def test_heading_1(self):
        block = {
            "type": "heading_1",
            "heading_1": {
                "rich_text": [{"plain_text": "Title"}],
            },
        }
        assert render_block(block) == "# Title"

    def test_heading_2(self):
        block = {
            "type": "heading_2",
            "heading_2": {
                "rich_text": [{"plain_text": "Subtitle"}],
            },
        }
        assert render_block(block) == "## Subtitle"

    def test_heading_3(self):
        block = {
            "type": "heading_3",
            "heading_3": {
                "rich_text": [{"plain_text": "Section"}],
            },
        }
        assert render_block(block) == "### Section"

    def test_bulleted_list_item(self):
        block = {
            "type": "bulleted_list_item",
            "bulleted_list_item": {
                "rich_text": [{"plain_text": "Item"}],
            },
        }
        assert render_block(block) == "- Item"

    def test_numbered_list_item(self):
        block = {
            "type": "numbered_list_item",
            "numbered_list_item": {
                "rich_text": [{"plain_text": "First"}],
            },
        }
        assert render_block(block) == "1. First"

    def test_to_do_unchecked(self):
        block = {
            "type": "to_do",
            "to_do": {
                "rich_text": [{"plain_text": "Task"}],
                "checked": False,
            },
        }
        assert render_block(block) == "[ ] Task"

    def test_to_do_checked(self):
        block = {
            "type": "to_do",
            "to_do": {
                "rich_text": [{"plain_text": "Done"}],
                "checked": True,
            },
        }
        assert render_block(block) == "[x] Done"

    def test_quote(self):
        block = {
            "type": "quote",
            "quote": {
                "rich_text": [{"plain_text": "Important"}],
            },
        }
        assert render_block(block) == "> Important"

    def test_code(self):
        block = {
            "type": "code",
            "code": {
                "rich_text": [{"plain_text": "print('hi')"}],
                "language": "python",
            },
        }
        assert render_block(block) == "```python\nprint('hi')\n```"

    def test_divider(self):
        block = {"type": "divider", "divider": {}}
        assert render_block(block) == "---"

    def test_callout_with_emoji(self):
        block = {
            "type": "callout",
            "callout": {
                "rich_text": [{"plain_text": "Note"}],
                "icon": {"type": "emoji", "emoji": "💡"},
            },
        }
        result = render_block(block)
        assert "Note" in result

    def test_bookmark(self):
        block = {
            "type": "bookmark",
            "bookmark": {
                "url": "https://example.com",
                "caption": [],
            },
        }
        assert render_block(block) == "[https://example.com](https://example.com)"

    def test_image_external(self):
        block = {
            "type": "image",
            "image": {
                "type": "external",
                "external": {"url": "https://example.com/img.png"},
                "caption": [{"plain_text": "photo"}],
            },
        }
        assert render_block(block) == "![photo](https://example.com/img.png)"

    def test_table_row(self):
        block = {
            "type": "table_row",
            "table_row": {
                "cells": [
                    [{"plain_text": "A"}],
                    [{"plain_text": "B"}],
                ],
            },
        }
        assert render_block(block) == "| A | B |"

    def test_empty_paragraph(self):
        block = {
            "type": "paragraph",
            "paragraph": {"rich_text": []},
        }
        assert render_block(block) == ""

    def test_multiple_rich_text_segments(self):
        block = {
            "type": "paragraph",
            "paragraph": {
                "rich_text": [
                    {"plain_text": "Hello "},
                    {"plain_text": "world"},
                ],
            },
        }
        assert render_block(block) == "Hello world"

    def test_unknown_type(self):
        block = {
            "type": "unknown_future_type",
            "unknown_future_type": {
                "rich_text": [{"plain_text": "some text"}],
            },
        }
        assert render_block(block) == "some text"


class TestBlocksToText:
    """Test converting lists of blocks to combined text."""

    def test_multiple_blocks(self):
        blocks = [
            {
                "type": "heading_1",
                "heading_1": {"rich_text": [{"plain_text": "Title"}]},
            },
            {
                "type": "paragraph",
                "paragraph": {"rich_text": [{"plain_text": "Body text"}]},
            },
            {
                "type": "bulleted_list_item",
                "bulleted_list_item": {"rich_text": [{"plain_text": "Item"}]},
            },
        ]
        result = blocks_to_text(blocks)
        assert result == "# Title\nBody text\n- Item"

    def test_empty_blocks_filtered(self):
        blocks = [
            {
                "type": "paragraph",
                "paragraph": {"rich_text": [{"plain_text": "Keep"}]},
            },
            {
                "type": "paragraph",
                "paragraph": {"rich_text": []},
            },
            {
                "type": "paragraph",
                "paragraph": {"rich_text": [{"plain_text": "Also keep"}]},
            },
        ]
        result = blocks_to_text(blocks)
        assert result == "Keep\nAlso keep"

    def test_empty_list(self):
        assert blocks_to_text([]) == ""
