from rest_framework import serializers
from .models import Tasks, Images, Content
from igny8_core.modules.planner.models import Clusters, ContentIdeas


class TasksSerializer(serializers.ModelSerializer):
    """Serializer for Tasks model"""
    cluster_name = serializers.SerializerMethodField()
    sector_name = serializers.SerializerMethodField()
    idea_title = serializers.SerializerMethodField()
    site_id = serializers.IntegerField(write_only=True, required=False)
    sector_id = serializers.IntegerField(write_only=True, required=False)
    
    class Meta:
        model = Tasks
        fields = [
            'id',
            'title',
            'description',
            'keywords',
            'cluster_id',
            'cluster_name',
            'sector_name',
            'idea_id',
            'idea_title',
            'content_structure',
            'content_type',
            'status',
            'content',
            'word_count',
            'meta_title',
            'meta_description',
            'assigned_post_id',
            'post_url',
            'created_at',
            'updated_at',
            'site_id',
            'sector_id',
            'account_id',
        ]
        read_only_fields = ['id', 'created_at', 'updated_at', 'account_id']
    
    def get_cluster_name(self, obj):
        """Get cluster name from Clusters model"""
        if obj.cluster_id:
            try:
                cluster = Clusters.objects.get(id=obj.cluster_id)
                return cluster.name
            except Clusters.DoesNotExist:
                return None
        return None
    
    def get_sector_name(self, obj):
        """Get sector name from Sector model"""
        if obj.sector_id:
            try:
                from igny8_core.auth.models import Sector
                sector = Sector.objects.get(id=obj.sector_id)
                return sector.name
            except Sector.DoesNotExist:
                return None
        return None
    
    def get_idea_title(self, obj):
        """Get idea title from ContentIdeas model"""
        if obj.idea_id:
            try:
                idea = ContentIdeas.objects.get(id=obj.idea_id)
                return idea.idea_title
            except ContentIdeas.DoesNotExist:
                return None
        return None


class ImagesSerializer(serializers.ModelSerializer):
    """Serializer for Images model"""
    task_title = serializers.SerializerMethodField()
    
    class Meta:
        model = Images
        fields = [
            'id',
            'task_id',
            'task_title',
            'image_type',
            'image_url',
            'image_path',
            'prompt',
            'status',
            'position',
            'created_at',
            'updated_at',
            'account_id',
        ]
        read_only_fields = ['id', 'created_at', 'updated_at', 'account_id']
    
    def get_task_title(self, obj):
        """Get task title"""
        if obj.task_id:
            try:
                task = Tasks.objects.get(id=obj.task_id)
                return task.title
            except Tasks.DoesNotExist:
                return None
        return None


class ContentSerializer(serializers.ModelSerializer):
    """Serializer for Content model"""
    task_title = serializers.SerializerMethodField()
    
    class Meta:
        model = Content
        fields = [
            'id',
            'task_id',
            'task_title',
            'html_content',
            'word_count',
            'metadata',
            'generated_at',
            'updated_at',
            'account_id',
        ]
        read_only_fields = ['id', 'generated_at', 'updated_at', 'account_id']
    
    def get_task_title(self, obj):
        """Get task title"""
        if obj.task_id:
            try:
                task = Tasks.objects.get(id=obj.task_id)
                return task.title
            except Tasks.DoesNotExist:
                return None
        return None

