"""
URL configuration for aibimagics project.
"""
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from frontend import wishlist_views
from django.conf.urls import handler403, handler404, handler500
from django.contrib.sitemaps.views import sitemap as sitemap_view
from frontend.sitemaps import ProductSitemap, CategorySitemap, StaticViewSitemap
from django.http import HttpResponse
from django.views.static import serve as serve_static

# API Documentation
schema_view = get_schema_view(
   openapi.Info(
      title="AiBiMagics E-commerce API",
      default_version='v1',
      description="Comprehensive E-commerce Platform with AI Integration",
      terms_of_service="https://www.aibimagics.com/terms/",
      contact=openapi.Contact(email="contact@aibimagics.com"),
      license=openapi.License(name="BSD License"),
   ),
   public=False,
   permission_classes=(permissions.IsAdminUser,),
)

urlpatterns = [
    # Admin
    path('admin/', admin.site.urls),
    
    # Vendor Dashboard
    path('vendor/', include('vendors.urls', namespace='vendor')),
    
    # API Documentation
    path('api/docs/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('api/redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    
    # Authentication
    path('api/auth/', include('accounts.urls')),
    path('accounts/', include('allauth.urls')),  # Allauth social auth URLs
    
    # Core Apps
    path('api/products/', include('products.urls')),
    path('api/cart/', include('cart.urls')),
    path('api/orders/', include(('orders.urls', 'orders'), namespace='orders_api')),
    path('api/payments/', include('payments.urls')),
    path('api/inventory/', include('inventory.urls')),
    
    # AI Features
    path('api/ai/', include('ai_features.urls')),
    
    # Analytics
    path('api/analytics/', include('analytics.urls')),
    
    # Wishlist API
    path('api/wishlist/toggle/<int:product_id>/', wishlist_views.toggle_wishlist, name='wishlist_toggle'),
    path('api/wishlist/count/', wishlist_views.get_wishlist_count, name='wishlist_count'),
    path('api/wishlist/items/', wishlist_views.get_wishlist_items, name='wishlist_items'),
    path('api/wishlist/remove/<int:product_id>/', wishlist_views.remove_from_wishlist, name='wishlist_remove'),
    path('api/wishlist/clear/', wishlist_views.clear_wishlist, name='wishlist_clear'),
    
    # Notifications
    path('api/notifications/', include('notifications.urls')),
    
    # SEO: Sitemaps and robots
    path('sitemap.xml', sitemap_view, {'sitemaps': {
        'products': ProductSitemap(),
        'categories': CategorySitemap(),
        'static': StaticViewSitemap(),
    }}, name='django_sitemap'),
    path('robots.txt', lambda request: HttpResponse(
        f"User-agent: *\nAllow: /\nSitemap: {request.scheme}://{request.get_host}/sitemap.xml\n",
        content_type="text/plain"
    )),
    
    # Staff Panel (Separate from Admin)
    path('staff/', include('frontend.staff_urls')),
    
    # Orders (Frontend)
    path('orders/', include('orders.urls')),
    
    # Frontend
    path('', include('frontend.urls')),
]

# Serve media 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)

# Ensure media files are served in production environments where the web server is not configured
urlpatterns += [
    re_path(r'^media/(?P<path>.*)$', serve_static, {'document_root': settings.MEDIA_ROOT}),
]

# Customize admin site
admin.site.site_header = "AiBiMagics E-commerce Administration"
admin.site.site_title = "AiBiMagics Admin Portal"
admin.site.index_title = "Welcome to AiBiMagics E-commerce Management"

# Error handlers
handler404 = 'frontend.error_views.custom_404'
handler403 = 'frontend.error_views.custom_403'
handler500 = 'frontend.error_views.custom_500'
