"""Types for Veo 3 video generation."""
from __future__ import annotations

from enum import Enum
from pydantic import BaseModel, Field


class VeoModel(str, Enum):
    """Available Veo 3 models."""
    FAST = "veo_3_fast"
    FAST_HD = "veo_3_fast_hd"
    FAST_4K = "veo_3_fast_4k"
    QUALITY = "veo_3_quality"
    QUALITY_HD = "veo_3_quality_hd"
    QUALITY_4K = "veo_3_quality_4k"


class VeoAspectRatio(str, Enum):
    """Video aspect ratios."""
    LANDSCAPE = "16:9"
    PORTRAIT = "9:16"


class VeoStatus(str, Enum):
    """Generation status."""
    PENDING = "pending"
    PROCESSING = "processing"
    COMPLETED = "completed"
    FAILED = "failed"


class VeoGenerateParams(BaseModel):
    """Video generation request parameters."""
    prompt: str = Field(..., min_length=10, max_length=4000)
    model: VeoModel = Field(default=VeoModel.FAST)
    aspect_ratio: VeoAspectRatio = Field(default=VeoAspectRatio.LANDSCAPE)
    seed: int | None = Field(default=None, ge=0, le=1000000000)
    callback_url: str | None = None
    watermark_text: str | None = Field(default=None, min_length=3, max_length=14)
    bytes_image: str | None = None
    start_frame: str | None = None
    end_frame: str | None = None
    reference_images: list[str] | None = Field(default=None, max_length=3)

    def to_api_body(self) -> dict:
        data: dict = {
            "prompt": self.prompt,
            "model": self.model.value,
            "aspect_ratio": self.aspect_ratio.value,
        }
        if self.seed is not None:
            data["seed"] = self.seed
        if self.callback_url:
            data["callback_url"] = self.callback_url
        if self.watermark_text:
            data["watermark_text"] = self.watermark_text
        if self.bytes_image:
            data["bytes_image"] = self.bytes_image
        if self.start_frame:
            data["start_frame"] = self.start_frame
        if self.end_frame:
            data["end_frame"] = self.end_frame
        if self.reference_images:
            data["reference_images"] = self.reference_images
        return data


class VeoGenerateResponse(BaseModel):
    """Response from POST /v1/veo/generate."""
    generation_id: str
    status: VeoStatus
    seed: int
    price: str


class VeoStatusResponse(BaseModel):
    """Video generation status response."""
    generation_id: str
    status: VeoStatus
    prompt: str
    video_url: str | None = None
    created_at: str | None = None
    completed_at: str | None = None
    seed: int | None = None
    error: dict | None = None

    @property
    def is_terminal(self) -> bool:
        return self.status in (VeoStatus.COMPLETED, VeoStatus.FAILED)

    @property
    def is_completed(self) -> bool:
        return self.status == VeoStatus.COMPLETED

    @property
    def is_failed(self) -> bool:
        return self.status == VeoStatus.FAILED
