from rest_framework import serializers
from .models import Lead, LeadAttachment, LeadActivity, Meeting
from apps.users.serializers import UserSerializer


class LeadAttachmentSerializer(serializers.ModelSerializer):
    uploaded_by_name = serializers.CharField(source='uploaded_by.full_name', read_only=True)

    class Meta:
        model = LeadAttachment
        fields = ['id', 'file', 'file_name', 'file_size', 'uploaded_by', 'uploaded_by_name', 'uploaded_at']
        read_only_fields = ['id', 'file_name', 'file_size', 'uploaded_by', 'uploaded_at']


class LeadActivitySerializer(serializers.ModelSerializer):
    actor_name = serializers.CharField(source='actor.full_name', read_only=True)

    class Meta:
        model = LeadActivity
        fields = ['id', 'actor', 'actor_name', 'action', 'details', 'timestamp']
        read_only_fields = ['id', 'timestamp']


class LeadListSerializer(serializers.ModelSerializer):
    created_by_name = serializers.CharField(source='created_by.full_name', read_only=True)
    assigned_pm_name = serializers.CharField(source='assigned_pm.full_name', read_only=True, default=None)

    class Meta:
        model = Lead
        fields = [
            'id', 'title', 'client_name', 'service_type', 'priority',
            'status', 'proposed_budget', 'deadline',
            'created_by', 'created_by_name',
            'assigned_pm', 'assigned_pm_name',
            'created_at', 'updated_at'
        ]


class LeadDetailSerializer(serializers.ModelSerializer):
    created_by_name = serializers.CharField(source='created_by.full_name', read_only=True)
    assigned_pm_name = serializers.CharField(source='assigned_pm.full_name', read_only=True, default=None)
    attachments = LeadAttachmentSerializer(many=True, read_only=True)
    activities = LeadActivitySerializer(many=True, read_only=True)

    class Meta:
        model = Lead
        fields = [
            'id', 'title', 'client_name', 'client_email', 'client_phone',
            'client_whatsapp', 'service_type', 'lead_source',
            'requirements', 'proposed_budget', 'deadline', 'priority', 'status',
            'created_by', 'created_by_name',
            'assigned_pm', 'assigned_pm_name',
            'attachments', 'activities',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_by', 'created_at', 'updated_at']


class LeadCreateSerializer(serializers.ModelSerializer):
    proposed_budget = serializers.DecimalField(
        max_digits=12, decimal_places=2, required=False, allow_null=True, default=0
    )
    deadline = serializers.DateField(required=False, allow_null=True)
    client_email = serializers.EmailField(required=False, allow_blank=True)
    client_phone = serializers.CharField(required=False, allow_blank=True)
    client_whatsapp = serializers.CharField(required=False, allow_blank=True)
    requirements = serializers.CharField(required=False, allow_blank=True)

    class Meta:
        model = Lead
        fields = [
            'title', 'client_name', 'client_email', 'client_phone',
            'client_whatsapp', 'service_type', 'lead_source',
            'requirements', 'proposed_budget', 'deadline', 'priority'
        ]

    def create(self, validated_data):
        validated_data['status'] = 'New'
        validated_data['created_by'] = self.context['request'].user
        return super().create(validated_data)


class MeetingSerializer(serializers.ModelSerializer):
    created_by_name = serializers.CharField(source='created_by.full_name', read_only=True)
    participant_names = serializers.SerializerMethodField()

    class Meta:
        model = Meeting
        fields = [
            'id', 'lead', 'project', 'title', 'meeting_type',
            'date', 'time', 'mode', 'agenda', 'participants',
            'participant_names', 'notes', 'outcome', 'is_completed',
            'created_by', 'created_by_name', 'created_at'
        ]
        read_only_fields = ['id', 'created_by', 'created_at']

    def get_participant_names(self, obj):
        return [{'id': u.id, 'name': u.full_name, 'initials': u.initials} for u in obj.participants.all()]

    def create(self, validated_data):
        participants = validated_data.pop('participants', [])
        validated_data['created_by'] = self.context['request'].user
        meeting = Meeting.objects.create(**validated_data)
        meeting.participants.set(participants)
        return meeting
