"""DOCX 生成器测试."""

import os
import tempfile
import pytest
from docx import Document

from mcp_docx_thesis_server.models import ThesisContent, ThesisMetadata, Section, Reference
from mcp_docx_thesis_server.formats import GENERIC_FORMAT
from mcp_docx_thesis_server.docx_generator import DocxGenerator, generate_thesis_docx


class TestDocxGenerator:
    """测试 DOCX 生成器."""

    def test_generator_initialization(self):
        """测试生成器初始化."""
        generator = DocxGenerator(GENERIC_FORMAT)

        assert generator.document is not None
        assert generator.format == GENERIC_FORMAT

    def test_generate_simple_thesis(self):
        """测试生成简单论文."""
        content = ThesisContent(
            metadata=ThesisMetadata(
                title="测试论文",
                author="张三",
                abstract_cn="这是中文摘要。",
                abstract_en="This is English abstract.",
                keywords_cn=["关键词1", "关键词2"],
                keywords_en=["keyword1", "keyword2"],
            ),
            sections=[
                Section(title="第一章 绪论", level=1, content="这是第一章内容。"),
                Section(title="第二章 正文", level=1, content="这是第二章内容。"),
            ],
            references=[
                Reference(index=1, content="参考文献1"),
                Reference(index=2, content="参考文献2"),
            ],
        )

        generator = DocxGenerator(GENERIC_FORMAT)
        doc = generator.generate(content)

        assert doc is not None

    def test_save_document(self):
        """测试保存文档."""
        with tempfile.TemporaryDirectory() as tmpdir:
            output_path = os.path.join(tmpdir, "test_thesis.docx")

            content = ThesisContent(
                metadata=ThesisMetadata(title="测试论文"),
                sections=[Section(title="第一章", level=1, content="内容")],
            )

            generate_thesis_docx(content, GENERIC_FORMAT, output_path)

            assert os.path.exists(output_path)

            # 验证可以打开
            doc = Document(output_path)
            assert doc is not None

    def test_page_setup(self):
        """测试页面设置."""
        generator = DocxGenerator(GENERIC_FORMAT)

        section = generator.document.sections[0]

        # 验证页面大小 (A4)
        assert section.page_width.cm == pytest.approx(21.0, abs=0.1)
        assert section.page_height.cm == pytest.approx(29.7, abs=0.1)

    def test_cover_page_generation(self):
        """测试封面生成."""
        content = ThesisContent(
            metadata=ThesisMetadata(
                title="测试论文标题",
                author="张三",
                student_id="2024001",
                major="计算机科学",
                college="信息学院",
                advisor="李老师",
                date="2025年3月",
            ),
            sections=[],
        )

        generator = DocxGenerator(GENERIC_FORMAT)
        generator._add_cover_page(content)

        # 验证文档中有内容
        assert len(generator.document.paragraphs) > 0

    def test_abstract_generation(self):
        """测试摘要生成."""
        content = ThesisContent(
            metadata=ThesisMetadata(
                abstract_cn="中文摘要内容。",
                abstract_en="English abstract content.",
                keywords_cn=["关键词1", "关键词2"],
                keywords_en=["keyword1", "keyword2"],
            ),
            sections=[],
        )

        generator = DocxGenerator(GENERIC_FORMAT)
        generator._add_abstract(content)

        # 验证文档中有内容
        assert len(generator.document.paragraphs) > 0

    def test_references_generation(self):
        """测试参考文献生成."""
        references = [
            Reference(index=1, content="参考文献1的内容"),
            Reference(index=2, content="参考文献2的内容"),
        ]

        generator = DocxGenerator(GENERIC_FORMAT)
        generator._add_references(references)

        # 验证文档中有内容
        assert len(generator.document.paragraphs) > 0


class TestDocxGeneratorEdgeCases:
    """测试 DOCX 生成器边界情况."""

    def test_empty_content(self):
        """测试空内容."""
        content = ThesisContent(metadata=ThesisMetadata())

        generator = DocxGenerator(GENERIC_FORMAT)
        doc = generator.generate(content)

        assert doc is not None

    def test_no_abstract(self):
        """测试无摘要."""
        content = ThesisContent(
            metadata=ThesisMetadata(title="测试论文"),
            sections=[Section(title="第一章", level=1, content="内容")],
        )

        generator = DocxGenerator(GENERIC_FORMAT)
        doc = generator.generate(content)

        assert doc is not None

    def test_no_references(self):
        """测试无参考文献."""
        content = ThesisContent(
            metadata=ThesisMetadata(title="测试论文"),
            sections=[Section(title="第一章", level=1, content="内容")],
            references=[],
        )

        generator = DocxGenerator(GENERIC_FORMAT)
        doc = generator.generate(content)

        assert doc is not None

    def test_nested_sections(self):
        """测试嵌套章节."""
        section = Section(
            title="第一章",
            level=1,
            content="第一章内容",
            subsections=[
                Section(title="1.1 节", level=2, content="1.1节内容"),
                Section(title="1.2 节", level=2, content="1.2节内容"),
            ],
        )

        content = ThesisContent(
            metadata=ThesisMetadata(title="测试论文"),
            sections=[section],
        )

        generator = DocxGenerator(GENERIC_FORMAT)
        doc = generator.generate(content)

        assert doc is not None
