from django.urls import path, include
from django.views.generic import RedirectView
from django.contrib.auth import views as auth_views
from . import views
from .views import (
    DashboardView, CustomerListView, CustomerDetailView, CustomerCreateView,
    CustomerUpdateView, CustomerDeleteView, ServiceListView, ServiceCreateView,
    ServiceUpdateView, ServiceDeleteView, CustomerTagListView, CustomerTagCreateView,
    CustomerTagUpdateView, CustomerTagDeleteView, send_customer_invitation,
    customer_account_setup, manage_customer_balance, CustomerLoginView,
    DashboardMetricsAPIView, HeartbeatAPIView
)
from . import marketing_views
from .marketing_views import MarketingDashboardView, marketing_quick_add_customer

app_name = 'customers'

urlpatterns = [
    # Main dashboard (no namespace)
    path('', DashboardView.as_view(), name='dashboard'),
    path('list/', CustomerListView.as_view(), name='customer_list'),
    path('add/', CustomerCreateView.as_view(), name='customer_add'),
    path('create/', CustomerCreateView.as_view(), name='customer_create'),  # Alias for compatibility

    # Service (Package) management - Admin only - Must come before customer_id patterns
    path('services/', ServiceListView.as_view(), name='service_list'),
    path('packages/', ServiceListView.as_view(), name='package_list'),  # Alias for compatibility
    path('services/add/', ServiceCreateView.as_view(), name='service_add'),
    path('services/<int:pk>/edit/', ServiceUpdateView.as_view(), name='service_edit'),
    path('services/<int:pk>/delete/', ServiceDeleteView.as_view(), name='service_delete'),

    # Backward compatibility for package URLs
    path('packages/', views.ServiceListView.as_view(), name='package_list'),
    path('packages/add/', views.ServiceCreateView.as_view(), name='package_add'),
    path('packages/<int:pk>/edit/', views.ServiceUpdateView.as_view(), name='package_edit'),
    path('packages/<int:pk>/delete/', views.ServiceDeleteView.as_view(), name='package_delete'),

    # Customer Tag Management - Must come before customer_id patterns
    path('tags/', CustomerTagListView.as_view(), name='tag_list'),
    path('tags/add/', CustomerTagCreateView.as_view(), name='tag_create'),
    path('tags/<int:pk>/edit/', CustomerTagUpdateView.as_view(), name='tag_edit'),
    path('tags/<int:pk>/delete/', CustomerTagDeleteView.as_view(), name='tag_delete'),

    # Customer Login and Account Setup - Must come before customer_id patterns
    path('login/', CustomerLoginView.as_view(), name='customer_login'),
    path('logout/', auth_views.LogoutView.as_view(), name='customer_logout'),

    # Dashboard - Must come before customer_id patterns
    path('dashboard/', DashboardView.as_view(), name='dashboard'),

    # Dashboard API - Must come before customer_id patterns
    path('api/dashboard-metrics/', DashboardMetricsAPIView.as_view(), name='dashboard_metrics_api'),
    path('api/heartbeat/', HeartbeatAPIView.as_view(), name='heartbeat_api'),


    # Marketing Reports URLs
    path('marketing/reports/', marketing_views.marketing_report_list, name='marketing_report_list'),
    path('marketing/reports/create/', marketing_views.marketing_report_create, name='marketing_report_create'),
    path('marketing/reports/<int:pk>/', marketing_views.marketing_report_detail, name='marketing_report_detail'),
    path('marketing/reports/<int:pk>/edit/', marketing_views.marketing_report_edit, name='marketing_report_edit'),
    path('marketing/reports/<int:pk>/submit/', marketing_views.marketing_report_submit, name='marketing_report_submit'),
    path('marketing/reports/<int:pk>/review/', marketing_views.marketing_report_review, name='marketing_report_review'),
    path('marketing/analytics/', RedirectView.as_view(pattern_name='customers:marketing_dashboard', permanent=True), name='marketing_analytics'),


    # Customer impersonation
    path('stop-impersonation/', views.stop_customer_impersonation, name='stop_impersonation'),

    # Customer account setup - Must come before customer_id patterns
    path('setup/<uuid:token>/', views.customer_account_setup, name='customer_account_setup'),

    # Marketing dashboard and quick actions
    path('marketing/dashboard/', MarketingDashboardView.as_view(), name='marketing_dashboard'),
    path('marketing/quick-add/', marketing_quick_add_customer, name='marketing_quick_add_customer'),
    path('marketing/tickets/', marketing_views.marketing_tickets_view, name='marketing_tickets'),

    # Import/Export and SMS features
    path('customers/export/', views.export_customers, name='export_customers'),
    path('customers/import/', views.import_customers, name='import_customers'),
    path('customers/template/', views.download_template, name='download_template'),
    path('customers/send-sms/', views.send_single_sms, name='send_single_sms'),

    # Customer-specific URLs with customer_id - Must come after all other specific patterns
    path('<str:customer_id>/', CustomerDetailView.as_view(), name='customer_detail'),
    path('<str:customer_id>/edit/', CustomerUpdateView.as_view(), name='customer_edit'),
    path('<str:customer_id>/delete/', CustomerDeleteView.as_view(), name='customer_delete'),
    path('<str:customer_id>/view-as-customer/', views.view_as_customer, name='view_as_customer'),
    path('<int:customer_id>/send-invitation/', views.send_customer_invitation, name='send_customer_invitation'),
    path('manage-balance/<str:customer_id>/', views.manage_customer_balance, name='manage_balance'),
    path('manage-service/<str:customer_id>/', views.manage_customer_service, name='manage_service'),
    path('create-sub-account/<str:customer_id>/', views.create_sub_account, name='create_sub_account'),
    path('action-log/<str:customer_id>/', views.customer_action_log, name='action_log'),
    path('api/service-status/<str:customer_id>/', views.customer_service_status_api, name='service_status_api'),
]