"""
URL configuration for qy_lty project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
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 django.views.generic import RedirectView
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from rest_framework import permissions
import logging
from common.views import upload_file

logger = logging.getLogger(__name__)
logger.info('start at fengye urls')

# API 信息配置
api_info = openapi.Info(
    title="气元科技 API",
    default_version='v1',
    description="API documentation for 气元科技",
    terms_of_service="https://www.google.com/policies/terms/",
    contact=openapi.Contact(email="contact@qiyuankeji.com"),
    license=openapi.License(name="BSD License"),
)

schema_view = get_schema_view(
    api_info,
    public=True,
    permission_classes=(permissions.AllowAny,),
    url='http://localhost:8000' if settings.DEBUG else 'https://your-production-domain.com'
)

# API URLs
api_urlpatterns = [
    path('user/', include('userapp.urls')),
    path('ai/', include('aiapp.urls')),
    # path('ali/vi/api/', include('ali_vi_app.urls')),
    path('device/', include('device_interaction.urls')),
    path('card/', include('card.urls')),
    path('achievement/', include('achievement_app.urls')),  # 成就系统API
    path('food/', include('food_app.urls')),  # 食物管理API
    path('common/upload/', upload_file, name='file-upload'),
    # 管理员API接口路径（v1版本）
    path('v1/admin/', include('userapp.admin_urls')),
]

# 基本 URL 配置
urlpatterns = [
    path('', RedirectView.as_view(url='/admin/', permanent=False), name='index'),
    path('admin/', admin.site.urls),  # Django 原生 admin URLs
    path('api/', include(api_urlpatterns)),  # 所有 API 路由
    
    
    # API 文档
    path('swagger<format>/', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]

# 开发环境配置
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    
    import debug_toolbar
    urlpatterns += [
        path('__debug__/', include(debug_toolbar.urls)),
    ]
