from typing import Optional
from pydantic import AwareDatetime, BaseModel, EmailStr


class BlogBase(BaseModel):
    title: str
    author_id: int
    author_name: str
    description: Optional[str] = None
    body: str
    created_at: AwareDatetime
    updated_at: AwareDatetime
    published_at: AwareDatetime

    # Document meta
    word_count: Optional[int] = None
    version: Optional[int] = None
    read_time: Optional[int] = None
    language: Optional[str] = None
    tags: Optional[list[str]] = None


class BlogCreate(BlogBase):
    """All fields required to create a new blog post."""

    pass


class BlogUpdate(BaseModel):
    """All fields are optional for partial updates."""

    title: Optional[str] = None
    description: Optional[str] = None
    author_name: Optional[str] = None
    body: Optional[str] = None
    created_at: Optional[str] = None
    updated_at: Optional[str] = None
    published_at: Optional[str] = None

    word_count: Optional[int] = None
    version: Optional[int] = None
    read_time: Optional[int] = None
    language: Optional[str] = None
    tags: Optional[list[str]] = None

    view_count: Optional[int] = None
    like_count: Optional[int] = None


class Blog(BlogBase):
    """What’s returned in responses."""

    id: int
    view_count: int
    like_count: int

    class Config:
        from_attributes = True


class UserBase(BaseModel):
    username: str
    email: EmailStr


class UserCreate(UserBase):
    password: str
    permissions: dict = {}
    subscriber: bool = False


class UserOut(UserBase):
    id: int

    class Config:
        from_attributes = True


# Other Schemas


class Token(BaseModel):
    access_token: str
    token_type: str
    user_id: int


class TokenData(BaseModel):
    username: str | None = None


class UserLogin(BaseModel):
    username: str
    password: str
