from rest_framework import serializers
from .models import Invoice, Expense


class InvoiceSerializer(serializers.ModelSerializer):
    created_by_name = serializers.CharField(source='created_by.full_name', read_only=True)
    project_name = serializers.CharField(source='project.name', read_only=True)
    balance_due = serializers.DecimalField(max_digits=12, decimal_places=2, read_only=True)

    class Meta:
        model = Invoice
        fields = [
            'id', 'invoice_number', 'project', 'project_name', 'lead', 
            'client_name', 'client_email', 'amount', 'tax_amount', 'total_amount', 
            'amount_paid', 'balance_due', 'issue_date', 'due_date', 
            'status', 'notes', 'created_by', 'created_by_name', 
            'created_at', 'updated_at'
        ]
        read_only_fields = ['created_by']


class ExpenseSerializer(serializers.ModelSerializer):
    logged_by_name = serializers.CharField(source='logged_by.full_name', read_only=True)
    project_name = serializers.CharField(source='project.name', read_only=True)

    class Meta:
        model = Expense
        fields = [
            'id', 'project', 'project_name', 'title', 'category', 
            'amount', 'date_incurred', 'receipt_file', 'notes', 
            'logged_by', 'logged_by_name', 'created_at'
        ]
        read_only_fields = ['logged_by']
