from django.contrib import admin
from django.conf import settings
from django.utils.html import format_html
from .models import Identity, ServiceType, Frontend

@admin.register(ServiceType)
class ServiceTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'description')  # Display fields in the list view
    search_fields = ('name',)  # Add search functionality for the name field

    def has_add_permission(self, request):
        """Allow adding only if no Identity instance exists."""
        return not ServiceType.objects.exists()


@admin.register(Identity)
class IdentityAdmin(admin.ModelAdmin):
    list_display = ('name', 'service_type', 'enable_admin', 'description', 'service_name_display', 'service_root_display')  # Show in list view
    readonly_fields = ('service_name_display', 'service_root_display')  # Make these fields uneditable in form view

    def has_add_permission(self, request):
        """Allow adding only if no Identity instance exists."""
        return not Identity.objects.exists()

    def service_name_display(self, obj):
        """Display SERVICE_NAME as a read-only field."""
        return format_html("<strong>{}</strong>", settings.SERVICE_NAME)
    service_name_display.short_description = "Service Name"

    def service_root_display(self, obj):
        """Display SERVICE_ROOT as a read-only field."""
        return format_html("<strong>{}</strong>", settings.SERVICE_ROOT)
    service_root_display.short_description = "Service Root"


@admin.register(Frontend)
class FrontendAdmin(admin.ModelAdmin):
    list_display = (
       'account_type', 'flow_title', 'flow_description', 'switch_description', 'sign_in_description',
        'register_description', 'forgot_description', 'reset_description', 'authenticated_route'
    )  # Display all fields in the list view
    search_fields = ('flow_title',)

    def has_add_permission(self, request):
        """Allow adding only if no Frontend instance exists."""
        return not Frontend.objects.exists()