from django.contrib import admin
from django.utils.html import format_html
from .models import (
    Category,
    CloudProvider,
    ConsultingPartner,
    ExternalLink,
    ExternalLinkOffering,
    Lead,
    Plan,
    ReusableText,
    Service,
    ServiceOffering,
)


@admin.register(ReusableText)
class ReusableTextAdmin(admin.ModelAdmin):
    list_display = ("name",)
    search_fields = ("name", "text")
    ordering = ("name",)


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "parent", "order")
    list_filter = ("parent",)
    search_fields = ("name", "description")
    prepopulated_fields = {"slug": ("name",)}
    ordering = ("order", "name")


@admin.register(CloudProvider)
class CloudProviderAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "logo_preview")
    search_fields = ("name", "description")
    prepopulated_fields = {"slug": ("name",)}

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

    logo_preview.short_description = "Logo"


class ExternalLinkInline(admin.TabularInline):
    model = ExternalLink
    extra = 1
    fields = ("description", "url", "order")
    ordering = ("order", "description")


@admin.register(Service)
class ServiceAdmin(admin.ModelAdmin):
    list_display = ("name", "logo_preview", "category_list", "partner_list")
    list_filter = ("categories",)
    search_fields = ("name", "description", "slug")
    prepopulated_fields = {"slug": ("name",)}
    filter_horizontal = ("categories",)
    inlines = [ExternalLinkInline]

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

    logo_preview.short_description = "Logo"

    def category_list(self, obj):
        return ", ".join([cat.name for cat in obj.categories.all()])

    category_list.short_description = "Categories"

    def partner_list(self, obj):
        return ", ".join([partner.name for partner in obj.consulting_partners.all()])

    partner_list.short_description = "Consulting Partners"


class PlanInline(admin.StackedInline):
    model = Plan
    extra = 1
    fieldsets = (
        (None, {"fields": ("name", "description", "pricing", "plan_description")}),
    )


class ExternalLinkOfferingInline(admin.TabularInline):
    model = ExternalLinkOffering
    extra = 1
    fields = ("description", "url", "order")
    ordering = ("order", "description")


@admin.register(ServiceOffering)
class ServiceOfferingAdmin(admin.ModelAdmin):
    list_display = ("service", "cloud_provider")
    list_filter = ("service", "cloud_provider")
    search_fields = ("service__name", "cloud_provider__name", "description")
    inlines = [ExternalLinkOfferingInline, PlanInline]


@admin.register(ConsultingPartner)
class ConsultingPartnerAdmin(admin.ModelAdmin):
    list_display = ("name", "website", "logo_preview")
    search_fields = ("name", "description")
    prepopulated_fields = {"slug": ("name",)}
    filter_horizontal = ("services", "cloud_providers")

    def logo_preview(self, obj):
        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(Lead)
class LeadAdmin(admin.ModelAdmin):
    list_display = ("name", "company", "created_at", "odoo_lead_id")
    search_fields = ("name", "company")
