from django.contrib import admin
from django.http import HttpRequest
from django.http.response import HttpResponse
from .models import AppDashboardMessage, Banners, DefaultDistributor, KYCSetting, ManualRecharge, Operator, MRobotics, State, ServiceCategory, TransferRecharge, WhatsappInstance, WhatsappMessages
from helper import calculate_percentage_commission, calculate_overall_fre_amount


# Register your models here.

class ManualRechargeAdmin(admin.ModelAdmin):

    list_display = ["operator", "is_active"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)


class TransferRechargeAdmin(admin.ModelAdmin):

    list_display = ["operator_from", "amount", "operator_to", "is_active"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)


class ServiceCategoryAdmin(admin.ModelAdmin):

    list_display = ["name", "is_active"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)


class OperatorAdmin(admin.ModelAdmin):

    list_filter = ["service"]
    list_display = [
        "name",
        "service",
        "commission",
        "forward_commission",
        "distributor_commission",
        "retailer_commission",
    ]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        # percentage = calculate_percentage_commission(
        #     obj.forward_commission, obj.commission
        # )
        # obj.percentage = percentage
        obj.save()

        super().save_model(request, obj, form, change)

    def response_change(self, request, obj):
        try:
            # if obj.percentage != 0:
            calculate_overall_fre_amount(obj)
            return super().response_change(request, obj)
        except Exception as error:
            print(error)


class MRoboticsAdmin(admin.ModelAdmin):

    list_display = ["lapu_id", "lapu_no", "operator", "balance"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)


class StateAdmin(admin.ModelAdmin):

    list_display = ["name", "api_code", "is_active"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)


class BannersAdmin(admin.ModelAdmin):

    list_display = ["name", "image", "is_active"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)


class DefaultDistributorAdmin(admin.ModelAdmin):

    list_display = ["distributor", "created_on", "created_by"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)

class AppDashboardMessageAdmin(admin.ModelAdmin):
    list_display = ["message", "created_on", "created_by"]

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        super().save_model(request, obj, form, change)


class WhatsappInstanceAdmin(admin.ModelAdmin):
    list_display = ["instance_id"]

class WhatsappMessagesAdmin(admin.ModelAdmin):
    list_display = ["name"]


class KYCSettingAdmin(admin.ModelAdmin):
    list_display = ["amount"]

admin.site.register(TransferRecharge, TransferRechargeAdmin)
admin.site.register(ManualRecharge, ManualRechargeAdmin)
admin.site.register(Operator, OperatorAdmin)
admin.site.register(MRobotics, MRoboticsAdmin)
admin.site.register(State, StateAdmin)
admin.site.register(KYCSetting, KYCSettingAdmin)
admin.site.register(ServiceCategory, ServiceCategoryAdmin)
admin.site.register(Banners, BannersAdmin)
admin.site.register(WhatsappInstance, WhatsappInstanceAdmin)
admin.site.register(WhatsappMessages, WhatsappMessagesAdmin)
admin.site.register(DefaultDistributor, DefaultDistributorAdmin)
admin.site.register(AppDashboardMessage, AppDashboardMessageAdmin)
