from __future__ import annotations

from pydantic import BaseModel, Field, field_validator

from app.models.enums import LucosSourceType


class ChunkPayload(BaseModel):
    """Single normalized chunk uploaded by the Lucos daemon."""

    chunk_id: str = Field(min_length=1, max_length=128)
    chunk_hash: str = Field(
        min_length=8,
        max_length=128,
        description="Content hash for dedup, e.g. sha256:...",
    )
    file_path: str = Field(min_length=1, max_length=2048)
    language: str | None = Field(default=None, max_length=64)
    start_line: int = Field(ge=1)
    end_line: int = Field(ge=1)
    content: str = Field(min_length=1)
    source_type: LucosSourceType = LucosSourceType.REPO_CODE
    symbol_name: str | None = Field(default=None, max_length=256)

    @field_validator("end_line")
    @classmethod
    def end_line_gte_start(cls, value: int, info) -> int:
        start = info.data.get("start_line")
        if start is not None and value < start:
            raise ValueError("end_line must be >= start_line")
        return value
