<?php
namespace Dreamfox\DeliveryDate\Premium\Admin;

use Dreamfox\DeliveryDate\Abstracts\AbstractAjax;
use Dreamfox\DeliveryDate\Facades\SystemFacade as FreeSystemFacade;
use Dreamfox\DeliveryDate\Premium\Facades\SystemFacade;

class AdminAjax extends AbstractAjax
{

  public function getAjaxEvents()
  {
    return [
      'load_categories',
      'save_common_settings',
      'load_common_settings',
      'save_category_settings',
      'load_category_settings',
      'save_other_settings',
      'load_other_settings',
      'save_holiday_settings',
      'load_holiday_settings',
      // Timeslot is a free feature — always register in premium too.
      'save_timeslot_settings',
      'load_timeslot_settings',
    ];
  }

  public function load_categories()
  {
    $this->_verifyAdminRequest();
    $categories = SystemFacade::getInstance()->loadCategories();
    $response = array(
			'message' => 'Updated successfully!',
      'categories' => $categories,
		);
		wp_send_json( $response );
  }

  #begin_region common settings
    public function save_common_settings()
    {
      $this->_verifyAdminRequest();
      $data = $this->_getRequestData();
      SystemFacade::getInstance()->saveCommonSettings($data);
      $response = array(
        'message' => 'Updated successfully!',
      );
      wp_send_json( $response );
    }

    public function load_common_settings()
    {
      $this->_verifyAdminRequest();
      $settings = SystemFacade::getInstance()->loadCommonSettings();
      $response = $settings->toArray();
      wp_send_json( $response );
    }
  #end_region

  #begin_region category settings
    public function save_category_settings()
    {
      $this->_verifyAdminRequest();
      $data = $this->_getRequestData();
      SystemFacade::getInstance()->saveCategorySettings($data);
      $response = array(
        'message' => 'Updated successfully!',
      );
      wp_send_json( $response );
    }

    public function load_category_settings()
    {
      $this->_verifyAdminRequest();
      $settings = SystemFacade::getInstance()->loadCategorySettings();
      $response = $settings->toArray();
      wp_send_json( $response );
    }
  #end_region

  #begin_region holiday settings
    public function save_holiday_settings()
    {
      $this->_verifyAdminRequest();
      $data = $this->_getRequestData();
      SystemFacade::getInstance()->saveHolidaySettings($data);
      $response = array(
        'message' => 'Updated successfully!',
      );
      wp_send_json( $response );
    }

    public function load_holiday_settings()
    {
      $this->_verifyAdminRequest();
      $settings = SystemFacade::getInstance()->loadHolidaySettings();
      $response = $settings->toArray();
      wp_send_json( $response );
    }
  #end_region

  #begin_region other settings
    public function save_other_settings()
    {
      $this->_verifyAdminRequest();
      $data = $this->_getRequestData();
      SystemFacade::getInstance()->saveOtherSettings($data);
      $response = array(
        'message' => 'Updated successfully!',
      );
      wp_send_json( $response );
    }

    public function load_other_settings()
    {
      $this->_verifyAdminRequest();
      $settings = SystemFacade::getInstance()->loadOtherSettings();
      $response = $settings->toArray();
      wp_send_json( $response );
    }
  #end_region

  #begin_region timeslot settings (free feature — available in premium too)
    public function save_timeslot_settings()
    {
      $this->_verifyAdminRequest();
      $data = $this->_getRequestData();
      FreeSystemFacade::getInstance()->saveTimeslotSettings( $data );
      wp_send_json( array( 'message' => 'Timeslot settings saved!' ) );
    }

    public function load_timeslot_settings()
    {
      $this->_verifyAdminRequest();
      $settings = FreeSystemFacade::getInstance()->loadTimeslotSettings();
      wp_send_json( $settings->toArray() );
    }
  #end_region

}