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

from apps.standards.views import (
    StandardSourceViewSet,
    StandardCategoryViewSet,
    StandardViewSet,
    StandardComplianceViewSet,
    StandardAttachmentViewSet,
    standards_dashboard,
    department_standards_view,
    standard_detail,
    standard_create,
    standard_update,
    standard_delete,
    standard_compliance_update,
    standard_attachment_upload,
    standard_attachment_delete,
    standards_search,
    get_compliance_status,
    create_compliance_ajax,
    update_compliance_ajax,
    get_attachments_ajax,
    upload_attachment_ajax,
    delete_attachment_ajax,
    source_list,
    source_create,
    source_update,
    source_delete,
    category_list,
    category_create,
    category_update,
    category_delete,
    activity_type_list,
    activity_type_create,
    activity_type_update,
    activity_type_delete,
)

# API Router
router = DefaultRouter()
router.register(r"sources", StandardSourceViewSet, basename="standard-source")
router.register(r"categories", StandardCategoryViewSet, basename="standard-category")
router.register(r"standards", StandardViewSet, basename="standard")
router.register(r"compliance", StandardComplianceViewSet, basename="standard-compliance")
router.register(r"attachments", StandardAttachmentViewSet, basename="standard-attachment")

app_name = "standards"

urlpatterns = [
    # Custom AJAX endpoints (MUST be before router to take precedence)
    path("api/compliance/create/", create_compliance_ajax, name="compliance_create_ajax"),
    path("api/compliance/update/", update_compliance_ajax, name="compliance_update_ajax"),
    path("api/compliance/<uuid:department_id>/<uuid:standard_id>/", get_compliance_status, name="compliance_status"),
    # Attachment AJAX endpoints
    path("api/attachments/list/<uuid:compliance_id>/", get_attachments_ajax, name="attachments_list_ajax"),
    path("api/attachments/upload/", upload_attachment_ajax, name="attachment_upload_ajax"),
    path("api/attachments/<uuid:attachment_id>/delete/", delete_attachment_ajax, name="attachment_delete_ajax"),
    # API endpoints (router)
    path("api/", include(router.urls)),
    # UI Views
    path("", standards_dashboard, name="dashboard"),
    path("search/", standards_search, name="search"),
    path("departments/<uuid:pk>/", department_standards_view, name="department_standards"),
    path("departments/<uuid:department_id>/create-standard/", standard_create, name="standard_create"),
    path("standards/create/", standard_create, name="standard_create_global"),
    path("standards/<uuid:pk>/update/", standard_update, name="standard_update"),
    path("standards/<uuid:pk>/delete/", standard_delete, name="standard_delete"),
    path("standards/<uuid:pk>/", standard_detail, name="standard_detail"),
    path("compliance/<uuid:compliance_id>/update/", standard_compliance_update, name="standard_compliance_update"),
    path("attachments/upload/<uuid:compliance_id>/", standard_attachment_upload, name="attachment_upload"),
    path("attachments/<uuid:pk>/delete/", standard_attachment_delete, name="attachment_delete"),
    # Source Management
    path("sources/", source_list, name="source_list"),
    path("sources/create/", source_create, name="source_create"),
    path("sources/<uuid:pk>/update/", source_update, name="source_update"),
    path("sources/<uuid:pk>/delete/", source_delete, name="source_delete"),
    # Category Management
    path("categories/", category_list, name="category_list"),
    path("categories/create/", category_create, name="category_create"),
    path("categories/<uuid:pk>/update/", category_update, name="category_update"),
    path("categories/<uuid:pk>/delete/", category_delete, name="category_delete"),
    # Activity Type Management
    path("activity-types/", activity_type_list, name="activity_type_list"),
    path("activity-types/create/", activity_type_create, name="activity_type_create"),
    path("activity-types/<uuid:pk>/update/", activity_type_update, name="activity_type_update"),
    path("activity-types/<uuid:pk>/delete/", activity_type_delete, name="activity_type_delete"),
]
