from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from servala.core.models import (
    BillingEntity,
    CloudProvider,
    ControlPlane,
    Organization,
    OrganizationMembership,
    OrganizationOrigin,
    Plan,
    Service,
    ServiceCategory,
    ServiceOffering,
    User,
)


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = (
        "email",
        "first_name",
        "last_name",
        "company",
        "is_staff",
        "is_superuser",
    )
    list_filter = ("is_staff", "is_superuser")
    search_fields = ("email", "first_name", "last_name", "company")
    ordering = ("email",)
    fieldsets = (
        (None, {"fields": ("email",)}),
        (_("Personal info"), {"fields": ("first_name", "last_name", "company")}),
        (_("Permissions"), {"fields": ("is_staff", "is_superuser")}),
    )
    add_fieldsets = (
        (
            None,
            {
                "classes": ("wide",),
                "fields": ("email", "password1", "password2"),
            },
        ),
    )


class OrganizationMembershipInline(admin.TabularInline):
    model = OrganizationMembership
    extra = 1
    autocomplete_fields = ("user",)


@admin.register(Organization)
class OrganizationAdmin(admin.ModelAdmin):
    list_display = ("name", "billing_entity", "origin")
    list_filter = ("origin",)
    search_fields = ("name",)
    autocomplete_fields = ("billing_entity", "origin")
    inlines = (OrganizationMembershipInline,)


@admin.register(BillingEntity)
class BillingEntityAdmin(admin.ModelAdmin):
    list_display = ("name", "erp_reference")
    search_fields = ("name", "erp_reference")


@admin.register(OrganizationOrigin)
class OrganizationOriginAdmin(admin.ModelAdmin):
    list_display = ("name",)
    search_fields = ("name",)


@admin.register(OrganizationMembership)
class OrganizationMembershipAdmin(admin.ModelAdmin):
    list_display = ("user", "organization", "role", "date_joined")
    list_filter = ("role", "date_joined", "organization")
    search_fields = ("user__email", "organization__name")
    autocomplete_fields = ("user", "organization")
    date_hierarchy = "date_joined"


@admin.register(ServiceCategory)
class ServiceCategoryAdmin(admin.ModelAdmin):
    list_display = ("name", "parent")
    list_filter = ("parent",)
    search_fields = ("name", "description")
    autocomplete_fields = ("parent",)


class PlanInline(admin.TabularInline):
    model = Plan
    extra = 1


@admin.register(Service)
class ServiceAdmin(admin.ModelAdmin):
    list_display = ("name", "category")
    list_filter = ("category",)
    search_fields = ("name", "description")
    autocomplete_fields = ("category",)


@admin.register(CloudProvider)
class CloudProviderAdmin(admin.ModelAdmin):
    list_display = ("name",)
    search_fields = ("name", "description")


@admin.register(ControlPlane)
class ControlPlaneAdmin(admin.ModelAdmin):
    list_display = ("name", "cloud_provider", "k8s_api_endpoint")
    list_filter = ("cloud_provider",)
    search_fields = ("name", "description", "k8s_api_endpoint")
    autocomplete_fields = ("cloud_provider",)


@admin.register(Plan)
class PlanAdmin(admin.ModelAdmin):
    list_display = ("name", "service_offering", "term")
    list_filter = ("service_offering", "term")
    search_fields = ("name", "description")
    autocomplete_fields = ("service_offering",)


@admin.register(ServiceOffering)
class ServiceOfferingAdmin(admin.ModelAdmin):
    list_display = ("id", "service", "provider")
    list_filter = ("service", "provider", "control_plane")
    search_fields = ("description",)
    autocomplete_fields = ("service", "provider")
    filter_horizontal = ("control_plane",)
    inlines = (PlanInline,)
