from django.contrib import admin

from webpages.models import Domain, HistoricalWebPage, WebPage

from scrobbles.admin import ScrobbleInline


class WebPageInline(admin.TabularInline):
    model = WebPage
    extra = 0

    exclude = ("extract",)


@admin.register(Domain)
class DomainAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    ordering = ("root",)

    inlines = [WebPageInline]


class HistoricalWebPageInline(admin.TabularInline):
    model = HistoricalWebPage
    extra = 0
    readonly_fields = ("date", "domain", "extract", "created")


@admin.register(WebPage)
class WebPageAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    list_display = (
        "uuid",
        "title",
        "url",
    )
    raw_id_fields = ("domain",)
    ordering = ("-created",)
    search_fields = ("title",)
    inlines = [
        ScrobbleInline,
        HistoricalWebPageInline,
    ]


@admin.register(HistoricalWebPage)
class HistoricalWebPageAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    list_display = (
        "uuid",
        "webpage",
        "date",
        "domain",
        "created",
    )
    raw_id_fields = ("webpage", "domain")
    ordering = ("-created",)
    search_fields = ("webpage__title",)
