from django.urls import include, path
from rest_framework.routers import DefaultRouter

from .views import (
    PublicSurveyViewSet,
    SurveyInstanceViewSet,
    SurveyQuestionViewSet,
    SurveyResponseViewSet,
    SurveyTemplateViewSet,
)
from .analytics_views import SurveyAnalyticsViewSet, SurveyTrackingViewSet
from . import public_views, ui_views, his_views

app_name = "surveys"

router = DefaultRouter()
router.register(r"api/templates", SurveyTemplateViewSet, basename="survey-template-api")
router.register(r"api/questions", SurveyQuestionViewSet, basename="survey-question-api")
router.register(r"api/instances", SurveyInstanceViewSet, basename="survey-instance-api")
router.register(r"api/responses", SurveyResponseViewSet, basename="survey-response-api")
router.register(r"api/analytics", SurveyAnalyticsViewSet, basename="survey-analytics-api")
router.register(r"api/tracking", SurveyTrackingViewSet, basename="survey-tracking-api")

urlpatterns = [
    # Public survey pages (no auth required)
    path("invalid/", public_views.invalid_token, name="invalid_token"),
    # UI Views (authenticated) - specific paths first
    path("send/", ui_views.manual_survey_send, name="manual_send"),
    path("send/phone/", ui_views.manual_survey_send_phone, name="manual_send_phone"),
    path("send/csv/", ui_views.manual_survey_send_csv, name="manual_send_csv"),
    # HIS Patient Import
    path("his-import/", his_views.his_patient_import, name="his_patient_import"),
    path("his-import/review/", his_views.his_patient_review, name="his_patient_review"),
    path("his-import/send/", his_views.his_patient_survey_send, name="his_patient_survey_send"),
    # Bulk Survey Jobs
    path("bulk-jobs/", his_views.bulk_job_list, name="bulk_job_list"),
    path("bulk-jobs/<uuid:job_id>/", his_views.bulk_job_status, name="bulk_job_status"),
    path("reports/", ui_views.survey_analytics_reports, name="analytics_reports"),
    path(
        "reports/<str:filename>/view/",
        ui_views.survey_analytics_report_view_inline,
        name="analytics_report_view_inline",
    ),
    path(
        "reports/<str:filename>/download/", ui_views.survey_analytics_report_download, name="analytics_report_download"
    ),
    path("reports/<str:filename>/delete/", ui_views.survey_analytics_report_delete, name="analytics_report_delete"),
    path("reports/<str:filename>/", ui_views.survey_analytics_report_view, name="analytics_report_view"),
    # Analytics Dashboard
    path("analytics/", ui_views.survey_analytics_dashboard, name="analytics_dashboard"),
    # Enhanced Reports (separate report per survey type)
    path("enhanced-reports/", ui_views.enhanced_survey_reports_list, name="enhanced_reports_list"),
    path("enhanced-reports/generate/", ui_views.generate_enhanced_report_ui, name="generate_enhanced_report"),
    path("enhanced-reports/<str:dir_name>/", ui_views.enhanced_survey_report_view, name="enhanced_report_view"),
    path(
        "enhanced-reports/<str:dir_name>/<str:filename>",
        ui_views.enhanced_survey_report_file,
        name="enhanced_report_file",
    ),
    path("comments/", ui_views.survey_comments_list, name="survey_comments_list"),
    path("instances/", ui_views.survey_instance_list, name="instance_list"),
    path("instances/<uuid:pk>/", ui_views.survey_instance_detail, name="instance_detail"),
    path("instances/<uuid:pk>/log-contact/", ui_views.survey_log_patient_contact, name="log_patient_contact"),
    path(
        "instances/<uuid:pk>/send-satisfaction/",
        ui_views.survey_send_satisfaction_feedback,
        name="send_satisfaction_feedback",
    ),
    # Survey Templates (manual/ad-hoc surveys)
    path("templates/", ui_views.survey_template_list, name="template_list"),
    path("templates/create/", ui_views.survey_template_create, name="template_create"),
    path("templates/<uuid:pk>/", ui_views.survey_template_detail, name="template_detail"),
    path("templates/<uuid:pk>/edit/", ui_views.survey_template_edit, name="template_edit"),
    path("templates/<uuid:pk>/delete/", ui_views.survey_template_delete, name="template_delete"),
    # Public API endpoints (no auth required)
    path("public/<str:token>/", PublicSurveyViewSet.as_view({"get": "retrieve"}), name="public-survey"),
    path("public/<str:token>/submit/", PublicSurveyViewSet.as_view({"post": "submit"}), name="public-survey-submit"),
    # Authenticated API endpoints
    path("", include(router.urls)),
    # Public survey token access (requires /s/ prefix)
    path("s/<str:token>/", public_views.survey_form, name="survey_form"),
    path("s/<str:token>/answer/", public_views.submit_answer, name="submit_answer"),
    path("s/<str:token>/thank-you/", public_views.thank_you, name="thank_you"),
    path("s/<str:token>/track-start/", public_views.track_survey_start, name="track_survey_start"),
path("templates/<uuid:pk>/reorder/", ui_views.template_question_reorder, name="template_question_reorder"),
]
