"""
URL patterns for system module.
"""
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import AIPromptViewSet, AuthorProfileViewSet, StrategyViewSet, system_status, get_request_metrics, gitea_webhook
from .integration_views import IntegrationSettingsViewSet
from .settings_views import (
    SystemSettingsViewSet, AccountSettingsViewSet, UserSettingsViewSet,
    ModuleSettingsViewSet, AISettingsViewSet
)
router = DefaultRouter()
router.register(r'prompts', AIPromptViewSet, basename='prompts')
router.register(r'author-profiles', AuthorProfileViewSet, basename='author-profile')
router.register(r'strategies', StrategyViewSet, basename='strategy')
router.register(r'settings/system', SystemSettingsViewSet, basename='system-settings')
router.register(r'settings/account', AccountSettingsViewSet, basename='account-settings')
router.register(r'settings/user', UserSettingsViewSet, basename='user-settings')
router.register(r'settings/modules', ModuleSettingsViewSet, basename='module-settings')
router.register(r'settings/ai', AISettingsViewSet, basename='ai-settings')

# Custom URL patterns for integration settings - matching reference plugin structure
# Reference: WordPress uses settings_fields('igny8_api_settings') -> options.php which calls update_option()
# We use REST endpoints: /api/v1/system/settings/integrations/{type}/save/
# Use as_view with proper method mapping

integration_detail_viewset = IntegrationSettingsViewSet.as_view({
    'get': 'retrieve',
})

integration_save_viewset = IntegrationSettingsViewSet.as_view({
    'post': 'save_post',
    'put': 'update',
})

integration_test_viewset = IntegrationSettingsViewSet.as_view({
    'post': 'test_connection',
})

integration_generate_viewset = IntegrationSettingsViewSet.as_view({
    'post': 'generate_image',
})

integration_task_progress_viewset = IntegrationSettingsViewSet.as_view({
    'get': 'task_progress',
})

integration_image_gen_settings_viewset = IntegrationSettingsViewSet.as_view({
    'get': 'get_image_generation_settings',
})

urlpatterns = [
    path('', include(router.urls)),
    # System status endpoint
    path('status/', system_status, name='system-status'),
    # Request metrics endpoint
    path('request-metrics/<str:request_id>/', get_request_metrics, name='request-metrics'),
    # Gitea webhook endpoint
    path('webhook/', gitea_webhook, name='gitea-webhook'),
    # Integration settings routes - exact match to reference plugin workflow
    # IMPORTANT: More specific paths must come BEFORE less specific ones
    # GET: Image generation settings - MUST come before other settings paths
    path('integrations/image_generation/', integration_image_gen_settings_viewset, name='integration-image-gen-settings'),
    # GET: Task progress - MUST come before other settings paths
    path('settings/task_progress/<str:task_id>/', integration_task_progress_viewset, name='integration-task-progress'),
    # POST: Generate image (for image_generation integration) - MUST come before base path
    path('settings/integrations/<str:pk>/generate/', integration_generate_viewset, name='integration-settings-generate'),
    # POST: Test connection - MUST come before base path
    path('settings/integrations/<str:pk>/test/', integration_test_viewset, name='integration-settings-test'),
    # POST/PUT: Save settings (matches frontend call to /save/) - MUST come before base path
    path('settings/integrations/<str:pk>/save/', integration_save_viewset, name='integration-settings-save'),
    # GET: Retrieve settings - Base path comes last
    path('settings/integrations/<str:pk>/', integration_detail_viewset, name='integration-settings-detail'),
]

