'use strict';

$(function () { // jQuery document ready

  // --- Global Variables & Selectors ---
  const categoriesDataTableElement = $('.datatables-lms-categories');
  const categoryOffcanvasElement = $('#lmsCategoryOffcanvas');
  const categoryOffcanvas = categoryOffcanvasElement.length ? new bootstrap.Offcanvas(categoryOffcanvasElement[0]) : null;
  const categoryForm = $('#lmsCategoryForm');
  const categoryOffcanvasLabel = $('#lmsCategoryOffcanvasLabel');
  const submitCategoryBtn = $('#submitLmsCategoryBtn');
  const categoryIdInput = $('#category_id');
  const categoryMethodInput = $('#categoryMethod');
  const categoryNameInput = $('#categoryName');
  const isActiveCheckbox = $('#is_active');
  const generalErrorDiv = $('#general-error');
  const categoryDescriptionInput = $('#categoryDescription');

  // --- CSRF Setup ---
  $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': pageData.csrfToken } });

  // --- Initialize Plugins ---
  $('.select2-basic').each(function () {
    $(this).select2({ minimumResultsForSearch: Infinity, dropdownParent: $(this).parent() });
  });

  // --- Helper Functions ---

  // Reset Offcanvas Form Validation
  function resetCategoryFormValidation() {
    categoryForm.find('.is-invalid').removeClass('is-invalid');
    categoryForm.find('.invalid-feedback').text('');
    generalErrorDiv.text('').hide();
  }

  // Reset Offcanvas Form Content
  function resetCategoryForm() {
    resetCategoryFormValidation();
    categoryForm[0]?.reset();
    categoryIdInput.val('');
    categoryMethodInput.val('POST');
    isActiveCheckbox.prop('checked', true);
    categoryOffcanvasLabel.text(pageData.labels.addNewCategory);
    submitCategoryBtn.text(pageData.labels.submit).prop('disabled', false);
  }

  // Set Submit Button Loading State
  function setCategoryButtonLoading(isLoading) {
    const buttonText = categoryIdInput.val() ? pageData.labels.update : pageData.labels.submit;
    submitCategoryBtn.prop('disabled', isLoading);
    submitCategoryBtn.html(isLoading ? '<span class="spinner-border spinner-border-sm"></span> ' + pageData.labels.processing : buttonText);
  }

  // Display Validation Errors in Form
  function displayCategoryValidationErrors(errors) {
    resetCategoryFormValidation();
    let firstErrorElement = null;
    console.log("Validation Errors:", errors);
    for (const fieldName in errors) {
      const inputElement = categoryForm.find(`[name="${fieldName}"]`);
      if (inputElement.length) {
        inputElement.addClass('is-invalid');
        let feedbackElement = inputElement.closest('.mb-3, .mb-4').find('.invalid-feedback');
        if (feedbackElement.length) { feedbackElement.text(errors[fieldName][0]).show(); }
        else { inputElement.after(`<div class="invalid-feedback d-block">${errors[fieldName][0]}</div>`); }
        if (!firstErrorElement) firstErrorElement = inputElement;
      } else { generalErrorDiv.append(`<div>${fieldName}: ${errors[fieldName][0]}</div>`).show(); if (!firstErrorElement) firstErrorElement = generalErrorDiv; }
    }
    firstErrorElement?.focus();
  }

  // --- DataTable Initialization ---
  let dtCategories;
  if (categoriesDataTableElement.length) {
    dtCategories = categoriesDataTableElement.DataTable({
      processing: true, serverSide: true,
      ajax: { url: pageData.urls.listAjax, type: 'GET' },
      columns: [
        { data: 'id', name: 'id' },
        { data: 'name', name: 'name' },
        { data: 'description', name: 'description', orderable: false },
        { data: 'courses_count_display', name: 'courses_count', className: 'text-center', searchable: false },
        { data: 'status', name: 'is_active', className: 'text-center', orderable: true, searchable: false },
        { data: 'actions', name: 'actions', orderable: false, searchable: false, className: 'text-center' }
      ],
      columnDefs: [ { targets: [3, 4, 5], className: 'text-center' } ],
      order: [[1, 'asc']],
      dom: '<"row me-2"<"col-md-2"<"me-3"l>><"col-md-10"<"dt-action-buttons text-xl-end text-lg-start text-md-end text-start d-flex align-items-center justify-content-end flex-md-row flex-column mb-3 mb-md-0"fB>>>t<"row mx-2"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>',
      language: {
        processing: pageData.datatable?.processing || 'Processing...',
        lengthMenu: pageData.datatable?.lengthMenu || 'Show _MENU_ entries',
        zeroRecords: pageData.datatable?.zeroRecords || 'No matching records found',
        info: pageData.datatable?.info || 'Showing _START_ to _END_ of _TOTAL_ entries',
        infoEmpty: pageData.datatable?.infoEmpty || 'Showing 0 to 0 of 0 entries',
        infoFiltered: pageData.datatable?.infoFiltered || '(filtered from _MAX_ total entries)',
        search: '',
        searchPlaceholder: pageData.labels.searchPlaceholder,
        paginate: pageData.datatable?.paginate || { first: 'First', last: 'Last', next: 'Next', previous: 'Previous' },
        emptyTable: pageData.datatable?.emptyTable || 'No data available in table',
        loadingRecords: pageData.datatable?.loadingRecords || 'Loading...'
      },
      buttons: [],
    });
  }

  // --- Offcanvas Handling ---
  if(categoryOffcanvasElement.length) { categoryOffcanvasElement.on('hidden.bs.offcanvas', resetCategoryForm); }

  $('#addLmsCategoryBtn').on('click', function () {
    resetCategoryForm();
    categoryOffcanvasLabel.text(pageData.labels.addNewCategory);
    categoryOffcanvas?.show();
  });

  categoriesDataTableElement.on('click', '.edit-category', function () {
    const categoryId = $(this).data('id');
    const editUrl = $(this).data('url');
    resetCategoryForm();
    categoryOffcanvasLabel.text(pageData.labels.loadingData);
    categoryOffcanvas?.show();
    $.ajax({ url: editUrl, type: 'GET', dataType: 'json',
      success: function (response) {
        if (response.success && response.category) {
          const data = response.category;
          categoryOffcanvasLabel.text(pageData.labels.editCategory + ': ' + data.name);
          categoryIdInput.val(data.id); categoryMethodInput.val('PUT');
          categoryNameInput.val(data.name || ''); categoryDescriptionInput.val(data.description || '');
          isActiveCheckbox.prop('checked', data.is_active);
        } else { categoryOffcanvas?.hide(); showErrorToast(response.message || pageData.labels.failedToLoadData); }
      },
      error: function (jqXHR) { categoryOffcanvas?.hide(); showErrorToast(jqXHR.responseJSON?.message || pageData.labels.couldNotLoadData); }
    });
  });

  // --- Form Submission (Add/Edit) ---
  if (categoryForm.length) {
    categoryForm.on('submit', function(e) {
      e.preventDefault(); resetCategoryFormValidation();
      const isUpdate = !!categoryIdInput.val();
      const url = isUpdate ? `${pageData.urls.base}/${categoryIdInput.val()}` : pageData.urls.store;
      const method = 'POST'; let formData = new FormData(categoryForm[0]);
      if (isUpdate) { formData.append('_method', 'PUT'); }
      formData.set('is_active', isActiveCheckbox.is(':checked') ? '1' : '0');

      setCategoryButtonLoading(true);
      $.ajax({ url: url, method: method, data: formData, processData: false, contentType: false, dataType: 'json',
        success: function (response) {
          if(response.success) {
            categoryOffcanvas?.hide();
            showSuccessToast(response.message);
            dtCategories?.ajax.reload(null, false);
          } else { showErrorToast(response.message || pageData.labels.operationFailed); }
        },
        error: function (jqXHR) {
          if (jqXHR.status === 422 && jqXHR.responseJSON?.errors) { displayCategoryValidationErrors(jqXHR.responseJSON.errors); showErrorToast(pageData.labels.validationFailed); }
          else { showErrorToast(jqXHR.responseJSON?.message || pageData.labels.anErrorOccurred); }
        },
        complete: function () { setCategoryButtonLoading(false); }
      });
    });
  }

  // --- Toggle Status Handling ---
  categoriesDataTableElement.on('change', '.category-status-toggle', function () {
    var checkbox = $(this); var url = checkbox.data('url');
    var currentStateIsActive = !checkbox.is(':checked');
    checkbox.prop('disabled', true);
    $.ajax({ url: url, method: 'POST', data: { _method: 'PUT' }, dataType: 'json',
      success: function(response) {
        if (response.success) {
          showSuccessToast(response.message || pageData.labels.statusUpdated);
          dtCategories?.row(checkbox.closest('tr')).invalidate('data').draw(false);
        } else {
          showErrorToast(response.message || pageData.labels.failedToUpdateStatus);
          checkbox.prop('checked', currentStateIsActive);
        }
      },
      error: function(jqXHR) {
        showErrorToast(pageData.labels.errorTogglingStatus);
        checkbox.prop('checked', currentStateIsActive);
      },
      complete: function() { checkbox.prop('disabled', false); }
    });
  });

  // --- Delete Category Handling ---
  categoriesDataTableElement.on('click', '.delete-category', function () {
    const button = $(this); const deleteUrl = button.data('url');
    const courseCount = parseInt(button.data('count') || 0, 10);
    if (courseCount > 0) { Swal.fire({ icon: 'error', title: pageData.labels.cannotDelete, text: pageData.labels.categoryHasCourses }); return; }
    Swal.fire({ title: pageData.labels.areYouSure, text: pageData.labels.deleteLessonWarning, icon: 'warning', showCancelButton: true, confirmButtonText: pageData.labels.yesDeleteIt, customClass: { confirmButton: 'btn btn-danger me-3', cancelButton: 'btn btn-label-secondary' }, buttonsStyling: false
    }).then(function (result) {
      if (result.isConfirmed) {
        $.ajax({ url: deleteUrl, method: 'POST', data: { _method: 'DELETE' }, dataType: 'json',
          success: function(response) {
            if (response.success) { showSuccessToast(response.message); dtCategories?.row(button.closest('tr')).remove().draw(false); }
            else { showErrorToast(response.message || pageData.labels.failedToDelete); }
          },
          error: function(jqXHR) { showErrorToast(jqXHR.responseJSON?.message || pageData.labels.failedToDelete); }
        });
      }
    });
  });

}); // End Document Ready
