from rest_framework import serializers
from .models import (
    AboutBanner, AboutStat, AboutSocialLink,
    AboutService, AboutFeature,
    AboutProcess, AboutProcessStep,
    AboutJourney, AboutMilestone
)


class AboutStatSerializer(serializers.ModelSerializer):
    class Meta:
        model = AboutStat
        fields = ['number', 'label', 'order']


class AboutSocialLinkSerializer(serializers.ModelSerializer):
    class Meta:
        model = AboutSocialLink
        fields = ['platform', 'url', 'icon', 'aria_label', 'order']


class AboutBannerSerializer(serializers.ModelSerializer):
    stats = AboutStatSerializer(many=True, read_only=True)
    social_links = AboutSocialLinkSerializer(many=True, read_only=True)
    image_url = serializers.SerializerMethodField()

    class Meta:
        model = AboutBanner
        fields = [
            'id', 'title', 'subtitle', 'description', 'badge_text', 'badge_icon',
            'cta_text', 'cta_link', 'cta_icon', 'image_url', 'is_active',
            'stats', 'social_links', 'created_at', 'updated_at'
        ]

    def get_image_url(self, obj):
        if obj.image:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.image.url)
            return obj.image.url
        return None


class AboutFeatureSerializer(serializers.ModelSerializer):
    class Meta:
        model = AboutFeature
        fields = ['title', 'description', 'icon', 'order']


class AboutServiceSerializer(serializers.ModelSerializer):
    features = AboutFeatureSerializer(many=True, read_only=True)
    image_url = serializers.SerializerMethodField()

    class Meta:
        model = AboutService
        fields = [
            'id', 'title', 'subtitle', 'description', 'badge_text', 'badge_icon',
            'image_url', 'cta_text', 'cta_link', 'is_active',
            'features', 'created_at', 'updated_at'
        ]

    def get_image_url(self, obj):
        if obj.image:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.image.url)
            return obj.image.url
        return None


class AboutProcessStepSerializer(serializers.ModelSerializer):
    class Meta:
        model = AboutProcessStep
        fields = ['step_number', 'title', 'description', 'order']


class AboutProcessSerializer(serializers.ModelSerializer):
    steps = AboutProcessStepSerializer(many=True, read_only=True)
    image_url = serializers.SerializerMethodField()

    class Meta:
        model = AboutProcess
        fields = [
            'id', 'title', 'subtitle', 'description', 'badge_text', 'badge_icon',
            'image_url', 'cta_text', 'cta_link', 'is_active',
            'steps', 'created_at', 'updated_at'
        ]

    def get_image_url(self, obj):
        if obj.image:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.image.url)
            return obj.image.url
        return None


class AboutMilestoneSerializer(serializers.ModelSerializer):
    class Meta:
        model = AboutMilestone
        fields = ['year', 'title', 'description', 'order']


class AboutJourneySerializer(serializers.ModelSerializer):
    milestones = AboutMilestoneSerializer(many=True, read_only=True)
    image_url = serializers.SerializerMethodField()

    class Meta:
        model = AboutJourney
        fields = [
            'id', 'title', 'subtitle', 'description', 'badge_text', 'badge_icon',
            'image_url', 'cta_text', 'cta_link', 'is_active',
            'milestones', 'created_at', 'updated_at'
        ]

    def get_image_url(self, obj):
        if obj.image:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.image.url)
            return obj.image.url
        return None


class AboutPageSerializer(serializers.Serializer):
    """Combined serializer for the entire about page"""
    banner = AboutBannerSerializer()
    service = AboutServiceSerializer()
    process = AboutProcessSerializer()
    journey = AboutJourneySerializer()
