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

from .views import (
    DepartmentViewSet,
    HospitalViewSet,
    LegacyLocationViewSet,
    LegacyMainSectionViewSet,
    LegacySubSectionViewSet,
    OrgSubSectionViewSet,
    OrganizationViewSet,
    PatientViewSet,
    StaffViewSet,
    api_areas_by_hospital,
    api_department_contacts,
    api_departments_by_category,
    api_main_section_list,
    api_sections_by_department,
    api_subsections_by_section,
    api_staff_hierarchy,
    api_staff_hierarchy_children,
    api_staff_by_department,
    api_subsection_list,
    ajax_departments,
)
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,
    orgsection_list,
    orgsection_create,
    orgsection_detail,
    orgsection_update,
    orgsection_delete,
    orgsubsection_list,
    orgsubsection_create,
    orgsubsection_update,
    orgsubsection_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", LegacyLocationViewSet, basename="location-api")
router.register(r"main-sections", LegacyMainSectionViewSet, basename="main-section-api")
router.register(r"subsections", LegacySubSectionViewSet, basename="subsection-api")
router.register(r"org-sections", OrgSubSectionViewSet, basename="org-section-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>/complaints/", ui_views.department_complaints_list, name="department_complaints_list"),
    path("departments/<uuid:pk>/complaints/<uuid:cpk>/", ui_views.department_complaint_detail, name="department_complaint_detail"),
    path("departments/<uuid:pk>/manager-review/<uuid:idept_pk>/", ui_views.department_manager_review, name="department_manager_review"),
    path("manager-review-questions/", ui_views.manager_review_questions, name="manager_review_questions"),
    path("manager-review-questions/create/", ui_views.manager_review_question_create, name="manager_review_question_create"),
    path("manager-review-questions/<uuid:pk>/edit/", ui_views.manager_review_question_update, name="manager_review_question_update"),
    path("manager-review-questions/<uuid:pk>/delete/", ui_views.manager_review_question_delete, name="manager_review_question_delete"),
    path("departments/<uuid:pk>/inquiries/", ui_views.department_inquiries_list, name="department_inquiries_list"),
    path("departments/<uuid:pk>/inquiries/<uuid:ipk>/", ui_views.department_inquiry_detail, name="department_inquiry_detail"),
    path("departments/<uuid:pk>/observations/", ui_views.department_observations_list, name="department_observations_list"),
    path("departments/<uuid:pk>/observations/<uuid:opk>/", ui_views.department_observation_detail, name="department_observation_detail"),
    path("departments/<uuid:pk>/staff/<uuid:spk>/edit/", ui_views.department_staff_update, name="department_staff_update"),
    path("departments/<uuid:pk>/staff/<uuid:spk>/", ui_views.department_staff_detail, name="department_staff_detail"),
    path("api/dept-complaint/<uuid:pk>/", ui_views.department_record_complaint, name="department_record_complaint"),
    path("api/dept-inquiry/<uuid:pk>/", ui_views.department_record_inquiry, name="department_record_inquiry"),
    path("api/dept-observation/<uuid:pk>/", ui_views.department_record_observation, name="department_record_observation"),
    path("api/dept-suggestion/<uuid:pk>/", ui_views.department_record_suggestion, name="department_record_suggestion"),
    path("api/dept-appreciation/<uuid:pk>/", ui_views.department_record_appreciation, name="department_record_appreciation"),
    path("departments/<uuid:pk>/set-role/", ui_views.set_department_role, name="set_department_role"),
    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"),
    # Org Section CRUD
    path("org-sections/", orgsection_list, name="orgsection_list"),
    path("org-sections/create/", orgsection_create, name="orgsection_create"),
    path("org-sections/<uuid:pk>/", orgsection_detail, name="orgsection_detail"),
    path("org-sections/<uuid:pk>/edit/", orgsection_update, name="orgsection_update"),
    path("org-sections/<uuid:pk>/delete/", orgsection_delete, name="orgsection_delete"),
    # Org Sub-Section CRUD
    path("org-sub-sections/", orgsubsection_list, name="orgsubsection_list"),
    path("org-sub-sections/create/", orgsubsection_create, name="orgsubsection_create"),
    path("org-sub-sections/<uuid:pk>/edit/", orgsubsection_update, name="orgsubsection_update"),
    path("org-sub-sections/<uuid:pk>/delete/", orgsubsection_delete, name="orgsubsection_delete"),
    # API Routes for complaint form dropdowns (public access)
    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/departments/", ajax_departments, name="ajax_departments"),
    # New hierarchy API endpoints
    path("dropdowns/departments-by-category/", api_departments_by_category, name="api_departments_by_category"),
    path("dropdowns/sections/<uuid:department_id>/", api_sections_by_department, name="api_sections_by_department"),
    path("dropdowns/subsections/<uuid:section_id>/", api_subsections_by_section, name="api_subsections_by_section"),
    path("dropdowns/areas/", api_areas_by_hospital, name="api_areas_by_hospital"),
    path("dropdowns/staff-by-department/<uuid:department_id>/", api_staff_by_department, name="api_staff_by_department"),
    path("dropdowns/department-contacts/<uuid:department_id>/", api_department_contacts, name="api_department_contacts"),
    # 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)),
]
