from rest_framework import serializers
from .models import Site, SiteConfig, Page, Domain
from .validators import validate_puck_data
from smoothschedule.scheduling.schedule.models import Service, Event


class SiteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Site
        fields = ['id', 'tenant', 'primary_domain', 'is_enabled', 'template_key', 'created_at', 'updated_at']
        read_only_fields = ['id', 'tenant', 'created_at', 'updated_at']


class SiteConfigSerializer(serializers.ModelSerializer):
    """Serializer for site configuration (theme, header, footer)."""

    class Meta:
        model = SiteConfig
        fields = [
            'id', 'theme', 'header', 'footer', 'version',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at', 'version']

    def to_representation(self, instance):
        """Include merged theme with defaults."""
        data = super().to_representation(instance)
        # Add computed fields for convenience
        data['merged_theme'] = instance.get_merged_theme()
        return data


class PageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page
        fields = [
            'id', 'slug', 'path', 'title', 'is_home', 'is_published', 'order',
            'puck_data', 'version',
            # SEO fields
            'meta_title', 'meta_description', 'og_image', 'canonical_url', 'noindex',
            # Navigation and chrome
            'include_in_nav', 'hide_chrome',
            # Timestamps
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'site', 'slug', 'path', 'created_at', 'updated_at', 'version']

    def validate_puck_data(self, value):
        """Validate puck_data for structure and security."""
        return validate_puck_data(value)


class DomainSerializer(serializers.ModelSerializer):
    class Meta:
        model = Domain
        fields = ['id', 'host', 'is_primary', 'is_verified', 'verification_token', 'created_at']
        read_only_fields = ['id', 'is_verified', 'verification_token', 'created_at']


class PublicPageSerializer(serializers.ModelSerializer):
    """Serializer for public page rendering (includes SEO fields)."""

    class Meta:
        model = Page
        fields = [
            'title', 'puck_data',
            # SEO fields for meta tags
            'meta_title', 'meta_description', 'og_image', 'canonical_url', 'noindex',
            # Chrome control
            'hide_chrome'
        ]


class PublicSiteConfigSerializer(serializers.ModelSerializer):
    """Serializer for public site config (theme only, no edit permissions)."""

    merged_theme = serializers.SerializerMethodField()

    class Meta:
        model = SiteConfig
        fields = ['theme', 'header', 'footer', 'merged_theme']

    def get_merged_theme(self, obj):
        return obj.get_merged_theme()


class PublicServiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Service
        fields = ['id', 'name', 'description', 'duration', 'price_cents', 'deposit_amount_cents', 'photos']


class PublicBookingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ['id', 'start_time', 'end_time', 'status']
