from django.urls import path, include
from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import redirect

from proprietary.views import BlogSitemap
from .library_views import LibraryAgentLikeAPIView, LibraryAgentsAPIView, LibraryView
from .views import (
    MarkdownPageView,
    DocsIndexRedirectView,
    HomePage,
    HomeAgentSpawnView,
    TermsOfServiceView,
    PrivacyPolicyView,
    health_check,
    AboutView,
    CareersView,
    HomepageIntegrationsSearchView,
    StartupCheckoutView,
    StaticViewSitemap,
    PretrainedWorkerTemplateSitemap,
    LandingRedirectView,
    LandingLaunchView,
    ClearSignupTrackingView,
    PretrainedWorkerDirectoryRedirectView,
    PretrainedWorkerDetailView,
    PretrainedWorkerHireView,
    PublicTemplateSitemap,
    PublicTemplateCategorySitemap,
    PublicTemplateDetailView,
    PublicTemplateLegacyDetailRedirectView,
    PublicTemplateHireView,
    EngineeringProSignupView,
    SolutionView,
    MarketingContactRequestView,
    SolutionsSitemap,
    SpecialAccessStartView,
    SpecialAccessView,
    WebManifestView,
    InstallScriptView,
)

from djstripe import views as djstripe_views
from django.contrib.sitemaps.views import sitemap
from django.views.generic.base import RedirectView, TemplateView

app_name = "pages"
EXTERNAL_DOCS_URL = "https://docs.gobii.ai/"

_docs_index_view = DocsIndexRedirectView.as_view()
_markdown_page_view = MarkdownPageView.as_view()


def docs_index_view(request, *args, **kwargs):
    if settings.GOBII_PROPRIETARY_MODE:
        return redirect(EXTERNAL_DOCS_URL, permanent=True)
    return _docs_index_view(request, *args, **kwargs)


def markdown_page_view(request, *args, **kwargs):
    if settings.GOBII_PROPRIETARY_MODE:
        return redirect(EXTERNAL_DOCS_URL, permanent=True)
    return _markdown_page_view(request, *args, **kwargs)

sitemaps = {
    'static': StaticViewSitemap,
}

if settings.GOBII_PROPRIETARY_MODE:
    sitemaps['blog'] = BlogSitemap

sitemaps['pretrained_workers'] = PretrainedWorkerTemplateSitemap
sitemaps['public_template_categories'] = PublicTemplateCategorySitemap
sitemaps['public_templates'] = PublicTemplateSitemap
sitemaps['solutions'] = SolutionsSitemap

urlpatterns = [
    path("", HomePage.as_view(), name="home"),
    path("install.sh", InstallScriptView.as_view(), name="install_script"),
    path("manifest.json", WebManifestView.as_view(), name="web_manifest"),
    path("libary/", RedirectView.as_view(pattern_name="pages:library", permanent=True)),
    path("library/", LibraryView.as_view(), name="library"),
    path("library/<slug:category_slug>/", LibraryView.as_view(), name="library_category"),
    path("library/<slug:category_slug>/<slug:template_slug>/", PublicTemplateDetailView.as_view(), name="public_template_detail"),
    path("library/<slug:category_slug>/<slug:template_slug>/hire/", PublicTemplateHireView.as_view(), name="public_template_hire"),
    path("api/library/agents/", LibraryAgentsAPIView.as_view(), name="library_agents_api"),
    path("api/library/agents/like/", LibraryAgentLikeAPIView.as_view(), name="library_agent_like_api"),
    path("api/homepage/integrations/search/", HomepageIntegrationsSearchView.as_view(), name="homepage_integrations_search"),
    path("spawn-agent/", HomeAgentSpawnView.as_view(), name="home_agent_spawn"),
    path("pretrained-workers/", PretrainedWorkerDirectoryRedirectView.as_view(), name="pretrained_worker_directory"),
    path("pretrained-workers/<slug:slug>/", PretrainedWorkerDetailView.as_view(), name="pretrained_worker_detail"),
    path("pretrained-workers/<slug:slug>/hire/", PretrainedWorkerHireView.as_view(), name="pretrained_worker_hire"),
    path("solutions/engineering/pro-signup/", EngineeringProSignupView.as_view(), name="engineering_pro_signup"),
    path("special-access/", SpecialAccessView.as_view(), name="special_access"),
    path("special-access/start/", SpecialAccessStartView.as_view(), name="special_access_start"),
    path("contact/request/", MarketingContactRequestView.as_view(), name="marketing_contact_request"),
    path("health/", health_check, name="health_check"),
    # Kubernetes health check endpoint - matches /healthz/ in BackendConfig
    path("healthz/", health_check, name="health_check_k8s"),

    # Documentation URLs
    path("docs/", docs_index_view, name="docs_index"),
    path("docs/<path:slug>/", markdown_page_view, name="markdown_page"),

    # Short landing page redirects
    path("g/<slug:code>/spawn/", LandingLaunchView.as_view(), name="landing_launch"),
    path("g/<slug:code>/", LandingRedirectView.as_view(), name="landing_redirect"),

    # Solutions
    path("solutions/<slug:slug>/", SolutionView.as_view(), name="solution"),

    # Stripe webhooks
    path("stripe/", include("djstripe.urls", namespace="djstripe")),
    path("stripe/webhook/", djstripe_views.ProcessWebhookView.as_view(), name="stripe-webhook"),

    # Add sitemap URL pattern
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),

    # Make robots.txt available through Django
    path('robots.txt', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),

    path('clear_signup_tracking', ClearSignupTrackingView.as_view(), name='clear_signup_tracking'),

    path('<slug:handle>/<slug:template_slug>/', PublicTemplateLegacyDetailRedirectView.as_view(), name='public_template_legacy_detail'),
    path('<slug:handle>/<slug:template_slug>/hire/', PublicTemplateHireView.as_view(), name='public_template_legacy_hire'),

]

# Security.txt for vulnerability disclosure (RFC 9116) - proprietary mode only
if settings.GOBII_PROPRIETARY_MODE:
    urlpatterns.append(
        path('.well-known/security.txt', lambda r: HttpResponse(
            f"Contact: mailto:{settings.SECURITY_TXT_EMAIL}\nExpires: {settings.SECURITY_TXT_EXPIRY}\n",
            content_type='text/plain',
        ))
    )
