from pydantic import BaseModel, Field
from typing import List, Optional

class ChapterMetadata(BaseModel):
    """Metadata extracted from a document section/chapter."""
    title: str = Field(
        ..., description="Title of the text section or chapter."
    )
    summary: str = Field(
        ..., description="A concise summary of this text chunk covering the main concepts and key points."
    )
    questions: List[str] = Field(
        ..., description="Questions that a reader might ask that this section answers. Keep them short, specific, and focused on the content."
    )
    key_concepts: List[str] = Field(
        ..., description="Key concepts, terms, or topics covered in this section."
    )
    cross_references: Optional[List[str]] = Field(
        default=None, description="References to other sources, documents, or related materials mentioned in this section."
    )

    # -------------------------------------------------
    # Retrieval representation (embedding text)
    # -------------------------------------------------
    def __str__(self) -> str:
        lines = [
            f"Title: {self.title}",
            "",
            "Summary:",
            self.summary,
        ]

        if self.key_concepts:
            lines.extend(["", "Key Concepts:"])
            for concept in self.key_concepts:
                lines.append(f"- {concept}")

        if self.questions:
            lines.extend(["", "Questions this section answers:"])
            for q in self.questions:
                lines.append(f"- {q}")

        if self.cross_references:
            lines.extend(["", "Cross-references:"])
            for ref in self.cross_references:
                lines.append(f"- {ref}")

        return "\n".join(lines)
    
    # -------------------------------------------------
    # Debug / developer representation
    # -------------------------------------------------
    def __repr__(self) -> str:
        return (
            f"ChapterMetadata("
            f"title={self.title!r}, "
            f"questions={len(self.questions)}, "
            f"key_concepts={len(self.key_concepts)}"
            f")"
        )

