<?php
/**
 * Notifications admin controller.
 *
 * @package Awesomemotive\Rewardswp\Controllers\Admin
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Controllers\Admin;

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

use Awesomemotive\Rewardswp\Interfaces\ControllerInterface;
use Awesomemotive\Rewardswp\Services\NotificationService;

/**
 * Controller for handling admin notification AJAX actions.
 *
 * @since 1.0.0
 */
class NotificationsAdminController implements ControllerInterface {

	/**
	 * Notification service instance.
	 *
	 * @since 1.0.0
	 * @var NotificationService
	 */
	private NotificationService $notification_service;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param NotificationService $notification_service Notification service instance.
	 */
	public function __construct( NotificationService $notification_service ) {
		$this->notification_service = $notification_service;
	}

	/**
	 * Initialize the controller.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function init() {
		add_action( 'wp_ajax_rewardswp_dismiss_notification', [ $this, 'ajax_dismiss_notification' ] );
		add_action( 'wp_ajax_rewardswp_dismiss_all_notifications', [ $this, 'ajax_dismiss_all_notifications' ] );
	}

	/**
	 * Handle AJAX request to dismiss a notification.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function ajax_dismiss_notification() {
		// Verify nonce.
		check_ajax_referer( 'rewardswp_notifications_nonce', 'nonce' );

		// Check user capabilities.
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error(
				[
					'message' => __( 'You do not have permission to perform this action.', 'rewardswp' ),
				],
				403
			);
		}

		// Get notification ID from request.
		$notification_id = isset( $_POST['notification_id'] ) ? absint( $_POST['notification_id'] ) : 0;

		if ( empty( $notification_id ) ) {
			wp_send_json_error(
				[
					'message' => __( 'Notification ID is required.', 'rewardswp' ),
				],
				400
			);
		}

		// Dismiss the notification.
		$result = $this->notification_service->dismiss_notification( $notification_id );

		if ( $result ) {
			wp_send_json_success(
				[
					'message' => __( 'Notification dismissed successfully.', 'rewardswp' ),
				]
			);
		} else {
			wp_send_json_error(
				[
					'message' => __( 'Failed to dismiss notification.', 'rewardswp' ),
				],
				500
			);
		}
	}

	/**
	 * Handle AJAX request to dismiss all notifications.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function ajax_dismiss_all_notifications() {
		// Verify nonce.
		check_ajax_referer( 'rewardswp_notifications_nonce', 'nonce' );

		// Check user capabilities.
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error(
				[
					'message' => __( 'You do not have permission to perform this action.', 'rewardswp' ),
				],
				403
			);
		}

		// Dismiss all notifications.
		$result = $this->notification_service->dismiss_all_notifications();

		if ( $result ) {
			wp_send_json_success(
				[
					'message' => __( 'All notifications dismissed successfully.', 'rewardswp' ),
				]
			);
		} else {
			wp_send_json_error(
				[
					'message' => __( 'Failed to dismiss all notifications.', 'rewardswp' ),
				],
				500
			);
		}
	}
}
