from django.contrib import admin

from codelist.models import KonovaCode, KonovaCodeList


class KonovaCodeListAdmin(admin.ModelAdmin):
    list_display = [
        "id",
    ]
    readonly_fields = [
        "id",
        "codes",
    ]


class KonovaCodeAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "atom_id",
        "parent",
        "short_name",
        "long_name",
        "is_leaf",
        "is_selectable",
        "is_archived",
    ]

    readonly_fields = [
        "id",
        "short_name",
        "long_name",
        "is_archived",
        "is_selectable",
        "is_leaf",
        "parent",
        "found_in_codelists",
    ]

    search_fields = [
        "id",
        "atom_id",
        "long_name",
        "short_name",
    ]

    def found_in_codelists(self, obj):
        codelists = KonovaCodeList.objects.filter(
            codes__in=[obj]
        ).values_list("id", flat=True)
        codelists = "\n".join(str(x) for x in codelists)
        return codelists

#admin.site.register(KonovaCodeList, KonovaCodeListAdmin)
admin.site.register(KonovaCode, KonovaCodeAdmin)
