<?php

namespace PostenBringCheckout\admin\api\handlers;

use WP_REST_Request;
use WP_REST_Response;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Handles the remote log toggle.
 */
class RemoteLogApiHandler {
	/**
	 * Updates the remote log option based on the request data.
	 *
	 * @param WP_REST_Request $request The REST request object.
	 *
	 * @return WP_REST_Response The REST response object.
	 */
	public function update_remote_log( WP_REST_Request $request ) {
		$json_data = json_decode( $request->get_body(), true );

		if ( ! isset( $json_data['remoteLoggingEnabled'] ) || ! is_bool( $json_data['remoteLoggingEnabled'] ) ) {
			return new WP_REST_Response( 'Invalid input: remoteLoggingEnabled must be a boolean.', 400 );
		}

		$success = update_option( 'posten-bring-checkout-remote-log', $json_data['remoteLoggingEnabled'] );

		if ( $success ) {
			return new WP_REST_Response( array( 'success' => true ), 200 );
		} else {
			return new WP_REST_Response( 'Failed to update remote log setting.', 500 );
		}
	}

	/**
	 * Retrieves the current remote log setting.
	 *
	 * @return WP_REST_Response The REST response object.
	 */
	public function get_remote_log() {
		$remote_logging_enabled = get_option( 'posten-bring-checkout-remote-log', false );

		return new WP_REST_Response( array( 'remoteLoggingEnabled' => (bool) $remote_logging_enabled ), 200 );
	}
}
