"""
Serializers for Billing Models
"""
from rest_framework import serializers
from .models import CreditTransaction, CreditUsageLog
from igny8_core.auth.models import Account


class CreditTransactionSerializer(serializers.ModelSerializer):
    transaction_type_display = serializers.CharField(source='get_transaction_type_display', read_only=True)
    
    class Meta:
        model = CreditTransaction
        fields = [
            'id', 'transaction_type', 'transaction_type_display', 'amount',
            'balance_after', 'description', 'metadata', 'created_at'
        ]
        read_only_fields = ['created_at', 'account']


class CreditUsageLogSerializer(serializers.ModelSerializer):
    operation_type_display = serializers.CharField(source='get_operation_type_display', read_only=True)
    
    class Meta:
        model = CreditUsageLog
        fields = [
            'id', 'operation_type', 'operation_type_display', 'credits_used',
            'cost_usd', 'model_used', 'tokens_input', 'tokens_output',
            'related_object_type', 'related_object_id', 'metadata', 'created_at'
        ]
        read_only_fields = ['created_at', 'account']


class CreditBalanceSerializer(serializers.Serializer):
    """Serializer for credit balance response"""
    credits = serializers.IntegerField()
    plan_credits_per_month = serializers.IntegerField()
    credits_used_this_month = serializers.IntegerField()
    credits_remaining = serializers.IntegerField()


class UsageSummarySerializer(serializers.Serializer):
    """Serializer for usage summary response"""
    period = serializers.DictField()
    total_credits_used = serializers.IntegerField()
    total_cost_usd = serializers.DecimalField(max_digits=10, decimal_places=2)
    by_operation = serializers.DictField()
    by_model = serializers.DictField()


class LimitCardSerializer(serializers.Serializer):
    """Serializer for individual limit card"""
    title = serializers.CharField()
    limit = serializers.IntegerField()
    used = serializers.IntegerField()
    available = serializers.IntegerField()
    unit = serializers.CharField()
    category = serializers.CharField()
    percentage = serializers.FloatField()


class UsageLimitsSerializer(serializers.Serializer):
    """Serializer for usage limits response"""
    limits = LimitCardSerializer(many=True)

