from django.conf import settings
from django.urls import include, path
from django.contrib import admin

from wagtail.admin import urls as wagtailadmin_urls
from wagtail import urls as wagtail_urls
from wagtail.contrib.sitemaps.views import sitemap
from wagtail.documents import urls as wagtaildocs_urls

from search import views as search_views
from home.feeds import LatestArticlesFeed
from home import views as home_views
from base import views as base_views
from mysite import views as mysite_views

urlpatterns = [
    path("django-admin/", admin.site.urls),
    path("admin/", include(wagtailadmin_urls)),
    path("documents/", include(wagtaildocs_urls)),
    path("feed.xml", LatestArticlesFeed(), name="article_feed"),
    path("sitemap.xml", sitemap, name="sitemap"),
    path("media/<path:path>", mysite_views.media_proxy, name="media_proxy"),
    path("health", base_views.health_check, name="health_check"),
    # use <str:slug> so Unicode tag slugs (e.g. 台北美食) still resolve
    path("tags/<str:slug>/", home_views.hashtag_search, name="hashtag_search"),
    path("search/", search_views.search, name="search"),
    path("newsletter/subscribe/", base_views.newsletter_subscribe, name="newsletter_subscribe"),
    path("contact-form/submit/", base_views.contact_form_submit, name="contact_form_submit"),
    path("newsletter/confirm/", base_views.newsletter_confirm, name="newsletter_confirm"),
    path("newsletter/unsubscribe/", base_views.newsletter_unsubscribe, name="newsletter_unsubscribe"),
    path("u/unsubscribe", base_views.one_click_unsubscribe, name="one_click_unsubscribe"),
    path("newsletter/templates/<int:template_id>/payload/", base_views.newsletter_template_payload, name="newsletter_template_payload"),
    path("newsletter/campaigns/preview-compose/", base_views.newsletter_campaign_preview_compose, name="newsletter_campaign_preview_compose"),
    path("newsletter/smtp-test/", base_views.newsletter_smtp_test, name="newsletter_smtp_test"),
    path("newsletter/campaigns/<int:campaign_id>/send-now/", base_views.newsletter_campaign_send_now, name="newsletter_campaign_send_now"),
]

_media_prefix = (settings.MEDIA_URL or "").strip()
if _media_prefix.startswith("/") and _media_prefix.endswith("/"):
    _media_prefix = _media_prefix.strip("/")
    if _media_prefix:
        urlpatterns.insert(
            3,
            path(f"{_media_prefix}/<path:key>", base_views.media_proxy, name="media_proxy"),
        )


if settings.DEBUG:
    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    # Serve static and media files from development server
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns = urlpatterns + [
    # For anything not caught by a more specific rule above, hand over to
    # Wagtail's page serving mechanism. This should be the last pattern in
    # the list:
    path("", include(wagtail_urls)),
    # Alternatively, if you want Wagtail pages to be served from a subpath
    # of your site, rather than the site root:
    #    path("pages/", include(wagtail_urls)),
]
