from books.models import Author, Book, Journal, Paper
from django.contrib import admin
from scrobbles.admin import ScrobbleInline


@admin.register(Journal)
class JournalAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    list_display = (
        "title",
        "website_url",
    )
    search_fields = ("title",)
    ordering = ("-created",)


@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    list_display = (
        "name",
        "openlibrary_id",
        "bio",
        "wikipedia_url",
    )
    ordering = ("-created",)
    search_fields = ("name",)


@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    list_display = (
        "title",
        "author",
        "issue_or_volume",
        "isbn_13",
        "first_publish_year",
        "pages",
    )
    raw_id_fields = ("authors",)
    search_fields = ("name",)
    ordering = ("-created",)
    inlines = [
        ScrobbleInline,
    ]

    def issue_or_volume(self, obj):
        return obj.subtitle


@admin.register(Paper)
class PaperAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    list_display = (
        "title",
        "subtitle",
        "arxiv_id",
        "first_publish_year",
        "pages",
    )
    raw_id_fields = ("authors",)
    search_fields = ("name",)
    ordering = ("-created",)
    inlines = [
        ScrobbleInline,
    ]
