"""
Admin classes for cloud providers and consulting partners
"""

from django.contrib import admin
from django.utils.html import format_html
from adminsortable2.admin import SortableAdminMixin

from ..models import CloudProvider, ConsultingPartner, ServiceOffering


class OfferingInline(admin.StackedInline):
    """Inline admin for ServiceOffering model"""

    model = ServiceOffering
    extra = 1
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "description",
                    "service",
                    "cloud_provider",
                    "offer_description",
                )
            },
        ),
    )
    show_change_link = True


@admin.register(CloudProvider)
class CloudProviderAdmin(SortableAdminMixin, admin.ModelAdmin):
    """Admin configuration for CloudProvider model"""

    list_display = (
        "name",
        "slug",
        "logo_preview",
        "disable_listing",
        "is_featured",
        "order",
    )
    search_fields = ("name", "description")
    prepopulated_fields = {"slug": ("name",)}
    inlines = [OfferingInline]
    ordering = ("order",)

    def logo_preview(self, obj):
        """Display logo preview in admin list view"""
        if obj.logo:
            return format_html(
                '<img src="{}" style="max-height: 50px;"/>', obj.logo.url
            )
        return "No logo"

    logo_preview.short_description = "Logo"


@admin.register(ConsultingPartner)
class ConsultingPartnerAdmin(SortableAdminMixin, admin.ModelAdmin):
    """Admin configuration for ConsultingPartner model"""

    list_display = (
        "name",
        "website",
        "logo_preview",
        "disable_listing",
        "is_featured",
        "order",
    )
    search_fields = ("name", "description")
    prepopulated_fields = {"slug": ("name",)}
    filter_horizontal = ("services", "cloud_providers")
    ordering = ("order",)

    def logo_preview(self, obj):
        """Display logo preview in admin list view"""
        if obj.logo:
            return format_html(
                '<img src="{}" style="max-height: 50px;"/>', obj.logo.url
            )
        return "No logo"

    logo_preview.short_description = "Logo"
