<?php
/**
 * Sync Audits Controller
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/includes/common/controllers
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common\Controllers;

use MOMCS_Sync\Includes\Common\Constants\Plugin_Constants;
use MOMCS_Sync\Includes\Common\Handlers\Dashboard_Handler;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Handles sync audits related AJAX endpoints.
 */
class Sync_Queue_Controller {
	/**
	 * AJAX: return sync audits rows.
	 */
	public static function handle_get_sync_queue(): void {
		check_ajax_referer( Plugin_Constants::NONCE_SYNC_QUEUE, 'nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error();
		}

		$page     = isset( $_POST['page'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['page'] ) ) ) : 1;
		$per_page = isset( $_POST['per_page'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['per_page'] ) ) ) : 10;

		$handler = new Dashboard_Handler();
		$result  = $handler->get_sync_queue( $page, $per_page );

		wp_send_json_success( $result );
	}

	/**
	 * AJAX: return logs for a given Action Scheduler action.
	 */
	public static function handle_get_job_logs(): void {
		check_ajax_referer( Plugin_Constants::NONCE_SYNC_QUEUE, 'nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error();
		}

		$action_id = isset( $_POST['action_id'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['action_id'] ) ) ) : 0;
		if ( ! $action_id ) {
			wp_send_json_error();
		}

		$handler = new Dashboard_Handler();
		$logs    = $handler->get_job_logs( (string) $action_id );
		wp_send_json_success( $logs );
	}

	/**
	 * AJAX: return scheduled sync audits rows.
	 */
	public static function handle_get_scheduled_sync_queue(): void {
		check_ajax_referer( Plugin_Constants::NONCE_SYNC_QUEUE, 'nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error();
		}

		$page     = isset( $_POST['page'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['page'] ) ) ) : 1;
		$per_page = isset( $_POST['per_page'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['per_page'] ) ) ) : 10;

		$handler = new Dashboard_Handler();
		$result  = $handler->get_scheduled_sync_queue( $page, $per_page );

		wp_send_json_success( $result );
	}

	/**
	 * AJAX: return order sync audits rows.
	 *
	 * @return void
	 */
	public static function handle_get_order_sync_queue(): void {
		check_ajax_referer( Plugin_Constants::NONCE_SYNC_QUEUE, 'nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error();
		}

		$page      = isset( $_POST['page'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['page'] ) ) ) : 1;
		$per_page  = isset( $_POST['per_page'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['per_page'] ) ) ) : 10;
		$sync_type = isset( $_POST['sync_type'] ) ? sanitize_text_field( wp_unslash( $_POST['sync_type'] ) ) : '';

		$handler = new Dashboard_Handler();
		$result  = $handler->get_order_sync_queue( $page, $per_page, $sync_type );

		wp_send_json_success( $result );
	}
}
