from datetime import datetime

from pydantic import BaseModel, field_validator


class SectionCreate(BaseModel):
    name: str
    start_page: int
    end_page: int

    @field_validator("end_page")
    @classmethod
    def end_after_start(cls, v, info):
        if "start_page" in info.data and v <= info.data["start_page"]:
            raise ValueError("end_page must be greater than start_page")
        return v

    @field_validator("start_page")
    @classmethod
    def start_positive(cls, v):
        if v < 1:
            raise ValueError("start_page must be at least 1")
        return v


class SectionResponse(BaseModel):
    id: int
    document_id: int
    name: str
    start_page: int
    end_page: int

    class Config:
        from_attributes = True


class DocumentResponse(BaseModel):
    id: int
    user_id: int
    original_filename: str
    total_pages: int | None
    status: str
    error_message: str | None
    uploaded_at: datetime
    sections: list[SectionResponse] = []

    class Config:
        from_attributes = True


class DocumentStatusResponse(BaseModel):
    id: int
    status: str
    total_pages: int | None
    error_message: str | None
