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",
            "list_after_states",
            "list_before_states",
            "geometry",
        ]

    def get_readonly_fields(self, request, obj=None):
        return super().get_readonly_fields(request, obj) + [
            "list_after_states",
            "list_before_states",
            "geometry",
        ]

    def list_after_states(self, obj):
        states = obj.after_states.all()
        states = [str(state) for state in states]
        states = "\n".join(states)
        return states

    def list_before_states(self, obj):
        states = obj.before_states.all()
        states = [str(state) for state in states]
        states = "\n".join(states)
        return 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",
            "is_pik",
            "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",
        "teams",
    ]

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


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)