"""
Serializers for analytics models.
"""
from rest_framework import serializers
from .models import (
    Broker, Developer, Project, Land, Transaction, Valuation, Rent,
    Forecast, MarketTrend
)


class BrokerSerializer(serializers.ModelSerializer):
    """Serializer for Broker model."""
    class Meta:
        model = Broker
        fields = '__all__'


class DeveloperSerializer(serializers.ModelSerializer):
    """Serializer for Developer model."""
    class Meta:
        model = Developer
        fields = '__all__'


class ProjectSerializer(serializers.ModelSerializer):
    """Serializer for Project model."""
    developer_name = serializers.CharField(source='developer.developer_name_en', read_only=True)
    
    class Meta:
        model = Project
        fields = '__all__'


class LandSerializer(serializers.ModelSerializer):
    """Serializer for Land model."""
    project_name = serializers.CharField(source='project.project_name_en', read_only=True)
    
    class Meta:
        model = Land
        fields = '__all__'


class TransactionSerializer(serializers.ModelSerializer):
    """Serializer for Transaction model."""
    project_name = serializers.CharField(source='project.project_name_en', read_only=True)
    price_per_sqft = serializers.ReadOnlyField()
    
    class Meta:
        model = Transaction
        fields = '__all__'


class ValuationSerializer(serializers.ModelSerializer):
    """Serializer for Valuation model."""
    class Meta:
        model = Valuation
        fields = '__all__'


class RentSerializer(serializers.ModelSerializer):
    """Serializer for Rent model."""
    project_name = serializers.CharField(source='project.project_name_en', read_only=True)
    rent_per_sqft = serializers.ReadOnlyField()
    
    class Meta:
        model = Rent
        fields = '__all__'


class ForecastSerializer(serializers.ModelSerializer):
    """Serializer for Forecast model."""
    class Meta:
        model = Forecast
        fields = '__all__'


class MarketTrendSerializer(serializers.ModelSerializer):
    """Serializer for MarketTrend model."""
    class Meta:
        model = MarketTrend
        fields = '__all__'


class TransactionSummarySerializer(serializers.Serializer):
    """Serializer for transaction summary statistics."""
    total_transactions = serializers.IntegerField()
    total_value = serializers.DecimalField(max_digits=15, decimal_places=2)
    average_price = serializers.DecimalField(max_digits=15, decimal_places=2)
    median_price = serializers.DecimalField(max_digits=15, decimal_places=2)
    average_price_per_sqft = serializers.DecimalField(max_digits=10, decimal_places=2)
    price_range_min = serializers.DecimalField(max_digits=15, decimal_places=2)
    price_range_max = serializers.DecimalField(max_digits=15, decimal_places=2)


class AreaStatsSerializer(serializers.Serializer):
    """Serializer for area statistics."""
    area = serializers.CharField()
    transaction_count = serializers.IntegerField()
    average_price = serializers.DecimalField(max_digits=15, decimal_places=2)
    average_price_per_sqft = serializers.DecimalField(max_digits=10, decimal_places=2)
    price_trend = serializers.CharField()


class PropertyTypeStatsSerializer(serializers.Serializer):
    """Serializer for property type statistics."""
    property_type = serializers.CharField()
    transaction_count = serializers.IntegerField()
    average_price = serializers.DecimalField(max_digits=15, decimal_places=2)
    average_price_per_sqft = serializers.DecimalField(max_digits=10, decimal_places=2)
    market_share = serializers.DecimalField(max_digits=5, decimal_places=2)


class TimeSeriesDataSerializer(serializers.Serializer):
    """Serializer for time series data."""
    date = serializers.DateTimeField()
    value = serializers.DecimalField(max_digits=15, decimal_places=2)
    count = serializers.IntegerField()


class ForecastRequestSerializer(serializers.Serializer):
    """Serializer for forecast requests."""
    area_en = serializers.CharField(max_length=100)
    property_type = serializers.CharField(max_length=50)
    property_sub_type = serializers.CharField(max_length=50, required=False, allow_blank=True)
    forecast_periods = serializers.IntegerField(default=12, min_value=1, max_value=60)
    confidence_level = serializers.DecimalField(default=0.95, max_digits=3, decimal_places=2)


class MarketAnalysisSerializer(serializers.Serializer):
    """Serializer for market analysis results."""
    area = serializers.CharField()
    property_type = serializers.CharField()
    analysis_period = serializers.CharField()
    key_metrics = serializers.DictField()
    trends = serializers.ListField()
    recommendations = serializers.ListField()
    forecast_accuracy = serializers.DecimalField(max_digits=5, decimal_places=2, allow_null=True)

