from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import (
    KnowledgeBaseViewSet,
    PermissionViewSet,
    NotificationViewSet,
    verify_token,
    user_list,
    user_detail,
    user_update,
    user_delete,
    change_password,
    RegisterView,
    LoginView,
    LogoutView,
    ChatHistoryViewSet,
    user_profile,
    setup_gmail_integration,
    send_gmail_message,
    gmail_webhook,
    get_gmail_attachments,
    download_gmail_attachment,
    get_gmail_talents,
    refresh_gmail_watch,
    check_gmail_auth,
    import_gmail_from_sender,
    sync_talent_emails,
    manage_user_goal,
    generate_conversation_summary,
    get_recommended_reply,
    refresh_all_gmail_watches
)
from .feishu_chat_views import (
    process_feishu_table,
    run_auto_chat,
    feishu_user_goal,
    check_goal_status
)
from . import gmail_account_views

# 创建路由器
router = DefaultRouter()

# 注册视图集
router.register(r'knowledge-bases', KnowledgeBaseViewSet, basename='knowledge-bases')
router.register(r'permissions', PermissionViewSet, basename='permission')
router.register(r'notifications', NotificationViewSet, basename='notification')
router.register(r'chat-history', ChatHistoryViewSet, basename='chat-history')

# URL patterns
urlpatterns = [
    # API 路由
    path('', include(router.urls)),
    
    # 用户认证相关
    path('auth/register/', RegisterView.as_view(), name='register'),
    path('auth/login/', LoginView.as_view(), name='login'),
    path('auth/logout/', LogoutView.as_view(), name='logout'),
    path('auth/verify-token/', verify_token, name='verify-token'),
    path('auth/change-password/', change_password, name='change-password'),
    
    # 用户管理相关
    path('users/', user_list, name='user-list'),
    path('users/profile/', user_profile, name='user-profile'),
    path('users/<str:pk>/', user_detail, name='user-detail'),
    path('users/<str:pk>/update/', user_update, name='user-update'),
    path('users/<str:pk>/delete/', user_delete, name='user-delete'),
    
    # Gmail集成API
    path('gmail/setup/', setup_gmail_integration, name='setup_gmail_integration'),
    path('gmail/send/', send_gmail_message, name='send_gmail_message'),
    path('gmail/webhook/', gmail_webhook, name='gmail_webhook'),
    path('gmail/attachments/', get_gmail_attachments, name='get_gmail_attachments'),
    path('gmail/download/', download_gmail_attachment, name='download_gmail_attachment'),
    path('gmail/talents/', get_gmail_talents, name='get_gmail_talents'),
    path('gmail/refresh-watch/', refresh_gmail_watch, name='refresh_gmail_watch'),
    path('gmail/check-auth/', check_gmail_auth, name='check_gmail_auth'),
    path('gmail/import-from-sender/', import_gmail_from_sender, name='import_gmail_from_sender'),
    path('gmail/sync-talent/', sync_talent_emails, name='sync_talent_emails'),
    path('gmail/refresh-all-watches/', refresh_all_gmail_watches, name='refresh_all_gmail_watches'),
    path('gmail/webhook/', gmail_webhook, name='gmail_webhook'),
    # 添加新路由
    path('gmail/clear-cache/', gmail_account_views.clear_gmail_cache, name='clear_gmail_cache'),
    
    # Gmail账户管理
    path('gmail/accounts/', gmail_account_views.list_gmail_accounts, name='list_gmail_accounts'),
    path('gmail/accounts/add/', gmail_account_views.add_gmail_account, name='add_gmail_account'),
    path('gmail/accounts/auth-code/', gmail_account_views.handle_gmail_auth_code, name='handle_gmail_auth_code'),
    path('gmail/accounts/update/', gmail_account_views.update_gmail_account, name='update_gmail_account'),
    path('gmail/accounts/delete/<uuid:account_id>/', gmail_account_views.delete_gmail_account, name='delete_gmail_account'),
    path('gmail/accounts/set-default/', gmail_account_views.set_default_gmail_account, name='set_default_gmail_account'),
    path('gmail/accounts/refresh-watch/', gmail_account_views.refresh_gmail_account_watch, name='refresh_gmail_account_watch'),
    path('gmail/accounts/check-auth/', gmail_account_views.check_specific_gmail_auth, name='check_specific_gmail_auth'),
    
    # 新增功能API
    path('user-goal/', manage_user_goal, name='manage_user_goal'),
    path('conversation-summary/', generate_conversation_summary, name='generate_conversation_summary'),
    path('recommended-reply/', get_recommended_reply, name='get_recommended_reply'),
    
    # 飞书AI聊天相关API
    path('feishu/process-table/', process_feishu_table, name='process_feishu_table'),
    path('feishu/auto-chat/', run_auto_chat, name='run_auto_chat'),
    path('feishu/user-goal/', feishu_user_goal, name='feishu_user_goal'),
    path('feishu/check-goal/', check_goal_status, name='check_goal_status'),
]
