"""Document DTOs for parsing and chunking."""

from typing import Any, Optional
from uuid import UUID

from pydantic import BaseModel, Field


class ChunkedElementDTO(BaseModel):
    """DTO for a chunked element from document parsing."""

    file_id: UUID = Field(..., description="PostgreSQL UUID of the source file")
    element_type: str = Field(..., description="Type of element (e.g., 'Table', 'NarrativeText', 'Title')")
    file_type: str = Field(..., description="Type of source file (e.g., 'xlsx', 'xls', 'xlsm')")
    text: str = Field(..., description="Text content of the element")
    metadata: dict[str, Any] = Field(
        default_factory=dict, description="Metadata associated with the element"
    )

    class Config:
        """Pydantic config."""
        json_schema_extra = {
            "example": {
                "file_id": "550e8400-e29b-41d4-a716-446655440000",
                "element_type": "Table",
                "file_type": "xlsx",
                "text": "Column1 | Column2\n--- | ---\nValue1 | Value2",
                "metadata": {
                    "page_name": "Sheet1",
                    "page_number": 1,
                    "filename": "data.xlsx",
                    "text_as_html": "<table>...</table>"
                }
            }
        }


class ElementMetadataDTO(BaseModel):
    """Metadata for document elements (replaces unstructured.ElementMetadata)."""

    page_name: Optional[str] = Field(None, description="Name of the page/sheet")
    page_number: Optional[int] = Field(None, description="Page/sheet number")
    sheet_name: Optional[str] = Field(None, description="Excel sheet name (alias for page_name)")
    sheet_number: Optional[int] = Field(None, description="Excel sheet number (alias for page_number)")

    class Config:
        """Pydantic config."""
        extra = "allow"  # Allow additional metadata fields


class TextElementDTO(BaseModel):
    """Text element with metadata (replaces unstructured.Text)."""

    text: str = Field(..., description="Text content of the element")
    metadata: ElementMetadataDTO = Field(
        default_factory=lambda: ElementMetadataDTO(),
        description="Metadata associated with the text element"
    )

    def __str__(self) -> str:
        """Return the text content when converted to string."""
        return self.text

    class Config:
        """Pydantic config."""
        json_schema_extra = {
            "example": {
                "text": "Sample text content",
                "metadata": {
                    "page_name": "Sheet1",
                    "page_number": 1
                }
            }
        }
