from django.contrib import admin
from django.urls import path, include, re_path
from django.http import HttpResponsePermanentRedirect
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
from cms.admin import geo_admin_site
from cms.seo_views import llms_txt_view, markdown_mirror_view, robots_txt_view, sitemap_xml_view
from cms.views import (
    canada_main_view, canada_city_view,
    uae_main_view, uae_city_view,
    ireland_main_view, ireland_city_view
)


def uk_redirect_view(request, path=''):
    """Permanently redirect /uk/ URLs to the default (UK) root URLs."""
    target_path = f'/{path}' if path else '/'
    query_string = request.META.get('QUERY_STRING', '')
    if query_string:
        target_path = f"{target_path}?{query_string}"
    return HttpResponsePermanentRedirect(target_path)

urlpatterns = [
    path('admin/', geo_admin_site.urls),
    path('favicon.ico', RedirectView.as_view(url='/static/favicon.png', permanent=True)),

    # --- Well-known root files: must come BEFORE any catch-all includes ---
    path('robots.txt', robots_txt_view, name='robots_txt'),
    path('llms.txt', llms_txt_view, name='llms_txt'),
    path('sitemap.xml', sitemap_xml_view, name='sitemap_xml'),

    # Markdown mirror: matches any path ending in .md (e.g. /index.md, /services.md)
    re_path(r'^(?P<mirror_path>(?:.*?/)?index|.*)\.md$', markdown_mirror_view, name='markdown_mirror'),

    # UK legacy path redirect - default UK is root
    re_path(r'^uk/(?P<path>.*)$', uk_redirect_view),

    # Region-specific paths - these must come BEFORE the default path
    path('ca/', include([
        path('', canada_main_view, name='canada_main'),
        path('city/<slug:city_slug>/', canada_city_view, name='canada_city'),
        path('', include('cms.urls')),  # Include all other cms URLs under /ca/
    ])),

    path('uae/', include([
        path('', uae_main_view, name='uae_main'),
        path('city/<slug:city_slug>/', uae_city_view, name='uae_city'),
        path('', include('cms.urls')),  # Include all other cms URLs under /uae/
    ])),

    path('ireland/', include([
        path('', ireland_main_view, name='ireland_main'),
        path('city/<slug:city_slug>/', ireland_city_view, name='ireland_city'),
        path('', include('cms.urls')),  # Include all other cms URLs under /ireland/
    ])),

    # Default path (no region prefix) - includes all other URLs
    path('', include('cms.urls')),
]

# Serve media and static files in development
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
elif 'runserver' in __import__('sys').argv:
    # Running via `manage.py runserver` locally with DEBUG=False: Django must
    # serve /media/ and /static/ itself because there is no web server / Apache
    # rewrite in local dev. Without this every media image 404s locally (only
    # the production server serves /media/ via .htaccess).
    from django.views.static import serve as static_serve
    urlpatterns += [
        path(f'{settings.MEDIA_URL.lstrip("/")}<path:path>', static_serve,
             {'document_root': settings.MEDIA_ROOT}),
        path(f'{settings.STATIC_URL.lstrip("/")}<path:path>', static_serve,
             {'document_root': settings.STATIC_ROOT}),
    ]
