"""Tests for deploy.frontmatter module."""

import pytest

from ccfm_convert.deploy.frontmatter import parse_frontmatter


class TestFrontmatterParsing:
    """Test frontmatter parsing."""

    def test_no_frontmatter(self):
        """Test content without frontmatter."""
        content = "# Hello\n\nThis is content."
        metadata, markdown = parse_frontmatter(content)

        assert metadata == {}
        assert markdown == content

    def test_empty_frontmatter(self):
        """Test empty frontmatter."""
        content = "---\n---\n# Content"
        metadata, markdown = parse_frontmatter(content)

        # Empty frontmatter returns defaults
        assert metadata["title"] is None
        assert metadata["ci_banner"] is True  # Default
        assert metadata["deploy_page"] is True  # Default
        assert markdown == "# Content"

    def test_simple_frontmatter(self):
        """Test simple key-value frontmatter."""
        content = """---
page_meta:
  title: My Page
  author: John Doe
---
# Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["title"] == "My Page"
        assert metadata["author"] == "John Doe"
        assert markdown == "# Content"

    def test_frontmatter_with_boolean(self):
        """Test frontmatter with boolean values."""
        content = """---
deploy_config:
  deploy_page: false
  ci_banner: true
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["deploy_page"] is False
        assert metadata["ci_banner"] is True

    def test_frontmatter_with_list(self):
        """Test frontmatter with list values."""
        content = """---
page_meta:
  labels:
    - python
    - documentation
    - api
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["labels"] == ["python", "documentation", "api"]

    def test_frontmatter_with_nested_dict(self):
        """Test frontmatter with nested dictionary."""
        content = """---
page_meta:
  attachments:
    - path: diagram.png
      alt: Architecture diagram
    - path: screenshot.jpg
      alt: UI screenshot
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert len(metadata["attachments"]) == 2
        assert metadata["attachments"][0]["path"] == "diagram.png"
        assert metadata["attachments"][0]["alt"] == "Architecture diagram"

    def test_attachment_with_width(self):
        """Test attachment with width field is preserved."""
        content = """---
page_meta:
  attachments:
    - path: diagram.png
      alt: Architecture diagram
      width: narrow
    - path: big.png
      alt: Full width image
      width: max
    - path: custom.png
      alt: Custom size
      width: 500
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["attachments"][0]["width"] == "narrow"
        assert metadata["attachments"][1]["width"] == "max"
        assert metadata["attachments"][2]["width"] == 500

    def test_frontmatter_with_multiline_string(self):
        """Test frontmatter with multiline string."""
        content = """---
deploy_config:
  ci_banner_text: |
    This page is auto-generated.
    Do not edit manually.
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert "This page is auto-generated" in metadata["ci_banner_text"]
        assert "Do not edit manually" in metadata["ci_banner_text"]

    @pytest.mark.skip(reason="Implementation only returns predefined fields, not arbitrary keys")
    def test_frontmatter_with_numbers(self):
        """Test frontmatter with numeric values."""
        content = """---
version: 2
page_id: 12345
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["version"] == 2
        assert metadata["page_id"] == 12345

    def test_frontmatter_with_null(self):
        """Test frontmatter with null values."""
        content = """---
page_meta:
  title: null
  author: ~
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["title"] is None
        assert metadata["author"] is None

    def test_incomplete_frontmatter_opening(self):
        """Test content with incomplete frontmatter opening."""
        content = "--\ntitle: Test\n---\nContent"
        metadata, markdown = parse_frontmatter(content)

        # Should treat as no frontmatter
        assert metadata == {}
        assert markdown == content

    def test_incomplete_frontmatter_closing(self):
        """Test content with incomplete frontmatter closing."""
        content = "---\ntitle: Test\n--\nContent"
        metadata, markdown = parse_frontmatter(content)

        # Should treat as no frontmatter
        assert metadata == {}
        assert markdown == content

    def test_frontmatter_not_at_start(self):
        """Test frontmatter not at document start."""
        content = "# Title\n---\nkey: value\n---\nContent"
        metadata, markdown = parse_frontmatter(content)

        # Frontmatter must be at document start
        assert metadata == {}
        assert markdown == content

    def test_frontmatter_with_spaces(self):
        """Test frontmatter with spacing variations."""
        content = """---
page_meta:
  title: My Page
  author: John Doe
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        # YAML parser handles spacing, so values come through clean
        assert metadata["title"] == "My Page"
        assert metadata["author"] == "John Doe"

    @pytest.mark.skip(reason="Implementation only returns predefined fields")
    def test_frontmatter_with_special_chars(self):
        """Test frontmatter with special characters in values."""
        content = """---
page_meta:
  title: "Page: Advanced Topics"
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert "Advanced Topics" in metadata["title"]

    def test_markdown_content_preserved(self):
        """Test that markdown content is fully preserved."""
        markdown_text = """# Heading

Paragraph with **bold** and *italic*.

```python
code()
```

- List item 1
- List item 2

---

Final paragraph."""

        content = f"---\ntitle: Test\n---\n{markdown_text}"
        metadata, markdown = parse_frontmatter(content)

        assert markdown == markdown_text

    def test_empty_content(self):
        """Test empty content."""
        content = ""
        metadata, markdown = parse_frontmatter(content)

        assert metadata == {}
        assert markdown == ""

    def test_only_frontmatter(self):
        """Test content with only frontmatter, no markdown."""
        content = """---
page_meta:
  title: Test
---"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["title"] == "Test"
        assert markdown == ""

    @pytest.mark.skip(reason="Implementation only returns predefined fields")
    def test_frontmatter_with_colon_in_value(self):
        """Test frontmatter with colon in value."""
        content = """---
url: "https://example.com:8080/path"
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["url"] == "https://example.com:8080/path"

    @pytest.mark.skip(reason="Implementation only returns predefined fields")
    def test_frontmatter_with_complex_nested_structure(self):
        """Test frontmatter with complex nested structure."""
        content = """---
metadata:
  section:
    items:
      - name: item1
        value: 100
      - name: item2
        value: 200
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        assert metadata["metadata"]["section"]["items"][0]["name"] == "item1"
        assert metadata["metadata"]["section"]["items"][1]["value"] == 200

    def test_frontmatter_with_parent(self):
        """Test frontmatter with parent field."""
        content = """---
page_meta:
  title: My Page
  parent: Parent Page Title
---
# Content"""
        metadata, markdown = parse_frontmatter(content)
        assert metadata["parent"] == "Parent Page Title"

    def test_frontmatter_parent_defaults_none(self):
        """Test that parent defaults to None when not specified."""
        content = """---
page_meta:
  title: My Page
---
# Content"""
        metadata, _ = parse_frontmatter(content)
        assert metadata["parent"] is None

    def test_invalid_yaml(self):
        """Test handling of invalid YAML in frontmatter."""
        content = """---
key: value
  invalid indentation
---
Content"""
        metadata, markdown = parse_frontmatter(content)

        # Should gracefully handle invalid YAML
        # Either return empty metadata or partial metadata
        assert isinstance(metadata, dict)
        assert isinstance(markdown, str)

    def test_yaml_error_returns_empty_metadata_and_original_content(self):
        """Lines 35-37: YAMLError prints error and returns ({}, original content)."""
        # Use a tab character inside a mapping value — yaml.safe_load will raise YAMLError
        bad_yaml = "---\nkey: [\n  - bad\n    indent: oops\n---\nBody"
        metadata, markdown = parse_frontmatter(bad_yaml)

        assert metadata == {}
        assert markdown == bad_yaml

    def test_invalid_page_status_resets_to_current(self):
        """Lines 61-62: an unrecognised page_status is reset to 'current'."""
        content = """---
deploy_config:
  page_status: published
---
# Content"""
        metadata, _ = parse_frontmatter(content)

        assert metadata["page_status"] == "current"
