"""Types for ChatGPT image generation."""
from __future__ import annotations

from enum import Enum

from pydantic import BaseModel, Field


class ChatGPTModel(str, Enum):
    """Available ChatGPT image models."""
    GPT_5_2 = "gpt-5-2"


class ChatGPTAspectRatio(str, Enum):
    """Image aspect ratios."""
    SQUARE = "1:1"
    LANDSCAPE = "4:3"
    PORTRAIT = "3:4"
    LANDSCAPE_2 = "3:2"
    PORTRAIT_2 = "2:3"


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


class ChatGPTGenerateParams(BaseModel):
    """Image generation request parameters."""
    prompt: str = Field(..., min_length=1, max_length=4000)
    model: ChatGPTModel = Field(default=ChatGPTModel.GPT_5_2)
    aspect_ratio: ChatGPTAspectRatio | None = None
    images: list[str] | None = Field(default=None, max_length=4)
    callback_url: str | None = None

    def to_api_body(self) -> dict:
        data: dict = {
            "prompt": self.prompt,
            "model": self.model.value,
        }
        if self.aspect_ratio:
            data["aspect_ratio"] = self.aspect_ratio.value
        if self.images:
            data["images"] = self.images
        if self.callback_url:
            data["callback_url"] = self.callback_url
        return data


class ChatGPTGenerateResponse(BaseModel):
    """Response from generation endpoint."""
    task_uuid: str
    status: ChatGPTStatus
    price: str


class ChatGPTStatusResponse(BaseModel):
    """Image generation status response."""
    task_uuid: str
    status: ChatGPTStatus
    prompt: str
    result_image_url: str | None = None
    message: str | None = None

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

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

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