from rest_framework import serializers
from .models import Job, JobFavorite
from apps.organizations.serializers import OrganizationSerializer
from apps.organizations.models import Organization


class JobListSerializer(serializers.ModelSerializer):
    organization_name = serializers.CharField(source='organization.name', read_only=True)

    class Meta:
        model = Job
        fields = ['id', 'title', 'category', 'location', 'salary', 'education',
                  'organization', 'organization_name', 'status', 'created_at']


class JobDetailSerializer(serializers.ModelSerializer):
    organization = OrganizationSerializer(read_only=True)
    organization_id = serializers.PrimaryKeyRelatedField(
        source='organization',
        queryset=Organization.objects.all(),
        write_only=True,
        required=False
    )

    class Meta:
        model = Job
        fields = ['id', 'title', 'category', 'location', 'salary', 'education',
                  'description', 'organization', 'organization_id', 'status', 'created_at']


class JobFavoriteSerializer(serializers.ModelSerializer):
    job = JobListSerializer(read_only=True)

    class Meta:
        model = JobFavorite
        fields = ['id', 'job', 'created_at']
