from django.contrib import admin

from compensation.models import Compensation, CompensationAction, CompensationState, Payment, \
    EcoAccountDeduction, EcoAccount
from konova.admin import BaseObjectAdmin, BaseResourceAdmin
from konova.utils.message_templates import COMPENSATION_REMOVED_TEMPLATE
from user.models import UserAction


class AbstractCompensationAdmin(BaseObjectAdmin):
    list_display = [
        "id",
        "identifier",
        "title",
        "created",
        "deleted",
    ]

    def get_fields(self, request, obj=None):
        return super().get_fields(request, obj) + [
            "identifier",
            "title",
            "comment",
            "after_states",
            "before_states",
        ]

    def get_readonly_fields(self, request, obj=None):
        return super().get_readonly_fields(request, obj) + [
            "after_states",
            "before_states",
        ]


class CompensationAdmin(AbstractCompensationAdmin):
    autocomplete_fields = [
        "intervention",
    ]

    def get_fields(self, request, obj=None):
        return super().get_fields(request, obj) + [
            "is_cef",
            "is_coherence_keeping",
            "intervention",
        ]

    def restore_deleted_data(self, request, queryset):
        super().restore_deleted_data(request, queryset)

        for entry in queryset:
            # Remove delete log entry from related intervention log history
            logs = entry.intervention.log.filter(
                action=UserAction.EDITED,
                comment=COMPENSATION_REMOVED_TEMPLATE.format(entry.identifier)
            )
            logs.delete()


class EcoAccountAdmin(AbstractCompensationAdmin):
    list_display = [
        "id",
        "identifier",
        "title",
        "created",
        "deleted",
    ]

    filter_horizontal = [
        "users"
    ]

    def get_fields(self, request, obj=None):
        return super().get_fields(request, obj) + [
            "deductable_surface",
            "users"
        ]


class PaymentAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "amount",
        "due_on",
    ]


class EcoAccountDeductionAdmin(BaseResourceAdmin):
    list_display = [
        "id",
        "account",
        "intervention",
        "surface",
    ]
    search_fields = [
        "account__identifier",
        "account__title",
        "intervention__identifier",
        "intervention__title",
        "surface",
    ]
    autocomplete_fields = [
        "account",
        "intervention",
    ]

    def get_fields(self, request, obj=None):
        return super().get_fields(request, obj) + [
            "account",
            "intervention",
            "surface",
        ]


class CompensationStateAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "biotope_type",
        "surface",
    ]


class CompensationActionAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "action_type",
        "amount",
        "unit",
        "comment",
    ]


admin.site.register(Compensation, CompensationAdmin)
admin.site.register(EcoAccount, EcoAccountAdmin)
admin.site.register(EcoAccountDeduction, EcoAccountDeductionAdmin)

# For a more cleaner admin interface these rarely used admin views are not important for deployment
#admin.site.register(Payment, PaymentAdmin)
#admin.site.register(CompensationAction, CompensationActionAdmin)
#admin.site.register(CompensationState, CompensationStateAdmin)