from django.utils.translation import gettext_lazy as _
from wagtail.admin.ui.tables import DateColumn
from wagtail.admin.ui.tables.pages import PageStatusColumn, PageTitleColumn
from wagtail.admin.viewsets.pages import PageListingViewSet, PageViewSet

from dnscms.admin import ListingRedirectChooseParentView
from news.models import NewsPage


class NewsChooseParentView(ListingRedirectChooseParentView):
    listing_url_name = "news:index"


class NewsListingMixin:
    """Shared model + columns for the standalone listing and the page explorer."""

    model = NewsPage
    icon = "info-circle"
    columns = [
        PageTitleColumn("title", label=_("Title"), sort_key="title", classname="title"),
        DateColumn(
            "first_published_at",
            label=_("First published"),
            sort_key="first_published_at",
            width="10%",
        ),
        DateColumn(
            "latest_revision_created_at",
            label=_("Updated"),
            sort_key="latest_revision_created_at",
            width="10%",
        ),
        PageStatusColumn("status", label=_("Status"), sort_key="live", width="10%"),
    ]


class NewsSidebarViewSet(NewsListingMixin, PageListingViewSet):
    """Standalone 'News' sidebar entry, reached independently of the page tree."""

    choose_parent_view_class = NewsChooseParentView
    menu_label = _("News")
    menu_order = 3
    add_to_admin_menu = True
    ordering = "-latest_revision_created_at"


class NewsExplorerViewSet(NewsListingMixin, PageViewSet):
    """Applies the same columns when navigating into NewsIndex via the page explorer."""


news_sidebar_viewset = NewsSidebarViewSet("news")
news_explorer_viewset = NewsExplorerViewSet()
