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

from .views import (
    ComplaintAttachmentViewSet,
    ComplaintMeetingViewSet,
    ComplaintPRInteractionViewSet,
    ComplaintViewSet,
    InquiryViewSet,
    complaint_explanation_form,
    champion_start_investigation,
    staff_investigation_form,
    champion_review_answers,
    generate_complaint_pdf,
    api_locations,
    api_sections,
    api_subsections,
    api_departments,
)
from . import ui_views, ui_views_explanation, ui_views_oncall, ui_views_templates

app_name = "complaints"

router = DefaultRouter()
router.register(r"api/complaints", ComplaintViewSet, basename="complaint-api")
router.register(r"api/attachments", ComplaintAttachmentViewSet, basename="complaint-attachment-api")
router.register(r"api/inquiries", InquiryViewSet, basename="inquiry-api")
router.register(r"api/pr-interactions", ComplaintPRInteractionViewSet, basename="pr-interaction-api")
router.register(r"api/meetings", ComplaintMeetingViewSet, basename="complaint-meeting-api")

urlpatterns = [
    # Complaints UI Views
    path("", ui_views.complaint_list, name="complaint_list"),
    path("new/", ui_views.complaint_create, name="complaint_create"),
    path("<uuid:pk>/", ui_views.complaint_detail, name="complaint_detail"),
    path("<uuid:pk>/assign/", ui_views.complaint_assign, name="complaint_assign"),
    path("<uuid:pk>/change-status/", ui_views.complaint_change_status, name="complaint_change_status"),
    path("<uuid:pk>/update-satisfaction/", ui_views.update_satisfaction, name="update_satisfaction"),
    path("<uuid:pk>/update-patient-contact-status/", ui_views.update_patient_contact_status, name="update_patient_contact_status"),
    path("<uuid:pk>/toggle-escalated-ovr/", ui_views.toggle_escalated_ovr, name="toggle_escalated_ovr"),
    path("<uuid:pk>/approve-ovr/", ui_views.approve_ovr_escalation, name="approve_ovr_escalation"),
    path("<uuid:pk>/reject-ovr/", ui_views.reject_ovr_escalation, name="reject_ovr_escalation"),
    path("<uuid:pk>/reopen/", ui_views.complaint_reopen, name="complaint_reopen"),
    path(
        "<uuid:pk>/update-delay-reason-closure/",
        ui_views.update_delay_reason_closure,
        name="update_delay_reason_closure",
    ),
    path(
        "<uuid:pk>/update-explanation-delay-reason/",
        ui_views.update_explanation_delay_reason,
        name="update_explanation_delay_reason",
    ),
    path("<uuid:pk>/change-department/", ui_views.complaint_change_department, name="complaint_change_department"),
    path("<uuid:pk>/add-note/", ui_views.complaint_add_note, name="complaint_add_note"),
    path("<uuid:pk>/escalate/", ui_views.complaint_escalate, name="complaint_escalate"),
    path("<uuid:pk>/activate/", ui_views.complaint_activate, name="complaint_activate"),
    # Export Views
    path("export/csv/", ui_views.complaint_export_csv, name="complaint_export_csv"),
    path("export/excel/", ui_views.complaint_export_excel, name="complaint_export_excel"),
    path(
        "export/historical-excel/",
        ui_views.complaint_export_historical_excel,
        name="complaint_export_historical_excel",
    ),
    path(
        "export/monthly-calculations/",
        ui_views.complaint_export_monthly_calculations,
        name="complaint_export_monthly_calculations",
    ),
    path(
        "export/quarterly-calculations/",
        ui_views.complaint_export_quarterly_calculations,
        name="complaint_export_quarterly_calculations",
    ),
    path(
        "export/yearly-calculations/",
        ui_views.complaint_export_yearly_calculations,
        name="complaint_export_yearly_calculations",
    ),
    # Bulk Actions
    path("bulk/assign/", ui_views.complaint_bulk_assign, name="complaint_bulk_assign"),
    path("bulk/status/", ui_views.complaint_bulk_status, name="complaint_bulk_status"),
    path("bulk/escalate/", ui_views.complaint_bulk_escalate, name="complaint_bulk_escalate"),
    # Soft Delete & Trash
    path("trash/", ui_views.trash_list, name="trash_list"),
    path("<uuid:pk>/delete/", ui_views.complaint_soft_delete, name="complaint_soft_delete"),
    path("<uuid:pk>/restore/", ui_views.complaint_restore, name="complaint_restore"),
    path("inquiries/<uuid:pk>/restore/", ui_views.inquiry_restore, name="inquiry_restore"),
    # Analytics
    path("analytics/", ui_views.complaints_analytics, name="complaints_analytics"),
    # SLA Management (PX Admin only with session-based hospital)
    path("settings/sla-management/", ui_views.sla_management, name="sla_management"),
    path("settings/sla-management/new/", ui_views.sla_management_create, name="sla_management_create"),
    path("settings/sla-management/<uuid:pk>/edit/", ui_views.sla_management_edit, name="sla_management_edit"),
    path("settings/sla-management/<uuid:pk>/toggle/", ui_views.sla_management_toggle, name="sla_management_toggle"),
    # Escalation Rules Management
    path("settings/escalation-rules/", ui_views.escalation_rule_list, name="escalation_rule_list"),
    path("settings/escalation-rules/new/", ui_views.escalation_rule_create, name="escalation_rule_create"),
    path("settings/escalation-rules/<uuid:pk>/edit/", ui_views.escalation_rule_edit, name="escalation_rule_edit"),
    path("settings/escalation-rules/<uuid:pk>/delete/", ui_views.escalation_rule_delete, name="escalation_rule_delete"),
    # Complaint Thresholds Management
    path("settings/thresholds/", ui_views.complaint_threshold_list, name="complaint_threshold_list"),
    path("settings/thresholds/new/", ui_views.complaint_threshold_create, name="complaint_threshold_create"),
    path("settings/thresholds/<uuid:pk>/edit/", ui_views.complaint_threshold_edit, name="complaint_threshold_edit"),
    path(
        "settings/thresholds/<uuid:pk>/delete/", ui_views.complaint_threshold_delete, name="complaint_threshold_delete"
    ),
    # Complaint Templates Management
    path("templates/", ui_views_templates.template_list, name="template_list"),
    path("templates/new/", ui_views_templates.template_create, name="template_create"),
    path("templates/<uuid:pk>/", ui_views_templates.template_detail, name="template_detail"),
    path("templates/<uuid:pk>/edit/", ui_views_templates.template_edit, name="template_edit"),
    path("templates/<uuid:pk>/delete/", ui_views_templates.template_delete, name="template_delete"),
    path("templates/<uuid:pk>/toggle/", ui_views_templates.template_toggle_status, name="template_toggle_status"),
    # AJAX Helpers
    path("ajax/departments/", ui_views.get_departments_by_hospital, name="get_departments_by_hospital"),
    path("ajax/physicians/", ui_views.get_staff_by_department, name="get_physicians_by_department"),
    path("ajax/search-patients/", ui_views.search_patients, name="search_patients"),
    # Public Complaint Form (No Authentication Required)
    path("public/submit/", ui_views.public_complaint_submit, name="public_complaint_submit"),
    path("public/track/", ui_views.public_complaint_track, name="public_complaint_track"),
    path("public/success/<str:reference>/", ui_views.public_complaint_success, name="public_complaint_success"),
    path("public/api/lookup-patient/", ui_views.api_lookup_patient, name="api_lookup_patient"),
    path("public/api/load-departments/", ui_views.api_load_departments, name="api_load_departments"),
    path("public/api/load-categories/", ui_views.api_load_categories, name="api_load_categories"),
    # Location Hierarchy APIs (No Authentication Required)
    path("public/api/locations/", api_locations, name="api_locations"),
    path("public/api/locations/<int:location_id>/sections/", api_sections, name="api_sections"),
    path(
        "public/api/locations/<int:location_id>/sections/<int:section_id>/subsections/",
        api_subsections,
        name="api_subsections",
    ),
    path("public/api/hospitals/<int:hospital_id>/departments/", api_departments, name="api_departments"),
    # Public Explanation Form (No Authentication Required)
    path("<uuid:complaint_id>/explain/<str:token>/", complaint_explanation_form, name="complaint_explanation_form"),
    path("<uuid:complaint_id>/investigate/<str:token>/", champion_start_investigation, name="champion_start_investigation"),
    path("<uuid:complaint_id>/investigate/respond/<str:token>/", staff_investigation_form, name="staff_investigation_form"),
    path("<uuid:complaint_id>/investigate/review/<str:token>/", champion_review_answers, name="champion_review_answers"),
    # Patient Complaint Portal (No Authentication Required)
    path("patient/<str:token>/", ui_views.patient_complaint_portal, name="patient_complaint_portal"),
    path(
        "patient/<str:token>/hospital/<uuid:hospital_id>/",
        ui_views.patient_complaint_hospital_visits,
        name="patient_complaint_hospital_visits",
    ),
    path(
        "patient/<str:token>/visit/<uuid:visit_id>/",
        ui_views.patient_complaint_visit_form,
        name="patient_complaint_visit_form",
    ),
    # Resend Explanation
    path(
        "<uuid:pk>/resend-explanation/",
        ComplaintViewSet.as_view({"post": "resend_explanation"}),
        name="complaint_resend_explanation",
    ),
    # PDF Export
    path("<uuid:pk>/pdf/", generate_complaint_pdf, name="complaint_pdf"),
    # Involved Departments Management
    path("<uuid:complaint_pk>/departments/add/", ui_views.involved_department_add, name="involved_department_add"),
    path(
        "<uuid:complaint_pk>/departments/confirm-ai-suggestion/",
        ui_views.confirm_ai_department_suggestion,
        name="confirm_ai_department_suggestion",
    ),
    path("departments/<uuid:pk>/edit/", ui_views.involved_department_edit, name="involved_department_edit"),
    path("departments/<uuid:pk>/remove/", ui_views.involved_department_remove, name="involved_department_remove"),
    path("departments/<uuid:pk>/response/", ui_views.involved_department_response, name="involved_department_response"),
    path("departments/<uuid:pk>/review-response/", ui_views.involved_department_review_response, name="involved_department_review_response"),
    # Send to Department Form
    path(
        "<uuid:pk>/send-to-department/", ui_views_explanation.send_to_department_form, name="send_to_department_form"
    ),
    # Unified Send To (Person or Department) - AJAX
    path("<uuid:pk>/send-to/", ui_views.complaint_send_to, name="complaint_send_to"),
    # Involved Staff Management
    path("<uuid:complaint_pk>/staff/add/", ui_views.involved_staff_add, name="involved_staff_add"),
    path("staff/<uuid:pk>/edit/", ui_views.involved_staff_edit, name="involved_staff_edit"),
    path("staff/<uuid:pk>/remove/", ui_views.involved_staff_remove, name="involved_staff_remove"),
    path("staff/<uuid:pk>/explanation/", ui_views.involved_staff_explanation, name="involved_staff_explanation"),
    # On-Call Admin Schedule Management
    path("oncall/", ui_views_oncall.oncall_dashboard, name="oncall_dashboard"),
    path("oncall/schedules/", ui_views_oncall.oncall_schedule_list, name="oncall_schedule_list"),
    path("oncall/schedules/new/", ui_views_oncall.oncall_schedule_create, name="oncall_schedule_create"),
    path("oncall/schedules/<uuid:pk>/", ui_views_oncall.oncall_schedule_detail, name="oncall_schedule_detail"),
    path("oncall/schedules/<uuid:pk>/edit/", ui_views_oncall.oncall_schedule_edit, name="oncall_schedule_edit"),
    path("oncall/schedules/<uuid:pk>/delete/", ui_views_oncall.oncall_schedule_delete, name="oncall_schedule_delete"),
    path("oncall/schedules/<uuid:schedule_pk>/admins/add/", ui_views_oncall.oncall_admin_add, name="oncall_admin_add"),
    path("oncall/admins/<uuid:pk>/edit/", ui_views_oncall.oncall_admin_edit, name="oncall_admin_edit"),
    path("oncall/admins/<uuid:pk>/delete/", ui_views_oncall.oncall_admin_delete, name="oncall_admin_delete"),
    # Complaint Adverse Action Management
    path("adverse-actions/", ui_views.adverse_action_list, name="adverse_action_list"),
    path("<uuid:complaint_pk>/adverse-actions/add/", ui_views.adverse_action_add, name="adverse_action_add"),
    path("adverse-actions/<uuid:pk>/edit/", ui_views.adverse_action_edit, name="adverse_action_edit"),
    path(
        "adverse-actions/<uuid:pk>/status/", ui_views.adverse_action_update_status, name="adverse_action_update_status"
    ),
    path("adverse-actions/<uuid:pk>/escalate/", ui_views.adverse_action_escalate, name="adverse_action_escalate"),
    path("adverse-actions/<uuid:pk>/delete/", ui_views.adverse_action_delete, name="adverse_action_delete"),
    # Government Tickets
    path("government-tickets/", ui_views.government_ticket_list, name="government_ticket_list"),
    path("government-tickets/create/", ui_views.government_ticket_create, name="government_ticket_create"),
    path("government-tickets/<uuid:pk>/", ui_views.government_ticket_detail, name="government_ticket_detail"),
    path("government-tickets/<uuid:pk>/edit/", ui_views.government_ticket_update, name="government_ticket_update"),
    path("government-tickets/<uuid:pk>/convert/", ui_views.convert_to_complaint, name="convert_to_complaint"),
    path("government-tickets/import/", ui_views.government_ticket_import, name="government_ticket_import"),
    path("government-tickets/export/", ui_views.government_ticket_export, name="government_ticket_export"),
    # API Routes
    path("", include(router.urls)),
]
