<?php
/**
 * Breakpoint Rest API.
 *
 * @package tripzzy
 */

namespace Tripzzy\Core\RestApis;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

use Tripzzy\Core\Bases\RestApiBase;
use Tripzzy\Core\Helpers\Breakpoints;

if ( ! class_exists( 'Tripzzy\Core\RestApis\BreakpointsRestApi' ) ) {
	/**
	 * Breakpoint Rest API.
	 *
	 * @since 1.3.7
	 */
	class BreakpointsRestApi extends RestApiBase {
		/**
		 * API Name space
		 *
		 * @var string
		 */
		protected static $api_namespace = 'tripzzy/v1';

		/**
		 * Constructor.
		 */
		public function __construct() {
			add_filter( 'tripzzy_filter_rest_api_args', array( $this, 'init_args' ) );
		}

		/**
		 * Rest API arguments.
		 *
		 * $args = array( 'route_name' => $route_args );
		 *
		 * @since 1.3.7
		 */
		protected static function api_args() {
			$args = array(
				'/breakpoints' => array(
					array(
						'methods'             => \WP_REST_Server::READABLE,
						'callback'            => array( __CLASS__, 'get_breakpoints' ),
						'permission_callback' => '__return_true',
					),
					array(
						'methods'             => \WP_REST_Server::EDITABLE,
						'callback'            => array( __CLASS__, 'update_breakpoints' ),
						'permission_callback' => array( __CLASS__, 'get_permissions' ),
						'args'                => array(
							'breakpoints' => array(
								'required'          => true,
								'type'              => 'array',
								'description'       => __( 'Responsive breakpoint settings.', 'tripzzy' ),
								'sanitize_callback' => static function ( $value ) {
									if ( ! is_array( $value ) ) {
										return array();
									}

									$sanitize_recursive = static function ( $data ) use ( &$sanitize_recursive ) {
										$clean = array();

										foreach ( $data as $key => $val ) {
											if ( is_array( $val ) ) {
												$clean[ sanitize_key( $key ) ] = $sanitize_recursive( $val );
											} else {
												$clean[ sanitize_key( $key ) ] = sanitize_text_field( $val );
											}
										}

										return $clean;
									};

									return $sanitize_recursive( $value );
								},
								'validate_callback' => static function ( $param ) {
									return is_array( $param );
								},
							),
						),
					),
				),
			);
			return $args;
		}

		/**
		 * Check permissions.
		 *
		 * @since 1.3.9
		 */
		public static function get_permissions() {
			return apply_filters( 'tripzzy_filter_breakpoints_rest_api_permission', current_user_can( 'edit_theme_options' ) );
		}

		// Callbacks.
		/**
		 * Get All breakpoints.
		 *
		 * @since 1.3.7
		 *
		 * @return array
		 */
		public static function get_breakpoints(): \WP_REST_Response {
			$data     = array(
				'options' => array_values( Breakpoints::options() ), // All Breakpoint options. for dropdown.
				'values'  => array_values( Breakpoints::get_active() ), // Active Breakpoints.
			);
			$response = array(
				'success' => true,
				'data'    => $data,
			);
			return new \WP_REST_Response( $response, 200 );
		}

		/**
		 * Update breakpoints.
		 *
		 * @param \WP_REST_Request $request WP_REST_Request object.
		 * @since 1.3.7
		 * @return array
		 */
		public static function update_breakpoints( \WP_REST_Request $request ): \WP_REST_Response {
			$breakpoints = $request->get_param( 'breakpoints' );
			if ( empty( $breakpoints ) || ! is_array( $breakpoints ) ) {
				return new WP_Error(
					'invalid_breakpoints',
					__( 'Invalid or missing breakpoints parameter.', 'tripzzy' ),
					array( 'status' => 400 )
				);
			}

			update_option( 'tz_active_breakpoints', array_column( $breakpoints, 'value', 'key' ) );

			return new \WP_REST_Response(
				array(
					'message' => __( 'Breakpoints updated successfully.', 'tripzzy' ),
					'data'    => $breakpoints,
				),
				200
			);
		}
	}
}
