from datetime import datetime
from typing import Literal

from pydantic import BaseModel, Field

VttKind = Literal["captions", "ad"]


class VttVersionActor(BaseModel):
    user_id: str
    user_email: str


class VttVersion(BaseModel):
    id: str | None = Field(None, alias="_id")
    job_id: str
    lang: str
    kind: VttKind
    version: int
    content: str
    gcs_uri: str
    created_at: datetime = Field(default_factory=datetime.utcnow)
    created_by: VttVersionActor
    note: str | None = None
    parent_version: int | None = None
    cue_count: int = 0
    byte_size: int = 0

    class Config:
        populate_by_name = True


class VttVersionSummary(BaseModel):
    """Lightweight version entry for list responses (no content)."""
    id: str | None = Field(None, alias="_id")
    job_id: str
    lang: str
    kind: VttKind
    version: int
    gcs_uri: str
    created_at: datetime
    created_by: VttVersionActor
    note: str | None = None
    parent_version: int | None = None
    cue_count: int = 0
    byte_size: int = 0

    class Config:
        populate_by_name = True


class VttVersionListResponse(BaseModel):
    versions: list[VttVersionSummary]
    total: int


class DiffLine(BaseModel):
    type: Literal["unchanged", "added", "removed"]
    content: str
    line_no_old: int | None = None
    line_no_new: int | None = None


class VttDiffResponse(BaseModel):
    job_id: str
    lang: str
    kind: VttKind
    from_version: int
    to_version: int
    lines: list[DiffLine]
    added_count: int
    removed_count: int
