from rest_framework import serializers
from .models import JobPosition, JobApplication


class JobPositionListSerializer(serializers.ModelSerializer):
    """Serializer for job position list view"""
    
    class Meta:
        model = JobPosition
        fields = [
            'id',
            'title',
            'slug',
            'department',
            'employment_type',
            'location_type',
            'location',
            'open_positions',
            'short_description',
            'salary_min',
            'salary_max',
            'salary_currency',
            'salary_period',
            'posted_date',
            'status',
            'featured',
        ]


class JobPositionDetailSerializer(serializers.ModelSerializer):
    """Serializer for job position detail view"""
    
    class Meta:
        model = JobPosition
        fields = [
            'id',
            'title',
            'slug',
            'department',
            'employment_type',
            'location_type',
            'location',
            'open_positions',
            'experience_required',
            'salary_min',
            'salary_max',
            'salary_currency',
            'salary_period',
            'salary_additional',
            'short_description',
            'about_role',
            'requirements',
            'responsibilities',
            'qualifications',
            'bonus_points',
            'benefits',
            'start_date',
            'posted_date',
            'updated_date',
            'deadline',
            'status',
        ]


class JobApplicationSerializer(serializers.ModelSerializer):
    """Serializer for job application submission"""
    
    job_title = serializers.CharField(source='job.title', read_only=True)
    
    class Meta:
        model = JobApplication
        fields = [
            'id',
            'job',
            'job_title',
            'first_name',
            'last_name',
            'email',
            'phone',
            'current_position',
            'current_company',
            'years_of_experience',
            'cover_letter',
            'resume',
            'portfolio_url',
            'linkedin_url',
            'github_url',
            'website_url',
            'available_from',
            'notice_period',
            'expected_salary',
            'salary_currency',
            'consent',
            'applied_date',
        ]
        read_only_fields = ['id', 'applied_date', 'job_title']
    
    def validate_resume(self, value):
        """Validate resume file size"""
        if value.size > 5 * 1024 * 1024:  # 5MB
            raise serializers.ValidationError("Resume file size cannot exceed 5MB")
        return value
    
    def validate_consent(self, value):
        """Ensure user has given consent"""
        if not value:
            raise serializers.ValidationError("You must consent to data processing to apply")
        return value


class JobApplicationListSerializer(serializers.ModelSerializer):
    """Serializer for listing job applications (admin view)"""
    
    job_title = serializers.CharField(source='job.title', read_only=True)
    full_name = serializers.CharField(read_only=True)
    
    class Meta:
        model = JobApplication
        fields = [
            'id',
            'job',
            'job_title',
            'full_name',
            'email',
            'phone',
            'status',
            'applied_date',
            'resume',
        ]
        read_only_fields = fields

