<?php

namespace PostenBringCheckout\admin\api\handlers;

use WP_REST_Request;
use WP_REST_Response;

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

/**
 * Handles Posten Bring Checkout settings.
 */
class SettingsHandler {
	/**
	 * Updates the dark mode 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_dark_mode( WP_REST_Request $request ): WP_REST_Response {
		$json_data = json_decode( $request->get_body(), true );

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

		$success = update_option( 'posten-bring-checkout-dark-mode', $json_data['darkMode'] );

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