from django.contrib import admin

from .models import Article, ArticleCitation, ArticleSection, MainProduct, SubProduct, SubProductVersion


class ArticleCitationInline(admin.TabularInline):
    model = ArticleCitation
    extra = 1
    fields = ("text", "url", "order")
    ordering = ("order",)


class ArticleSectionInline(admin.TabularInline):
    model = ArticleSection
    extra = 1
    fields = ("title", "value_format", "value", "order")
    ordering = ("order",)


class ArticleInline(admin.StackedInline):
    model = Article
    extra = 0
    fields = ("badge", "title", "description", "order")
    ordering = ("order",)
    show_change_link = True


class SubProductVersionInline(admin.TabularInline):
    model = SubProductVersion
    extra = 0
    ordering = ("order", "version")
    fields = (
        "version",
        "is_featured_stable",
        "windows_download_url",
        "macos_download_url",
        "linux_download_url",
        "source_code_url",
        "package_resource_url",
        "release_notes",
        "is_active",
        "order",
    )


class SubProductInline(admin.StackedInline):
    model = SubProduct
    extra = 0
    fields = ("name", "slug", "distribution", "short_description", "image", "logo", "show_on_homepage", "homepage_order", "order", "is_active")
    ordering = ("order",)
    show_change_link = True
    prepopulated_fields = {"slug": ("name",)}


@admin.register(MainProduct)
class MainProductAdmin(admin.ModelAdmin):
    list_display = ("name", "order", "is_active", "created_at")
    list_filter = ("is_active",)
    search_fields = ("name", "description")
    prepopulated_fields = {"slug": ("name",)}
    list_editable = ("order", "is_active")
    inlines = [SubProductInline]
    fieldsets = (
        (None, {"fields": ("name", "slug", "short_description")}),
        ("Description", {"fields": ("description_format", "description")}),
        ("Media", {"fields": ("image",)}),
        ("Settings", {"fields": ("order", "is_active")}),
    )


@admin.register(SubProduct)
class SubProductAdmin(admin.ModelAdmin):
    list_display = (
        "name",
        "main_product",
        "distribution",
        "show_on_homepage",
        "homepage_order",
        "order",
        "is_active",
        "created_at",
    )
    list_filter = ("is_active", "distribution", "main_product", "show_on_homepage")
    search_fields = ("name", "description", "main_product__name")
    prepopulated_fields = {"slug": ("name",)}
    list_editable = ("order", "is_active", "show_on_homepage", "homepage_order")
    raw_id_fields = ("main_product",)
    inlines = [ArticleInline, SubProductVersionInline]
    fieldsets = (
        (None, {"fields": ("main_product", "name", "slug", "distribution", "short_description")}),
        ("Description", {"fields": ("description_format", "description")}),
        ("Media", {"fields": ("image", "logo")}),
        ("Homepage", {"fields": ("show_on_homepage", "homepage_order")}),
        ("Settings", {"fields": ("order", "is_active")}),
    )


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ("title", "sub_product", "order", "created_at")
    list_filter = ("sub_product__main_product",)
    search_fields = ("title", "description")
    list_editable = ("order",)
    raw_id_fields = ("sub_product",)
    inlines = [ArticleSectionInline, ArticleCitationInline]
    fieldsets = (
        (None, {"fields": ("sub_product", "badge", "title")}),
        ("Description", {"fields": ("description_format", "description")}),
        ("Citation Badge", {"fields": ("citation_count_display",), "description": "Set a number to show the dashed citation circle badge at the bottom-right of the article card. Leave blank to hide it."}),
        ("Settings", {"fields": ("order",)}),
    )


@admin.register(ArticleSection)
class ArticleSectionAdmin(admin.ModelAdmin):
    list_display = ("title", "article", "value_format", "order")
    search_fields = ("title", "value", "article__title")
    list_editable = ("order",)
    raw_id_fields = ("article",)
    fieldsets = (
        (None, {"fields": ("article", "title")}),
        ("Content", {"fields": ("value_format", "value")}),
        ("Settings", {"fields": ("order",)}),
    )


@admin.register(SubProductVersion)
class SubProductVersionAdmin(admin.ModelAdmin):
    list_display = (
        "sub_product",
        "version",
        "is_featured_stable",
        "is_active",
        "order",
    )
    list_filter = ("is_active", "is_featured_stable", "sub_product__distribution")
    search_fields = ("sub_product__name", "version")
    list_editable = ("is_active", "order")
    raw_id_fields = ("sub_product",)
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "sub_product",
                    "version",
                    "is_featured_stable",
                )
            },
        ),
        (
            "Installable downloads",
            {
                "fields": (
                    "windows_download_url",
                    "macos_download_url",
                    "linux_download_url",
                    "source_code_url",
                )
            },
        ),
        ("Package link", {"fields": ("package_resource_url",)}),
        ("Details", {"fields": ("release_notes",)}),
        ("Settings", {"fields": ("is_active", "order")}),
    )
