"""
Pydantic schemas for page content-related requests and responses.
"""
from pydantic import BaseModel, Field, field_validator
from typing import Optional, Dict, Any, List, Union
import json


class PageContentUpdateRequest(BaseModel):
    """Schema for updating page content."""
    title: Optional[str] = Field(None, max_length=500)
    subtitle: Optional[str] = Field(None, max_length=1000)
    description: Optional[str] = Field(None, max_length=5000)
    content: Optional[str] = None
    meta_title: Optional[str] = Field(None, max_length=200)
    meta_description: Optional[str] = Field(None, max_length=500)
    meta_keywords: Optional[str] = Field(None, max_length=500)
    og_title: Optional[str] = Field(None, max_length=200)
    og_description: Optional[str] = Field(None, max_length=500)
    og_image: Optional[str] = Field(None, max_length=1000)
    canonical_url: Optional[str] = Field(None, max_length=1000)
    contact_info: Optional[Union[str, Dict[str, Any]]] = None
    map_url: Optional[str] = Field(None, max_length=1000)
    social_links: Optional[Union[str, Dict[str, Any], List[Dict[str, Any]]]] = None
    footer_links: Optional[Union[str, Dict[str, Any], List[Dict[str, Any]]]] = None
    badges: Optional[Union[str, List[Dict[str, Any]]]] = None
    hero_title: Optional[str] = Field(None, max_length=500)
    hero_subtitle: Optional[str] = Field(None, max_length=1000)
    hero_image: Optional[str] = Field(None, max_length=1000)
    hero_video_url: Optional[str] = Field(None, max_length=1000)
    hero_video_poster: Optional[str] = Field(None, max_length=1000)
    story_content: Optional[str] = None
    values: Optional[Union[str, List[Dict[str, Any]]]] = None
    features: Optional[Union[str, List[Dict[str, Any]]]] = None
    features_section_title: Optional[str] = Field(None, max_length=500)
    features_section_subtitle: Optional[str] = Field(None, max_length=1000)
    about_hero_image: Optional[str] = Field(None, max_length=1000)
    mission: Optional[str] = Field(None, max_length=2000)
    vision: Optional[str] = Field(None, max_length=2000)
    team: Optional[Union[str, List[Dict[str, Any]]]] = None
    timeline: Optional[Union[str, List[Dict[str, Any]]]] = None
    achievements: Optional[Union[str, List[Dict[str, Any]]]] = None
    amenities_section_title: Optional[str] = Field(None, max_length=500)
    amenities_section_subtitle: Optional[str] = Field(None, max_length=1000)
    amenities: Optional[Union[str, List[Dict[str, Any]]]] = None
    testimonials_section_title: Optional[str] = Field(None, max_length=500)
    testimonials_section_subtitle: Optional[str] = Field(None, max_length=1000)
    testimonials: Optional[Union[str, List[Dict[str, Any]]]] = None
    gallery_section_title: Optional[str] = Field(None, max_length=500)
    gallery_section_subtitle: Optional[str] = Field(None, max_length=1000)
    gallery_images: Optional[Union[str, List[str]]] = None
    luxury_section_title: Optional[str] = Field(None, max_length=500)
    luxury_section_subtitle: Optional[str] = Field(None, max_length=1000)
    luxury_section_image: Optional[str] = Field(None, max_length=1000)
    luxury_features: Optional[Union[str, List[Dict[str, Any]]]] = None
    luxury_gallery_section_title: Optional[str] = Field(None, max_length=500)
    luxury_gallery_section_subtitle: Optional[str] = Field(None, max_length=1000)
    luxury_gallery: Optional[Union[str, List[str]]] = None
    luxury_testimonials_section_title: Optional[str] = Field(None, max_length=500)
    luxury_testimonials_section_subtitle: Optional[str] = Field(None, max_length=1000)
    luxury_testimonials: Optional[Union[str, List[Dict[str, Any]]]] = None
    about_preview_title: Optional[str] = Field(None, max_length=500)
    about_preview_subtitle: Optional[str] = Field(None, max_length=1000)
    about_preview_content: Optional[str] = None
    about_preview_image: Optional[str] = Field(None, max_length=1000)
    stats: Optional[Union[str, List[Dict[str, Any]]]] = None
    stats_section_title: Optional[str] = Field(None, max_length=500)
    stats_section_subtitle: Optional[str] = Field(None, max_length=1000)
    rooms_section_title: Optional[str] = Field(None, max_length=500)
    rooms_section_subtitle: Optional[str] = Field(None, max_length=1000)
    rooms_section_button_text: Optional[str] = Field(None, max_length=200)
    rooms_section_button_link: Optional[str] = Field(None, max_length=1000)
    rooms_section_enabled: Optional[bool] = None
    luxury_services_section_title: Optional[str] = Field(None, max_length=500)
    luxury_services_section_subtitle: Optional[str] = Field(None, max_length=1000)
    services_section_button_text: Optional[str] = Field(None, max_length=200)
    services_section_button_link: Optional[str] = Field(None, max_length=1000)
    services_section_limit: Optional[int] = None
    luxury_services: Optional[Union[str, List[Dict[str, Any]]]] = None
    luxury_experiences_section_title: Optional[str] = Field(None, max_length=500)
    luxury_experiences_section_subtitle: Optional[str] = Field(None, max_length=1000)
    luxury_experiences: Optional[Union[str, List[Dict[str, Any]]]] = None
    awards_section_title: Optional[str] = Field(None, max_length=500)
    awards_section_subtitle: Optional[str] = Field(None, max_length=1000)
    awards: Optional[Union[str, List[Dict[str, Any]]]] = None
    cta_title: Optional[str] = Field(None, max_length=500)
    cta_subtitle: Optional[str] = Field(None, max_length=1000)
    cta_button_text: Optional[str] = Field(None, max_length=200)
    cta_button_link: Optional[str] = Field(None, max_length=1000)
    cta_image: Optional[str] = Field(None, max_length=1000)
    partners_section_title: Optional[str] = Field(None, max_length=500)
    partners_section_subtitle: Optional[str] = Field(None, max_length=1000)
    partners: Optional[Union[str, List[Dict[str, Any]]]] = None
    sections_enabled: Optional[Union[str, Dict[str, bool]]] = None
    newsletter_section_title: Optional[str] = Field(None, max_length=500)
    newsletter_section_subtitle: Optional[str] = Field(None, max_length=1000)
    newsletter_placeholder: Optional[str] = Field(None, max_length=200)
    newsletter_button_text: Optional[str] = Field(None, max_length=200)
    newsletter_enabled: Optional[bool] = None
    trust_badges_section_title: Optional[str] = Field(None, max_length=500)
    trust_badges_section_subtitle: Optional[str] = Field(None, max_length=1000)
    trust_badges: Optional[Union[str, List[Dict[str, Any]]]] = None
    trust_badges_enabled: Optional[bool] = None
    promotions_section_title: Optional[str] = Field(None, max_length=500)
    promotions_section_subtitle: Optional[str] = Field(None, max_length=1000)
    promotions: Optional[Union[str, List[Dict[str, Any]]]] = None
    promotions_enabled: Optional[bool] = None
    blog_section_title: Optional[str] = Field(None, max_length=500)
    blog_section_subtitle: Optional[str] = Field(None, max_length=1000)
    blog_posts_limit: Optional[int] = None
    blog_enabled: Optional[bool] = None
    copyright_text: Optional[str] = Field(None, max_length=500)
    is_active: Optional[bool] = True

    @field_validator('contact_info', 'social_links', 'footer_links', 'badges', 'values', 
                     'features', 'amenities', 'testimonials', 'gallery_images', 'stats',
                     'luxury_features', 'luxury_gallery', 'luxury_testimonials', 
                     'luxury_services', 'luxury_experiences', 'awards', 'partners', 
                     'team', 'timeline', 'achievements', 'sections_enabled', 'trust_badges',
                     'promotions', mode='before')
    @classmethod
    def validate_json_fields(cls, v):
        """Validate and parse JSON string fields."""
        if v is None:
            return None
        if isinstance(v, str):
            try:
                return json.loads(v)
            except json.JSONDecodeError:
                raise ValueError(f'Invalid JSON format: {v}')
        return v

    model_config = {
        "json_schema_extra": {
            "example": {
                "title": "Welcome to Our Hotel",
                "subtitle": "Experience luxury like never before",
                "description": "A beautiful hotel description",
                "is_active": True
            }
        }
    }

