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

from .views import (
    DepartmentViewSet,
    HospitalViewSet,
    LocationViewSet,
    MainSectionViewSet,
    OrganizationViewSet,
    PatientViewSet,
    StaffViewSet,
    SubSectionViewSet,
    api_location_list,
    api_main_section_list,
    api_staff_hierarchy,
    api_staff_hierarchy_children,
    api_subsection_list,
    ajax_departments,
    ajax_main_sections,
    ajax_subsections,
)
from . import ui_views
from .ui_views import (
    department_create,
    department_update,
    department_delete,
    section_list,
    section_create,
    section_update,
    section_delete,
    subsection_list,
    subsection_create,
    subsection_update,
    subsection_delete,
)

app_name = "organizations"

router = DefaultRouter()
router.register(r"organizations", OrganizationViewSet, basename="organization-api")
router.register(r"hospitals", HospitalViewSet, basename="hospital-api")
router.register(r"departments", DepartmentViewSet, basename="department-api")
router.register(r"staff", StaffViewSet, basename="staff-api")
router.register(r"patients", PatientViewSet, basename="patient-api")
router.register(r"locations", LocationViewSet, basename="location-api")
router.register(r"main-sections", MainSectionViewSet, basename="main-section-api")
router.register(r"subsections", SubSectionViewSet, basename="subsection-api")

urlpatterns = [
    # UI Views (come first - more specific routes)
    path("organizations/create/", ui_views.organization_create, name="organization_create"),
    path("organizations/<uuid:pk>/", ui_views.organization_detail, name="organization_detail"),
    path("organizations/", ui_views.organization_list, name="organization_list"),
    path("hospitals/", ui_views.hospital_list, name="hospital_list"),
    path("departments/", ui_views.department_list, name="department_list"),
    path("departments/<uuid:pk>/", ui_views.department_detail, name="department_detail"),
    path("departments/<uuid:pk>/analytics/", ui_views.department_analytics_api, name="department_analytics_api"),
    path("departments/<uuid:pk>/set-respondent/", ui_views.set_department_respondent, name="set_department_respondent"),
    path("departments/<uuid:pk>/set-manager/", ui_views.set_department_manager, name="set_department_manager"),
    path("staff/create/", ui_views.staff_create, name="staff_create"),
    path("staff/import/", ui_views.staff_import, name="staff_import"),
    path("staff/import/sample-csv/", ui_views.staff_import_sample_csv, name="staff_import_sample_csv"),
    path("staff/<uuid:pk>/edit/", ui_views.staff_update, name="staff_update"),
    path("staff/<uuid:pk>/complaints/export/", ui_views.staff_complaints_export, name="staff_complaints_export"),
    path("staff/<uuid:pk>/", ui_views.staff_detail, name="staff_detail"),
    path("staff/hierarchy/d3/", ui_views.staff_hierarchy_d3, name="staff_hierarchy_d3"),
    path("staff/hierarchy/", ui_views.staff_hierarchy, name="staff_hierarchy"),
    path("staff/", ui_views.staff_list, name="staff_list"),
    path("patients/", ui_views.patient_list, name="patient_list"),
    path("patients/search-his/", ui_views.search_his_patient, name="search_his_patient"),
    path("patients/save-his/", ui_views.save_his_patient, name="save_his_patient"),
    path("patients/create/", ui_views.patient_create, name="patient_create"),
    path("patients/<uuid:pk>/", ui_views.patient_detail, name="patient_detail"),
    path(
        "patients/<uuid:patient_pk>/visits/<uuid:visit_pk>/",
        ui_views.patient_visit_journey,
        name="patient_visit_journey",
    ),
    path("patients/<uuid:pk>/send-complaint-link/", ui_views.send_complaint_link, name="send_complaint_link"),
    path("patients/<uuid:pk>/edit/", ui_views.patient_update, name="patient_update"),
    path("patients/<uuid:pk>/delete/", ui_views.patient_delete, name="patient_delete"),
    # Department CRUD
    path("departments/create/", department_create, name="department_create"),
    path("departments/<uuid:pk>/edit/", department_update, name="department_update"),
    path("departments/<uuid:pk>/delete/", department_delete, name="department_delete"),
    # Section CRUD
    path("sections/", section_list, name="section_list"),
    path("sections/create/", section_create, name="section_create"),
    path("sections/<uuid:pk>/edit/", section_update, name="section_update"),
    path("sections/<uuid:pk>/delete/", section_delete, name="section_delete"),
    # Subsection CRUD
    path("subsections/", subsection_list, name="subsection_list"),
    path("subsections/create/", subsection_create, name="subsection_create"),
    path("subsections/<uuid:pk>/edit/", subsection_update, name="subsection_update"),
    path("subsections/<uuid:pk>/delete/", subsection_delete, name="subsection_delete"),
    # API Routes for complaint form dropdowns (public access)
    path("dropdowns/locations/", api_location_list, name="api_location_list"),
    path("dropdowns/main-sections/", api_main_section_list, name="api_main_section_list"),
    path("dropdowns/subsections/", api_subsection_list, name="api_subsection_list"),
    # AJAX Routes for cascading dropdowns in complaint form
    path("ajax/main-sections/", ajax_main_sections, name="ajax_main_sections"),
    path("ajax/subsections/", ajax_subsections, name="ajax_subsections"),
    path("ajax/departments/", ajax_departments, name="ajax_departments"),
    # Staff Hierarchy API (for D3 visualization)
    path("api/staff/hierarchy/", api_staff_hierarchy, name="api_staff_hierarchy"),
    path(
        "api/staff/hierarchy/<uuid:staff_id>/children/",
        api_staff_hierarchy_children,
        name="api_staff_hierarchy_children",
    ),
    # API Routes (must come last - catches anything not matched above)
    path("api/", include(router.urls)),
]
