from pydantic import BaseModel, UUID4, Field, HttpUrl
from typing import Optional
from datetime import datetime
from enum import Enum
from pydantic import validator

class ContentType(str, Enum):
    SONG = "song"
    STORY = "story"

class ContentStatus(str, Enum):
    SUBMITTED = "submitted"
    QUEUED = "queued"
    STREAMING = "streaming"
    COMPLETE = "complete"
    ERROR = "error"

class Child(BaseModel):
    id: UUID4
    name: str
    birth_date: str
    avatar_url: str

class ContentCreate(BaseModel):
    type: ContentType
    topic: str
    persona_id: int

class SunoMetadata(BaseModel):
    tags: Optional[str] = Field(None, description="Music style tags")
    prompt: Optional[str] = Field(None, description="Generated lyrics/prompt")
    gpt_description_prompt: Optional[str] = Field(None, description="Original topic provided")
    lyrics: Optional[str] = Field(None, description="Field to store generated lyrics")
    type: str = Field("gen", description="Type of generation")
    duration: Optional[float] = Field(None, description="Duration of generated audio in seconds")
    error_type: Optional[str] = Field(None, description="Error type if generation failed")
    error_message: Optional[str] = Field(None, description="Error message if generation failed")

    @validator('error_type', 'error_message')
    def check_error_type_and_message(cls, v, values, **kwargs):
        field_name = kwargs.get('field_name')
        error_type = values.get('error_type')
        error_message = values.get('error_message')
        
        if field_name == 'error_message' and error_message is not None and error_type is None:
            raise ValueError("error_message can only be provided if error_type is not None.")
        if field_name == 'error_type' and error_type is not None and 'error_message' in values and values['error_message'] is None:
            raise ValueError("error_type can only be provided if error_message is not None.")
        return v

class SunoGenerateResponse(BaseModel):
    id: UUID4 = Field(..., description="Unique identifier for the generation")
    request_id: Optional[str] = Field(None, description="Request ID for streaming events")
    video_url: Optional[str] = Field(None, description="URL to generated video content")
    audio_url: Optional[str] = Field(None, description="URL to generated audio content")
    image_url: Optional[str] = Field(None, description="URL to generated image content")
    image_large_url: Optional[str] = Field(None, description="URL to large generated image content")
    created_at: datetime = Field(..., description="Timestamp of generation creation")
    status: Optional[str] = Field(None, description="Current status of the generation")
    title: Optional[str] = Field(None, description="Title of the generated content")
    metadata: SunoMetadata = Field(..., description="Additional metadata about the generation")

    class Config:
        use_enum_values = True
        json_schema_extra = {
            "example": {
                "id": "af89176a-38c3-4c6b-bec2-1382d08772a4",
                "video_url": "https://cdn1.suno.ai/af89176a-38c3-4c6b-bec2-1382d08772a4.mp4",
                "audio_url": "https://cdn1.suno.ai/af89176a-38c3-4c6b-bec2-1382d08772a4.mp3",
                "image_url": "https://cdn1.suno.ai/af89176a-38c3-4c6b-bec2-1382d08772a4.png",
                "image_large_url": "https://cdn1.suno.ai/af89176a-38c3-4c6b-bec2-1382d08772a4-large.png",
                "metadata": {
                    "tags": "pop uplifting festive",
                    "prompt": "It's Christmas Time",
                    "gpt_description_prompt": "A song about traveling on Christmas",
                    "lyrics": "Verse 1: Packing up the car, on Christmas Eve...",
                    "type": "gen",
                    "duration": 59.99997916666667,
                },
                "created_at": "2023-12-24T12:00:00Z",
                "status": "complete",
                "title": "Christmas Journey"
            }
        }


class Content(BaseModel):
    id: UUID4
    child_id: UUID4
    type: ContentType
    topic: str
    persona_id: int
    suno_data: SunoGenerateResponse

class Persona(BaseModel):
    id: int
    name: str
    description: str
