from __future__ import annotations

from rest_framework import serializers

from .models import (
    BlogPost,
    ContactSubmission,
    Founder,
    JobApplication,
    JobOpening,
    NewsletterSignup,
    Product,
)


class ProductSerializer(serializers.ModelSerializer):
    category_label = serializers.CharField(source="get_category_display", read_only=True)

    class Meta:
        model = Product
        fields = [
            "slug",
            "name",
            "category",
            "category_label",
            "tagline",
            "summary",
            "description",
            "benefits",
            "features",
            "spec_table",
            "primary_cta_label",
        ]


class FounderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Founder
        fields = ["name", "role", "domain", "bio", "photo_url", "linkedin_url"]


class BlogPostListSerializer(serializers.ModelSerializer):
    category_label = serializers.CharField(source="get_category_display", read_only=True)

    class Meta:
        model = BlogPost
        fields = [
            "slug",
            "title",
            "excerpt",
            "category",
            "category_label",
            "author_name",
            "read_time_minutes",
            "cover_image_url",
            "published_at",
        ]


class BlogPostDetailSerializer(BlogPostListSerializer):
    class Meta(BlogPostListSerializer.Meta):
        fields = BlogPostListSerializer.Meta.fields + ["body"]


class JobOpeningSerializer(serializers.ModelSerializer):
    location_label = serializers.CharField(source="get_location_display", read_only=True)

    class Meta:
        model = JobOpening
        fields = [
            "slug",
            "title",
            "location",
            "location_label",
            "employment_type",
            "description",
            "posted_at",
        ]


class ContactSubmissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = ContactSubmission
        fields = [
            "name",
            "email",
            "company",
            "role",
            "country",
            "interest",
            "message",
            "referrer",
        ]
        extra_kwargs = {
            "name": {"required": True, "allow_blank": False},
            "email": {"required": True},
            "message": {"required": True, "allow_blank": False},
        }

    def validate_message(self, value: str) -> str:
        if len(value.strip()) < 10:
            raise serializers.ValidationError(
                "Please write a few words about what you're looking for."
            )
        return value.strip()


class NewsletterSignupSerializer(serializers.ModelSerializer):
    class Meta:
        model = NewsletterSignup
        fields = ["email", "source"]

    def create(self, validated_data):
        email = validated_data["email"].lower().strip()
        obj, _ = NewsletterSignup.objects.get_or_create(
            email=email,
            defaults={"source": validated_data.get("source", "")},
        )
        return obj


class JobApplicationSerializer(serializers.ModelSerializer):
    class Meta:
        model = JobApplication
        fields = ["name", "email", "role_applied_for", "portfolio_url", "message", "cv"]
