from django.urls import path, include

from generic.models import Examination, Supervisor
from . import views
from generic.views import ExamViews as GenericExamViews, GenericViewBase


app_name = "generic"

urlpatterns = [
    # User content review views
    path("user-content/", views.user_content_list, name="user_content_list"),
    path("user-content/<int:user_id>/", views.user_content_review, name="user_content_review"),
    path("toggle-hide-item/", views.toggle_hide_item, name="toggle_hide_item"),
    path("examination/", views.ExaminationView.as_view(), name="examination_view"),
    path("examination/<int:pk>", views.examination_detail, name="examination_detail"),
    path(
        "examination/<int:pk>/delete",
        views.ExaminationDelete.as_view(),
        name="examination_delete",
    ),
    path(
        "examination/<int:pk>/update",
        views.ExaminationUpdate.as_view(),
        name="examination_update",
    ),
    path(
        "examination/<int:pk>/merge", views.examination_merge, name="examination_merge"
    ),
    path("examination/create/", views.create_examination, name="create_examination"),
    path(
        "examination/ajax/get_examination_id",
        views.get_examination_id,
        name="get_examination_id",
    ),
    path(
        "generic_exam_json_edit",
        views.generic_exam_json_edit,
        name="generic_exam_json_edit",
    ),
    path(
        "generic_exam_htmx",
        views.generic_exam_htmx,
        name="generic_exam_htmx",
    ),
    path(
        "generic_exam_set_open_access",
        views.generic_exam_set_open_access,
        name="generic_exam_set_open_access",
    ),
    path("bulk_delete_questions/", views.bulk_delete_questions, name="bulk_delete_questions"),
    path(
        "examination-autocomplete",
        views.ExaminationAutocomplete.as_view(
            model=Examination, create_field="examination"
        ),
        name="examination-autocomplete",
    ),
    path(
        "supervisor-autocomplete",
        views.SupervisorAutocomplete.as_view(model=Supervisor),
        name="supervisor-autocomplete",
    ),
    path("user-search/", views.user_search, name="user_search"),
    path("user-search-widget/", views.user_search_widget, name="user_search_widget"),
    path("exam-search-widget/", views.exam_search_widget, name="exam_search_widget"),
    path("supervisor-search/", views.supervisor_search, name="supervisor_search"),
    path(
        "cids/manage/<int:pk>/update", views.CidUserUpdate.as_view(), name="update_cid"
    ),
    path(
        "cids/manage/<int:pk>/update/exams",
        views.CidUserExamUpdate.as_view(),
        name="update_cid_exams",
    ),
    path(
        "cids/manage/<int:cid>/email_results",
        views.candidate_email_results,
        name="candidate_email_results",
    ),
    path(
        "cids/manage/<int:cid>/email_details",
        views.candidate_email_details,
        name="candidate_email_details",
    ),
    path("cids/manage/", views.CidUserView.as_view(), name="manage_cids"),
    path("cids/manage/import-answers/", views.import_exam_answers, name="import_exam_answers"),
    path("answer-history/<str:app_name>/<str:model_name>/<int:pk>/", views.view_answer_history, name="view_answer_history"),
    path("cids/manage/exams", views.CidUserExamView.as_view(), name="manage_cid_exams"),
    path("cids/group/", views.cid_group_view, name="cid_group_view"),
    path("cids/group/all", views.cid_group_view_all, name="cid_group_view_all"),
    path(
        "cids/group/<int:group_id>/",
        views.cid_group_view_detail,
        name="cid_group_detail",
    ),
    path(
        "cids/group/<int:group_id>/generate_preview",
        views.cid_group_generate_preview,
        name="cid_group_generate_preview",
    ),
    path(
        "cids/group/<int:group_id>/create_bulk",
        views.cid_group_create_bulk,
        name="cid_group_create_bulk",
    ),
    path("cids/group/<int:pk>/email", views.group_email, name="group_email"),
    path(
        "cids/group/<int:pk>/exams",
        views.CidGroupExamUpdate.as_view(),
        name="cid_group_exams",
    ),
    path(
        "cids/group/<int:pk>/email_results",
        views.group_email_results,
        name="group_email_results",
    ),
    path(
        "cids/group/<int:pk>/email_results/resend",
        views.group_email_results_resend,
        name="group_email_results_resend",
    ),
    path(
        "cids/group/<int:pk>/email/resend",
        views.group_email_resend,
        name="group_email_resend",
    ),
    path(
        "cids/group/<int:pk>/update",
        views.CidUserGroupUpdate.as_view(),
        name="cid_group_update",
    ),

    # HTMX partial to show a CidUserExam record for an exam (used from exam lists)
    path(
        "exam/<str:app_name>/<int:exam_id>/cid/<int:cid>/ciduserexam",
        views.cid_user_exam_partial,
        name="cid_user_exam_partial",
    ),
    path(
        "cids/ciduserexam/<int:pk>/update",
        views.CidUserExamRecordUpdate.as_view(),
        name="update_cid_user_exam",
    ),
    path(
        "cids/group/<int:pk>/delete",
        views.CidUserGroupDelete.as_view(),
        name="cid_group_delete",
    ),
    path(
        "cid/group/<int:group_id>/add",
        views.cid_group_add_candidates_to_exams,
        name="cid_group_add_candidates_to_exams",
    ),
    path(
        "cids/group/create", views.CidUserGroupCreate.as_view(), name="cid_group_create"
    ),
    path(
        "user/bulk_delete_supervisors/",
        views.users_bulk_delete_supervisors,
        name="users_bulk_delete_supervisors",
    ),
    path(
        "user/bulk_edit/",
        views.users_bulk_edit,
        name="users_bulk_edit",
    ),
    path(
        "user/<int:user_id>/not_trainee",
        views.user_not_trainee,
        name="user_not_trainee",
    ),
    path("user/group/", views.user_group_view, name="user_group_view"),
    path("user/group/all", views.user_group_view_all, name="user_group_view_all"),
    path(
        "user/group/<int:group_id>/",
        views.user_group_view_detail,
        name="user_group_detail",
    ),
    path(
        "user/group/<int:group_id>/add_by_email",
        views.user_group_add_by_email_user,
        name="user_group_add_by_email_user",
    ),
    path(
        "user/group/<int:group_id>/add",
        views.user_group_add_candidates_to_exams,
        name="user_group_add_candidates_to_exams",
    ),
    path(
        "user/group/<int:group_id>/",
        views.user_group_view_detail,
        name="user_group_detail",
    ),
    path(
        "user/group/<int:pk>/exams",
        views.UserGroupExamUpdate.as_view(),
        name="user_group_exams",
    ),
    path(
        "user/group/<int:pk>/update",
        views.UserUserGroupUpdate.as_view(),
        name="user_group_update",
    ),
    path(
        "user/group/<int:pk>/delete",
        views.UserUserGroupDelete.as_view(),
        name="user_group_delete",
    ),
    path(
        "user/group/<int:pk>/archive",
        views.user_group_toggle_archive,
        name="user_group_toggle_archive",
    ),
    path(
        "user/group/create",
        views.UserUserGroupCreate.as_view(),
        name="user_group_create",
    ),
    path(
        "user/<int:user_id>/toggle_group/<int:group_id>/",
        views.user_toggle_group,
        name="user_toggle_group",
    ),
    path("cids/create", views.manage_cid_users, name="manage_cid_users"),
    path("cids/<int:cid>/details", views.cid_details, name="cid_details"),
    path("cids/create/email", views.create_cid_email, name="create_cid_email"),
    path("supervisor", views.SupervisorList.as_view(), name="supervisor"),
    path(
        "supervisor/request_account",
        views.supervisor_request_account,
        name="supervisor_request_account",
    ),
    path(
        "supervisor/<int:pk>/overview",
        views.supervisor_overview,
        name="supervisor_overview",
    ),
    path(
        "supervisor/<int:pk>/trainee/<int:trainee_id>/",
        views.supervisor_trainee,
        name="supervisor_trainee",
    ),
    path(
        "supervisor/<int:pk>/trainee/<int:trainee_id>/collection/<int:collection_id>/",
        views.supervisor_collection_feedback,
        name="supervisor_collection_feedback",
    ),
    path(
        "supervisor/<int:pk>",
        views.SupervisorDetail.as_view(),
        name="supervisor_detail",
    ),
    path(
        "supervisor/<int:pk>/edit",
        views.SupervisorUpdate.as_view(),
        name="supervisor_edit",
    ),
    path(
        "supervisor/create", views.SupervisorCreate.as_view(), name="supervisor_create"
    ),
    path(
        "supervisor/<int:pk>/delete",
        views.SupervisorDelete.as_view(),
        name="supervisor_delete",
    ),
    path(
        "supervisor/<int:pk>/trainee/search",
        views.supervisor_trainee_search,
        name="supervisor_trainee_search",
    ),
    path(
        "supervisor/<int:pk>/trainee/<int:user_id>/add",
        views.supervisor_trainee_add,
        name="supervisor_trainee_add",
    ),
    path(
        "supervisor/<int:pk>/trainee/<int:user_id>/remove",
        views.supervisor_trainee_remove,
        name="supervisor_trainee_remove",
    ),
    path(
        "examcollection/",
        views.ExamCollectionList.as_view(),
        name="examcollection_list",
    ),
    path(
        "examcollection/<int:pk>/",
        views.ExamCollectionDetail.as_view(),
        name="examcollection_detail",
    ),
    path(
        "examcollection/<int:pk>/edit",
        views.ExamCollectionEdit.as_view(),
        name="examcollection_edit",
    ),
    path(
        "examcollection/create",
        views.ExamCollectionCreate.as_view(),
        name="examcollection_create",
    ),
    path(
        "examcollection/<int:pk>/clone",
        views.ExamCollectionClone.as_view(),
        name="examcollection_clone",
    ),
    path(
        "examcollection/<int:pk>/delete",
        views.ExamCollectionDelete.as_view(),
        name="examcollection_delete",
    ),
    path("collection/<int:collection_id>/add_author", views.exam_collection_add_author, name="exam_collection_add_author"),
    path("collection/<int:collection_id>/bulk_add_groups", views.exam_collection_bulk_add_groups, name="exam_collection_bulk_add_groups"),
    path("collection/<int:collection_id>/add_marker/<str:exam_type>", views.exam_collection_add_marker, name="exam_collection_add_marker"),
    path("collection/<int:collection_id>/exams/<str:exam_type>/partial", views.exam_collection_exams_partial, name="exam_collection_exams_partial"),
    path(
        "share_with_supervisor_toggle/<int:pk>/",
        views.toggle_share_with_supervisor,
        name="toggle_share_with_supervisor",
    ),
    path(
        "cimar_case/<str:uuid>/refresh",
        views.cimar_case_refresh,
        name="cimar_case_refresh"
    ),


]


def generic_view_urls(generic_views: GenericViewBase):
    urlpatterns = [
        path(
            "user_answers/delete",
            generic_views.user_answer_delete_multiple,
            name="user_answer_delete_multiple",
        ),
        path("author/<int:pk>/", generic_views.author_detail, name="author_detail"),
        path("author/", generic_views.author_list, name="author_list"),
        path(
            "question/<int:pk>/", generic_views.question_detail, name="question_detail"
        ),
        path(
            "question/<int:pk>/set_review",
            generic_views.question_set_review,
            name="question_set_review",
        ),
        path(
            "question/<int:pk>/reviews",
            generic_views.question_reviews_list,
            name="question_reviews",
        ),
        path(
            "question/review/start",
            generic_views.question_review_start,
            name="question_review_start",
        ),
        path(
            "question/review/next",
            generic_views.question_review_next,
            name="question_review_next",
        ),
        path(
            "question/review/list",
            generic_views.question_review_list,
            name="question_review_list",
        ),
        path("question/<int:pk>/thumbnail/fail", generic_views.question_thumbnail_fail, name="series_thumbnail_fail"),
        path("question/<int:pk>/thumbnail", generic_views.question_thumbnail, name="series_thumbnail"),
        path(
            "question/<int:pk>/user_answers",
            generic_views.question_user_answers,
            name="question_user_answers",
        ),
        path(
            "question/<int:pk>/user_answers/<str:answer_compare>/str",
            generic_views.question_user_answers_by_compare,
            name="question_user_answers_by_compare",
        ),
        path(
            "exam/<int:pk>/review",
            generic_views.question_review,
            name="question_review",
        ),
        path("help", generic_views.help, name="help"),
    ]

    return urlpatterns


def generic_exam_urls(generic_exam_view: GenericExamViews):
    urlpatterns = [
        path(
            "exam/bulk_update/",
            generic_exam_view.exam_bulk_update,
            name="exam_bulk_update",
        ),
        path(
            "exam/bulk_archive/",
            generic_exam_view.exam_bulk_archive,
            name="exam_bulk_archive",
        ),
        path(
            "exam/bulk_delete/",
            generic_exam_view.exam_bulk_delete,
            name="exam_bulk_delete",
        ),
        path("", generic_exam_view.index, name="index"),
        path("exam/<int:pk>/", generic_exam_view.exam_overview, name="exam_overview"),
        path(
            "exam/<int:pk>/mark", generic_exam_view.mark_overview, name="mark_overview"
        ),
        path(
            "exam/<int:pk>/question/<int:sk>/",
            generic_exam_view.exam_question_detail,
            name="exam_question_detail",
        ),
        path(
            "exam/<int:pk>/review/start",
            generic_exam_view.exam_review_start,
            name="exam_review_start",
        ),
        path(
            "exam/<int:pk>/review/<int:q_index>",
            generic_exam_view.exam_review_question,
            name="exam_review_question",
        ),
        # path(
        #    "exam/<int:pk>/question/<int:sk>/answer/<str:user_or_cid>",
        #    generic_exam_view.exam_question_user_answer,
        #    name="exam_question_user_answer",
        # ),
        path(
            "exam/<int:pk>/question/<int:sk>/answer/<str:c_or_u>/<str:user_or_cid>",
            generic_exam_view.exam_question_user_answer,
            name="exam_question_user_answer",
        ),
        path(
            "exam/<int:exam_id>/user/<int:user_id>",
            generic_exam_view.exam_user,
            name="exam_user",
        ),
        path(
            "exam/<int:exam_id>/report/email",
            generic_exam_view.exam_report_email,
            name="exam_report_email",
        ),
        path(
            "exam/<int:exam_id>/report/email_unsent",
            generic_exam_view.exam_report_email_unsent,
            name="exam_report_email_unsent",
        ),
        path(
            "exam/<int:exam_id>/report/email/status",
            generic_exam_view.exam_report_email_status,
            name="exam_report_email_status",
        ),
        path(
            "exam/<int:exam_id>/report/<int:user_id>",
            generic_exam_view.exam_user_report,
            name="exam_user_report",
        ),
        path(
            "exam/<int:exam_id>/report/<int:user_id>/email",
            generic_exam_view.exam_user_report_email,
            name="exam_user_report_email",
        ),
        path(
            "exam/<int:pk>/json_edit",
            generic_exam_view.exam_json_edit,
            name="exam_json_edit",
        ),
        path(
            "exam/<int:pk>/cid_bulk_edit",
            generic_exam_view.exam_cid_bulk_edit,
            name="exam_cid_bulk_edit",
        ),
        path(
            "exam/<int:pk>/user_bulk_edit",
            generic_exam_view.exam_user_bulk_edit,
            name="exam_user_bulk_edit",
        ),
        path(
            "exam/<int:pk>/scores/all",
            generic_exam_view.exam_scores_all,
            name="exam_scores_all",
        ),
        path(
            "exam/<int:pk>/update_pass_mark",
            generic_exam_view.update_pass_mark,
            name="update_pass_mark",
        ),
        path(
            "exam/<int:pk>/toggle_candidate_stats_exclusion",
            generic_exam_view.toggle_candidate_stats_exclusion,
            name="toggle_candidate_stats_exclusion",
        ),
        path(
            "exam/<int:exam_id>/stats",
            generic_exam_view.exam_stats,
            name="exam_stats",
        ),

        path(
            "exam/<int:pk>/scores/<int:cid>/<str:passcode>/",
            generic_exam_view.exam_scores_cid_user,
            name="exam_scores_cid_user",
        ),
        path(
            "exam/<int:pk>/scores/<int:cid>/",
            generic_exam_view.exam_scores_cid_user_admin,
            name="exam_scores_cid_user_admin",
        ),
        path(
            "exam/<int:pk>/scores/",
            generic_exam_view.exam_scores_user,
            name="exam_scores_user",
        ),
        path(
            "exam/<int:pk>/scores/<int:user_id>/user",
            generic_exam_view.exam_scores_user_admin,
            name="exam_scores_user_admin",
        ),
        path(
            "exam/<int:pk>/scores/<int:user_id>/user/supervisor",
            generic_exam_view.exam_scores_user_supervisor,
            name="exam_scores_user_supervisor",
        ),
        path(
            "exam/<int:pk>/toggle_active",
            generic_exam_view.exam_toggle_active_htmx,
            name="exam_toggle_active",
        ),
        path(
            "exam/<int:pk>/toggle_results_published",
            generic_exam_view.exam_toggle_results_published_htmx,
            name="exam_toggle_results_published",
        ),
        path(
            "exam/<int:pk>/toggle_archived",
            generic_exam_view.exam_toggle_archived,
            name="exam_toggle_archived",
        ),
        path(
            "exam/submit",
            generic_exam_view.post_exam_answers,
            name="exam_answers_submit",
        ),
        path("exam/", generic_exam_view.exam_list, name="exam_list"),
        path("collection/<int:collection_id>/", generic_exam_view.exam_list_collection, name="exam_list_collection"),
        path("exam/all", generic_exam_view.exam_list_all, name="exam_list_all"),
        path(
            "exam/<int:exam_id>/order_questions",
            generic_exam_view.order_questions,
            name="order_questions",
        ),
        path("exam/<int:exam_id>/cids", generic_exam_view.exam_cids, name="exam_cids"),
        path("exam/<int:exam_id>/cids/cid_list", generic_exam_view.exam_cids_cid_list, name="exam_cids_cid_list"),
        path("exam/<int:exam_id>/cids/user_list", generic_exam_view.exam_cids_user_list, name="exam_cids_user_list"),
        path("exam/<int:exam_id>/cids/submitted_list", generic_exam_view.exam_cids_submitted_list, name="exam_cids_submitted_list"),
        # path("exam/<int:exam_id>/groups", generic_exam_view.exam_groups_edit, name="exam_groups_edit"),
        path(
            "exam/<int:exam_id>/reset_answers",
            generic_exam_view.exam_reset_answers,
            name="exam_reset_answers",
        ),
        path(
            "exam/<int:exam_id>/cids/edit",
            generic_exam_view.exam_cids_edit,
            name="exam_cids_edit",
        ),
        path(
            "exam/<int:exam_id>/users/edit",
            generic_exam_view.exam_users_edit,
            name="exam_users_edit",
        ),
        path("exam/json/", generic_exam_view.active_exams, name="active_exams"),
        path("exam/json/<int:pk>", generic_exam_view.exam_json, name="exam_json"),
        path(
            "exam/json/<int:pk>/<int:cid>/<str:passcode>",
            generic_exam_view.exam_json_cid,
            name="exam_json_cid",
        ),
        path(
            "exam/json/<int:pk>/unbased",
            generic_exam_view.exam_json_unbased,
            name="exam_json_unbased",
        ),
        path(
            "exam/json/<int:pk>/<int:cid>/<str:passcode>/unbased",
            generic_exam_view.exam_json_cid_unbased,
            name="exam_json_cid_unbased",
        ),
        # These are a little more ambiguous than ideal
        path(
            "exam/json/<int:pk>/question/<int:sk>",
            generic_exam_view.exam_question_json,
            name="exam_question_json",
        ),
        path(
            "exam/json/<int:pk>/question/<int:sk>/unbased",
            generic_exam_view.exam_question_json_unbased,
            name="exam_question_json_unbased",
        ),
        path(
            "exam/json/<int:pk>/question/<int:cid>/<str:passcode>/<int:sk>",
            generic_exam_view.exam_question_json_cid,
            name="exam_question_json_cid",
        ),
        path(
            "exam/json/<int:pk>/question/<int:cid>/<str:passcode>/<int:sk>/unbased",
            generic_exam_view.exam_question_json_unbased_cid,
            name="exam_question_json_unbased_cid",
        ),
        # path(
        #    "exam/json/<int:pk>/<int:sk>",
        #    generic_exam_view.exam_question_json,
        #    name="exam_question_json",
        # ),
        # path(
        #    "exam/json/<int:pk>/<int:sk>/unbased",
        #    generic_exam_view.exam_question_json_unbased,
        #    name="exam_question_json_unbased",
        # ),
        # path(
        #    "exam/json/<int:pk>/<int:cid>/<str:passcode>/<int:sk>",
        #    generic_exam_view.exam_question_json_cid,
        #    name="exam_question_json_cid",
        # ),
        # path(
        #    "exam/json/<int:pk>/<int:cid>/<str:passcode>/<int:sk>/unbased",
        #    generic_exam_view.exam_question_json_unbased_cid,
        #    name="exam_question_json_unbased_cid",
        # ),
        path(
            "exam/json/<int:pk>/recreate",
            generic_exam_view.exam_json_recreate,
            name="exam_json_recreate",
        ),
        path(
            "exam/<int:pk>/user_status",
            generic_exam_view.exam_user_status,
            name="exam_user_status",
        ),
        path(
            "exam/<int:pk>/user_status/<int:cid>/cid",
            generic_exam_view.exam_user_status_cid,
            name="exam_user_status_cid",
        ),
        path(
            "exam/<int:pk>/user_status/<int:user_id>/user",
            generic_exam_view.exam_user_status_user,
            name="exam_user_status_user",
        ),
    ]
    return urlpatterns
