(function($) {
  'use strict';

  var state = {
    paymentStarted: false
  };

  function checkTrialStatus() {
    return $.ajax({
        url: mindwecoData.ajaxurl,
        method: 'POST',
        data: {
          action: 'mindweco_get_trial_status',
          nonce: mindwecoData.nonce,
          status: mindwecoData.trial_status
        },
        dataType: 'json'
      })
      .then(function(response) {
        if (response.success && response.data) {
          var newEnd = response.data.newEndDate;
          var newStatus = response.data.status;
          var newType = response.data.type;
          if (newEnd) mindwecoData.trialEndDate = newEnd;
          if (newStatus != null) mindwecoData.trial_status = newStatus;
          if (newType != null) mindwecoData.trial_type = newType;
        }
        return response;
      })
      .catch(function(err) {
        // Trial status check failed — returning empty response, safe to ignore.
        return {
          success: false,
          data: null
        };
      });
  }

  async function initializeChat() {
    try {
      var trialResp = await checkTrialStatus();
      var newEndDate = (trialResp.data && trialResp.data.newEndDate) ?
        trialResp.data.newEndDate : mindwecoData.trialEndDate;
      var rawStatus = (trialResp.data && trialResp.data.status != null) ?
        trialResp.data.status : mindwecoData.trial_status;
      var status = (rawStatus === null || rawStatus === undefined || rawStatus === '') ?
        -1 : Number(rawStatus);

      mindwecoData.trialEndDate = newEndDate;
      mindwecoData.trial_status = status;

      if ($('#mindweco-container').is(':visible') && window.MindwecoHistory) {
        window.MindwecoHistory.showLiteBanner(false);
      }

    } catch (err) {
      // Chat initialization failed — subscription status unavailable, safe to ignore.
    }
  }

  async function handleSubscriptionRefresh() {
    state.paymentStarted = false;
    try {
      var response = await checkTrialStatus();
      var newType = Number(response.data && response.data.type ? response.data.type : 0);
      var newStatus = Number(response.data && response.data.status != null ? response.data.status : 0);
      var newEndDate = (response.data && response.data.newEndDate) ?
        response.data.newEndDate : mindwecoData.trialEndDate;

      mindwecoData.trial_type = newType;
      mindwecoData.trial_status = newStatus;
      mindwecoData.trialEndDate = newEndDate;

      $('#mindweco-btn-wrapper').show();
      setTimeout(function() {
        window.MindwecoSubscriptions.renderBuyButton();
        window.MindwecoSubscriptions.load();
      }, 500);

    } catch (error) {
      // Subscription refresh failed — safe to ignore.
    }
  }

  $(document).ready(function() {
    window.MindwecoTheme.init();
    window.MindwecoCitations.init();
    window.MindwecoMessages.init();
    window.MindwecoHistory.init();
    window.MindwecoSubscriptions.init();
    window.MindwecoLogs.init();
    window.MindwecoUI.init();

    $('#refresh-plugin').on('click', handleSubscriptionRefresh);

    initializeChat();

    window.MindwecoMain = {
      state: state,
      theme: {
        toggle: window.MindwecoTheme.toggle,
        updateIcon: window.MindwecoTheme.updateIcon,
        detect: window.MindwecoTheme.detect
      }
    };
  });

})(jQuery);
