"""Tests for property_parser module."""

from notion_bulk_export.property_parser import (
    get_page_title,
    parse_all_properties,
    parse_property,
)


class TestParseProperty:
    """Test parsing individual Notion property types."""

    def test_title(self):
        prop = {
            "type": "title",
            "title": [{"plain_text": "My Page"}],
        }
        assert parse_property(prop) == "My Page"

    def test_title_empty(self):
        prop = {"type": "title", "title": []}
        assert parse_property(prop) == ""

    def test_rich_text(self):
        prop = {
            "type": "rich_text",
            "rich_text": [
                {"plain_text": "Hello "},
                {"plain_text": "world"},
            ],
        }
        assert parse_property(prop) == "Hello world"

    def test_number(self):
        prop = {"type": "number", "number": 42}
        assert parse_property(prop) == 42

    def test_number_none(self):
        prop = {"type": "number", "number": None}
        assert parse_property(prop) is None

    def test_checkbox_true(self):
        prop = {"type": "checkbox", "checkbox": True}
        assert parse_property(prop) is True

    def test_checkbox_false(self):
        prop = {"type": "checkbox", "checkbox": False}
        assert parse_property(prop) is False

    def test_select(self):
        prop = {
            "type": "select",
            "select": {"name": "Option A", "color": "blue"},
        }
        assert parse_property(prop) == "Option A"

    def test_select_none(self):
        prop = {"type": "select", "select": None}
        assert parse_property(prop) is None

    def test_multi_select(self):
        prop = {
            "type": "multi_select",
            "multi_select": [
                {"name": "Tag1"},
                {"name": "Tag2"},
            ],
        }
        assert parse_property(prop) == ["Tag1", "Tag2"]

    def test_multi_select_empty(self):
        prop = {"type": "multi_select", "multi_select": []}
        assert parse_property(prop) == []

    def test_status(self):
        prop = {
            "type": "status",
            "status": {"name": "In Progress", "color": "yellow"},
        }
        assert parse_property(prop) == "In Progress"

    def test_date_single(self):
        prop = {
            "type": "date",
            "date": {"start": "2025-01-15", "end": None},
        }
        assert parse_property(prop) == "2025-01-15"

    def test_date_range(self):
        prop = {
            "type": "date",
            "date": {"start": "2025-01-15", "end": "2025-01-20"},
        }
        assert parse_property(prop) == "2025-01-15 ~ 2025-01-20"

    def test_date_none(self):
        prop = {"type": "date", "date": None}
        assert parse_property(prop) is None

    def test_people(self):
        prop = {
            "type": "people",
            "people": [
                {"name": "Alice", "id": "user-1"},
                {"name": "Bob", "id": "user-2"},
            ],
        }
        assert parse_property(prop) == ["Alice", "Bob"]

    def test_url(self):
        prop = {"type": "url", "url": "https://example.com"}
        assert parse_property(prop) == "https://example.com"

    def test_email(self):
        prop = {"type": "email", "email": "test@example.com"}
        assert parse_property(prop) == "test@example.com"

    def test_phone_number(self):
        prop = {"type": "phone_number", "phone_number": "010-1234-5678"}
        assert parse_property(prop) == "010-1234-5678"

    def test_relation(self):
        prop = {
            "type": "relation",
            "relation": [{"id": "page-1"}, {"id": "page-2"}],
        }
        assert parse_property(prop) == ["page-1", "page-2"]

    def test_created_time(self):
        prop = {
            "type": "created_time",
            "created_time": "2025-01-15T10:00:00.000Z",
        }
        assert parse_property(prop) == "2025-01-15T10:00:00.000Z"

    def test_last_edited_time(self):
        prop = {
            "type": "last_edited_time",
            "last_edited_time": "2025-02-01T15:30:00.000Z",
        }
        assert parse_property(prop) == "2025-02-01T15:30:00.000Z"

    def test_created_by(self):
        prop = {
            "type": "created_by",
            "created_by": {"name": "Alice", "id": "user-1"},
        }
        assert parse_property(prop) == "Alice"

    def test_formula_string(self):
        prop = {
            "type": "formula",
            "formula": {"type": "string", "string": "computed"},
        }
        assert parse_property(prop) == "computed"

    def test_formula_number(self):
        prop = {
            "type": "formula",
            "formula": {"type": "number", "number": 99},
        }
        assert parse_property(prop) == 99

    def test_files(self):
        prop = {
            "type": "files",
            "files": [
                {
                    "name": "doc.pdf",
                    "file": {"url": "https://s3.example.com/doc.pdf"},
                }
            ],
        }
        result = parse_property(prop)
        assert result == ["https://s3.example.com/doc.pdf"]

    def test_unique_id_with_prefix(self):
        prop = {
            "type": "unique_id",
            "unique_id": {"prefix": "TASK", "number": 42},
        }
        assert parse_property(prop) == "TASK-42"

    def test_unique_id_without_prefix(self):
        prop = {
            "type": "unique_id",
            "unique_id": {"prefix": None, "number": 7},
        }
        assert parse_property(prop) == "7"

    def test_unknown_type(self):
        prop = {"type": "future_type", "future_type": "some_value"}
        assert parse_property(prop) == "some_value"

    def test_no_type(self):
        prop = {"id": "something"}
        assert parse_property(prop) is None


class TestGetPageTitle:
    """Test extracting page title from properties."""

    def test_standard_title(self):
        properties = {
            "Name": {
                "type": "title",
                "title": [{"plain_text": "My Page"}],
            },
            "Status": {
                "type": "select",
                "select": {"name": "Done"},
            },
        }
        assert get_page_title(properties) == "My Page"

    def test_korean_title_field(self):
        properties = {
            "이름": {
                "type": "title",
                "title": [{"plain_text": "테스트 페이지"}],
            },
        }
        assert get_page_title(properties) == "테스트 페이지"

    def test_no_title_property(self):
        properties = {
            "Status": {
                "type": "select",
                "select": {"name": "Done"},
            },
        }
        assert get_page_title(properties) == ""


class TestParseAllProperties:
    """Test parsing all properties of a page."""

    def test_mixed_properties(self):
        properties = {
            "Name": {
                "type": "title",
                "title": [{"plain_text": "Test"}],
            },
            "Score": {
                "type": "number",
                "number": 95,
            },
            "Tags": {
                "type": "multi_select",
                "multi_select": [{"name": "A"}, {"name": "B"}],
            },
        }
        result = parse_all_properties(properties)
        assert result == {
            "Name": "Test",
            "Score": 95,
            "Tags": ["A", "B"],
        }

    def test_empty_properties(self):
        assert parse_all_properties({}) == {}
