<?php
/**
 * Dashboard API Controller
 *
 * @package DokanKits\REST\Controllers\Version1
 * @since 3.0.0
 */

namespace DokanKits\REST\Controllers\Version1;

use DokanKits\Core\Data\Models\Settings;
use DokanKits\Core\Data\Stores\SettingsDataStore;
use DokanKits\Features\Feature;
use DokanKits\REST\Controllers\Controller;
use Exception;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;

/**
 * Dashboard API Controller class.
 *
 * Handles API endpoints for dashboard data, statistics, and feature management.
 *
 * @since 3.0.0
 */
class DashboardController extends Controller {
	/**
	 * Route base.
	 *
	 * @var string
	 */
	protected $rest_base = 'dashboard';

	/**
	 * Register routes.
	 *
	 * @since 3.0.0
	 *
	 * @return void
	 */
	public function register_routes() {
		// Main dashboard data endpoint.
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_dashboard_data' ),
					'permission_callback' => array( $this, 'admin_permissions_check' ),
					'args'                => $this->get_dashboard_params(),
				),
				'schema' => array( $this, 'get_dashboard_schema' ),
			)
		);

		// Statistics endpoint.
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/statistics',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_statistics' ),
					'permission_callback' => array( $this, 'admin_permissions_check' ),
					'args'                => $this->get_statistics_params(),
				),
				'schema' => array( $this, 'get_statistics_schema' ),
			)
		);

		// Settings export endpoint.
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/export',
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'export_settings' ),
					'permission_callback' => array( $this, 'admin_permissions_check' ),
					'args'                => $this->get_export_params(),
				),
				'schema' => array( $this, 'get_export_schema' ),
			)
		);

		// Settings import endpoint.
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/import',
			array(
				array(
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => array( $this, 'import_settings' ),
					'permission_callback' => array( $this, 'admin_permissions_check' ),
					'args'                => $this->get_import_params(),
				),
				'schema' => array( $this, 'get_import_schema' ),
			)
		);

		// Settings reset endpoint.
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/reset',
			array(
				array(
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => array( $this, 'reset_settings' ),
					'permission_callback' => array( $this, 'admin_permissions_check' ),
					'args'                => $this->get_reset_params(),
				),
				'schema' => array( $this, 'get_reset_schema' ),
			)
		);
	}

	/**
	 * Get dashboard data.
	 *
	 * @since 3.0.0
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 *
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function get_dashboard_data( WP_REST_Request $request ) {
		try {
			// Get request parameters.
			$include_statistics = $this->sanitize_bool( $request->get_param( 'include_statistics' ) );
			$include_plugin     = $this->sanitize_bool( $request->get_param( 'include_plugin' ) );

			// Prepare base response data.
			$response_data = $this->prepare_base_dashboard_data();

			// Add statistics if requested.
			if ( $include_statistics ) {
				$response_data['statistics'] = $this->get_statistics_data();
			}

			// Add plugin data if requested.
			if ( $include_plugin ) {
				$response_data['plugin'] = $this->get_plugin_data();
			}

			/**
			 * Filter the dashboard data.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $response_data The dashboard data.
			 * @param WP_REST_Request $request       The request object.
			 */
			$response_data = apply_filters( 'dokan_kits_dashboard_data', $response_data, $request );

			/**
			 * Action fired after getting dashboard data.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $response_data The dashboard data.
			 * @param WP_REST_Request $request       The request object.
			 */
			do_action( 'dokan_kits_after_get_dashboard_data', $response_data, $request );

			return $this->success(
				$response_data,
				__( 'Dashboard data retrieved successfully.', 'dokan-kits' )
			);

		} catch ( Exception $e ) {
			return $this->handle_exception( $e, 'get_dashboard_data' );
		}
	}

	/**
	 * Get statistics data.
	 *
	 * @since 3.0.0
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 *
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function get_statistics( WP_REST_Request $request ) {
		try {
			/**
			 * Action fired before getting statistics.
			 *
			 * @since 3.0.0
			 *
			 * @param WP_REST_Request $request The request object.
			 */
			do_action( 'dokan_kits_before_get_statistics', $request );

			$refresh = $this->sanitize_bool( $request->get_param( 'refresh' ) );

			// Get statistics data.
			$statistics = $this->get_statistics_data( $refresh );

			/**
			 * Filter the statistics data.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $statistics The statistics data.
			 * @param WP_REST_Request $request    The request object.
			 */
			$statistics = apply_filters( 'dokan_kits_statistics_data', $statistics, $request );

			/**
			 * Action fired after getting statistics.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $statistics The statistics data.
			 * @param WP_REST_Request $request    The request object.
			 */
			do_action( 'dokan_kits_after_get_statistics', $statistics, $request );

			return $this->success(
				$statistics,
				__( 'Statistics retrieved successfully.', 'dokan-kits' )
			);

		} catch ( Exception $e ) {
			return $this->handle_exception( $e, 'get_statistics' );
		}
	}

	/**
	 * Export settings.
	 *
	 * @since 3.0.0
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 *
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function export_settings( WP_REST_Request $request ) {
		try {
			// Get and validate parameters.
			$format = $this->sanitize_text( $request->get_param( 'format' ) );
			$groups = $this->sanitize_array( $request->get_param( 'groups' ) );

			// Validate format.
			if ( ! in_array( $format, array( 'json', 'array' ), true ) ) {
				return $this->error(
					__( 'Invalid export format. Allowed formats: json, array', 'dokan-kits' ),
					'invalid_export_format',
					400
				);
			}

			// Validate and filter groups.
			$groups = array_intersect( $groups, dokan_kits()->get_settings_groups() );
			if ( empty( $groups ) ) {
				return $this->error(
					__( 'No valid groups specified for export.', 'dokan-kits' ),
					'no_valid_groups',
					400
				);
			}

			// Export settings.
			$export_result = $this->export_settings_data( $groups, $format );

			/**
			 * Filter the export data.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $export_result The export result.
			 * @param array           $groups        The exported groups.
			 * @param string          $format        The export format.
			 * @param WP_REST_Request $request       The request object.
			 */
			$export_result = apply_filters( 'dokan_kits_export_settings_result', $export_result, $groups, $format, $request );

			/**
			 * Action fired after exporting settings.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $export_result The export result.
			 * @param WP_REST_Request $request       The request object.
			 */
			do_action( 'dokan_kits_after_export_settings', $export_result, $request );

			return $this->success(
				$export_result,
				sprintf(
				/* translators: %d: Number of settings exported */
					_n(
						'%d setting exported successfully.',
						'%d settings exported successfully.',
						$export_result['count'],
						'dokan-kits'
					),
					$export_result['count']
				)
			);

		} catch ( Exception $e ) {
			return $this->handle_exception( $e, 'export_settings' );
		}
	}

	/**
	 * Import settings.
	 *
	 * @since 3.0.0
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 *
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function import_settings( WP_REST_Request $request ) {
		try {
			// Get and validate parameters.
			$settings_data = $request->get_param( 'settings' );
			$overwrite     = $this->sanitize_bool( $request->get_param( 'overwrite' ) );

			if ( empty( $settings_data ) || ! is_array( $settings_data ) ) {
				return $this->missing_parameter_error( 'settings' );
			}

			// Validate settings count to prevent abuse.
			if ( count( $settings_data ) > 100 ) {
				return $this->error(
					__( 'Too many settings provided. Maximum 100 settings allowed.', 'dokan-kits' ),
					'too_many_settings',
					400
				);
			}

			// Import settings.
			$import_result = $this->import_settings_data( $settings_data, $overwrite );

			/**
			 * Filter the import result.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $import_result The import result.
			 * @param array           $settings_data The imported settings data.
			 * @param bool            $overwrite     Whether to overwrite existing settings.
			 * @param WP_REST_Request $request       The request object.
			 */
			$import_result = apply_filters( 'dokan_kits_import_settings_result', $import_result, $settings_data, $overwrite, $request );

			/**
			 * Action fired after importing settings.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $import_result The import result.
			 * @param WP_REST_Request $request       The request object.
			 */
			do_action( 'dokan_kits_after_import_settings', $import_result, $request );

			// Return response based on results.
			if ( ! empty( $import_result['errors'] ) && empty( $import_result['imported'] ) ) {
				// All imports failed.
				return $this->error(
					__( 'All settings failed to import.', 'dokan-kits' ),
					'all_imports_failed',
					400,
					$import_result
				);
			}

			if ( ! empty( $import_result['errors'] ) ) {
				// Partial success.
				return $this->error(
					__( 'Some settings failed to import.', 'dokan-kits' ),
					'partial_import_failure',
					207, // Multi-Status.
					$import_result
				);
			}

			// All imports successful.
			return $this->success(
				$import_result,
				sprintf(
				/* translators: %d: Number of settings imported */
					_n(
						'%d setting imported successfully.',
						'%d settings imported successfully.',
						count( $import_result['imported'] ),
						'dokan-kits'
					),
					count( $import_result['imported'] )
				)
			);

		} catch ( Exception $e ) {
			return $this->handle_exception( $e, 'import_settings' );
		}
	}

	/**
	 * Reset settings.
	 *
	 * @since 3.0.0
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 *
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function reset_settings( WP_REST_Request $request ) {
		try {
			// Get and validate parameters.
			$scope   = $this->sanitize_text( $request->get_param( 'scope' ) );
			$confirm = $this->sanitize_bool( $request->get_param( 'confirm' ) );

			if ( ! $confirm ) {
				return $this->error(
					__( 'Please confirm that you want to reset the settings.', 'dokan-kits' ),
					'confirmation_required',
					400
				);
			}

			// Validate scope.
			$allowed_scopes = array_merge( array( 'all' ), dokan_kits()->get_settings_groups() );
			if ( ! in_array( $scope, $allowed_scopes, true ) ) {
				return $this->error(
					sprintf(
					/* translators: %s: allowed scopes */
						__( 'Invalid scope. Allowed scopes: %s', 'dokan-kits' ),
						implode( ', ', $allowed_scopes )
					),
					'invalid_scope',
					400
				);
			}

			// Reset settings.
			$reset_result = $this->reset_settings_data( $scope );

			/**
			 * Filter the reset result.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $reset_result The reset result.
			 * @param string          $scope        The reset scope.
			 * @param WP_REST_Request $request      The request object.
			 */
			$reset_result = apply_filters( 'dokan_kits_reset_settings_result', $reset_result, $scope, $request );

			/**
			 * Action fired after resetting settings.
			 *
			 * @since 3.0.0
			 *
			 * @param array           $reset_result The reset result.
			 * @param string          $scope        The reset scope.
			 * @param WP_REST_Request $request      The request object.
			 */
			do_action( 'dokan_kits_after_reset_settings', $reset_result, $scope, $request );

			return $this->success(
				$reset_result,
				$reset_result['message']
			);

		} catch ( Exception $e ) {
			return $this->handle_exception( $e, 'reset_settings' );
		}
	}

	/**
	 * Prepare base dashboard data.
	 *
	 * @since 3.0.0
	 *
	 * @return array Base dashboard data.
	 */
	protected function prepare_base_dashboard_data(): array {
		$settings_store = dokan_kits_get_container()->get( SettingsDataStore::class );
		$settings       = $settings_store->get_all( array( 'in' => dokan_kits()->get_settings_groups() ) );

		$dashboard_data = array(
			'settings_count' => count( $settings ),
			'last_updated'   => current_time( 'mysql' ),
		);

		/**
		 * Filter the base dashboard data before returning.
		 *
		 * @since 3.0.0
		 *
		 * @param array $dashboard_data The base dashboard data.
		 */
		return apply_filters( 'dokan_kits_dashboard_base_data', $dashboard_data );
	}

	/**
	 * Get statistics data.
	 *
	 * @since 3.0.0
	 *
	 * @param bool $refresh Whether to refresh cached data.
	 *
	 * @return array Statistics data.
	 */
	protected function get_statistics_data( bool $refresh = false ): array {
		// Check cache first (if not refreshing).
		if ( ! $refresh ) {
			$cached_stats = wp_cache_get( 'dokan_kits_dashboard_stats', 'dokan-kits' );
			if ( false !== $cached_stats ) {
				return $cached_stats;
			}
		}

		$settings_store  = dokan_kits_get_container()->get( SettingsDataStore::class );
		$settings        = $settings_store->get_all( array( 'in' => dokan_kits()->get_settings_groups() ) );
		$total_features  = 0;
		$active_features = 0;

		// Get feature statistics from all registered features.
		$features = dokan_kits_get_container()->get( 'feature-service' );

		foreach ( $features as $feature ) {
			if ( ! $feature instanceof Feature ) {
				continue;
			}

			$total_features  += $feature->get_total_features();
			$active_features += $feature->get_active_features_count();
		}

		$statistics = array(
			'active_features' => array(
				'value'       => $active_features,
				'label'       => __( 'Active Features', 'dokan-kits' ),
				'description' => __( 'Number of enabled features', 'dokan-kits' ),
				'icon'        => 'dashicons-admin-plugins',
				'color'       => 'primary',
			),
			'total_features'  => array(
				'value'       => $total_features,
				'label'       => __( 'Total Features', 'dokan-kits' ),
				'description' => __( 'Total available features', 'dokan-kits' ),
				'icon'        => 'dashicons-admin-generic',
				'color'       => 'secondary',
			),
			'settings_count'  => array(
				'value'       => count( $settings ),
				'label'       => __( 'Settings', 'dokan-kits' ),
				'description' => __( 'Number of configured settings', 'dokan-kits' ),
				'icon'        => 'dashicons-admin-settings',
				'color'       => 'blue',
			),
			'dokan_version'   => array(
				'value'       => defined( 'DOKAN_PLUGIN_VERSION' ) ? DOKAN_PLUGIN_VERSION : __( 'Not installed', 'dokan-kits' ),
				'label'       => __( 'Dokan Version', 'dokan-kits' ),
				'description' => __( 'Installed Dokan version', 'dokan-kits' ),
				'icon'        => 'dashicons-admin-tools',
				'color'       => 'green',
			),
		);

		/**
		 * Filter the statistics data.
		 *
		 * @since 3.0.0
		 *
		 * @param array      $statistics The statistics data.
		 * @param Settings[] $settings   The settings data.
		 */
		$statistics = apply_filters( 'dokan_kits_dashboard_statistics', $statistics, $settings );

		// Cache the statistics for 5 minutes.
		wp_cache_set( 'dokan_kits_dashboard_stats', $statistics, 'dokan-kits', 5 * MINUTE_IN_SECONDS );

		return $statistics;
	}

	/**
	 * Get plugin data.
	 *
	 * @since 3.0.0
	 *
	 * @return array Plugin data.
	 */
	protected function get_plugin_data(): array {
		$plugin_data = array(
			'version'     => defined( 'DOKAN_KITS_VERSION' ) ? DOKAN_KITS_VERSION : '1.0.0',
			'name'        => 'Dokan Kits',
			'description' => __( 'Enhance your Dokan-powered marketplace with powerful tools and customizations.', 'dokan-kits' ),
			'url'         => 'https://wpintegrity.com/addons/',
			'author'      => 'WPIntegrity',
			'text_domain' => 'dokan-kits',
		);

		/**
		 * Filter the plugin data.
		 *
		 * @since 3.0.0
		 *
		 * @param array $plugin_data The plugin data.
		 */
		return apply_filters( 'dokan_kits_dashboard_plugin_data', $plugin_data );
	}

	/**
	 * Export settings data.
	 *
	 * @since 3.0.0
	 *
	 * @param array  $groups Groups to export.
	 * @param string $format Export format.
	 *
	 * @return array Export result.
	 */
	protected function export_settings_data( array $groups, string $format ): array {
		$settings_store = dokan_kits_get_container()->get( SettingsDataStore::class );
		$settings       = $settings_store->get_all( array( 'in' => $groups ) );

		$export_data = array();
		foreach ( $settings as $setting ) {
			$export_data[ $setting->get_name() ] = $setting->get_value();
		}

		/**
		 * Filter the export data.
		 *
		 * @since 3.0.0
		 *
		 * @param array      $export_data The export data.
		 * @param Settings[] $settings    The settings data.
		 * @param array      $groups      The exported groups.
		 */
		$export_data = apply_filters( 'dokan_kits_export_settings_data', $export_data, $settings, $groups );

		// Format data based on request.
		if ( 'json' === $format ) {
			$formatted_data = wp_json_encode( $export_data, JSON_PRETTY_PRINT );
		} else {
			$formatted_data = $export_data;
		}

		return array(
			'data'      => $formatted_data,
			'format'    => $format,
			'groups'    => $groups,
			'count'     => count( $export_data ),
			'timestamp' => current_time( 'mysql' ),
		);
	}

	/**
	 * Import settings data.
	 *
	 * @since 3.0.0
	 *
	 * @param array $settings_data Settings data to import.
	 * @param bool  $overwrite     Whether to overwrite existing settings.
	 *
	 * @return array Import result.
	 */
	protected function import_settings_data( array $settings_data, bool $overwrite ): array {
		$errors   = array();
		$imported = array();

		foreach ( $settings_data as $name => $value ) {
			try {
				// Validate setting name.
				if ( ! is_string( $name ) || empty( $name ) ) {
					$errors[ $name ] = __( 'Invalid setting name.', 'dokan-kits' );
					continue;
				}

				// Sanitize setting name.
				$name = $this->sanitize_key( $name );

				$settings_store = dokan_kits_get_container()->get( SettingsDataStore::class );
				$setting        = $settings_store->get_settings_by_name( $name );

				if ( null === $setting ) {
					$setting = dokan_kits_get_container()->get( Settings::class );
					$setting->set_name( $name );
				} elseif ( ! $overwrite ) {
					// Skip existing settings if not overwriting.
					continue;
				}

				// Sanitize and validate value.
				if ( is_array( $value ) ) {
					$value = array_map( 'wc_clean', $value );
				} else {
					$value = wc_clean( $value );
				}

				$setting->set_value( $value );
				$setting->save();

				$imported[] = $name;

			} catch ( Exception $e ) {
				$errors[ $name ] = $e->getMessage();
			}
		}

		return array(
			'imported'  => $imported,
			'errors'    => $errors,
			'total'     => count( $imported ),
			'overwrite' => $overwrite,
		);
	}

	/**
	 * Reset settings data.
	 *
	 * @since 3.0.0
	 *
	 * @param string $scope Reset scope.
	 *
	 * @return array Reset result.
	 */
	protected function reset_settings_data( string $scope ): array {
		$settings_store = dokan_kits_get_container()->get( SettingsDataStore::class );
		if ( 'all' === $scope ) {
			$settings_store->reset_by_names( dokan_kits()->get_settings_groups() );
			$message = __( 'All settings have been reset to defaults.', 'dokan-kits' );
		} else {
			$settings_store->reset_by_name( $scope );
			/* translators: %s: settings group name */
			$message = sprintf( __( '%s settings have been reset to defaults.', 'dokan-kits' ), ucfirst( $scope ) );
		}

		return array(
			'scope'     => $scope,
			'message'   => $message,
			'timestamp' => current_time( 'mysql' ),
		);
	}

	/**
	 * Get dashboard endpoint parameters.
	 *
	 * @since 3.0.0
	 *
	 * @return array Dashboard parameters.
	 */
	protected function get_dashboard_params(): array {
		$params = array(
			'include_statistics' => array(
				'description' => __( 'Include statistics in response.', 'dokan-kits' ),
				'type'        => 'boolean',
				'default'     => true,
			),
			'include_plugin'     => array(
				'description' => __( 'Include plugin data in response.', 'dokan-kits' ),
				'type'        => 'boolean',
				'default'     => true,
			),
		);

		/**
		 * Filter dashboard endpoint parameters.
		 *
		 * @since 3.0.0
		 *
		 * @param array $params Dashboard endpoint parameters.
		 */
		return apply_filters( 'dokan_kits_rest_dashboard_params', $params );
	}

	/**
	 * Get statistics endpoint parameters.
	 *
	 * @since 3.0.0
	 *
	 * @return array Statistics parameters.
	 */
	protected function get_statistics_params(): array {
		$params = array(
			'refresh' => array(
				'description' => __( 'Force refresh of cached statistics.', 'dokan-kits' ),
				'type'        => 'boolean',
				'default'     => false,
			),
		);

		/**
		 * Filter statistics endpoint parameters.
		 *
		 * @since 3.0.0
		 *
		 * @param array $params Statistics endpoint parameters.
		 */
		return apply_filters( 'dokan_kits_rest_statistics_params', $params );
	}

	/**
	 * Get export endpoint parameters.
	 *
	 * @since 3.0.0
	 *
	 * @return array Export parameters.
	 */
	protected function get_export_params(): array {
		$params = array(
			'format' => array(
				'description' => __( 'Export format.', 'dokan-kits' ),
				'type'        => 'string',
				'enum'        => array( 'json', 'array' ),
				'default'     => 'array',
			),
			'groups' => array(
				'description' => __( 'Settings groups to export.', 'dokan-kits' ),
				'type'        => 'array',
				'items'       => array(
					'type' => 'string',
					'enum' => dokan_kits()->get_settings_groups(),
				),
				'default'     => dokan_kits()->get_settings_groups(),
			),
		);

		/**
		 * Filter export endpoint parameters.
		 *
		 * @since 3.0.5
		 *
		 * @param array $params Export endpoint parameters.
		 */
		return apply_filters( 'dokan_kits_rest_export_params', $params );
	}

	/**
	 * Get import endpoint parameters.
	 *
	 * @since 3.0.0
	 *
	 * @return array Import parameters.
	 */
	protected function get_import_params(): array {
		$params = array(
			'settings'  => array(
				'description' => __( 'Settings data to import as key-value pairs.', 'dokan-kits' ),
				'type'        => 'object',
				'required'    => true,
				'properties'  => array(
					'additionalProperties' => array(
						'type' => array( 'string', 'number', 'boolean', 'array', 'object' ),
					),
				),
			),
			'overwrite' => array(
				'description' => __( 'Whether to overwrite existing settings.', 'dokan-kits' ),
				'type'        => 'boolean',
				'default'     => true,
			),
		);

		/**
		 * Filter import endpoint parameters.
		 *
		 * @since 3.0.5
		 *
		 * @param array $params Import endpoint parameters.
		 */
		return apply_filters( 'dokan_kits_rest_import_params', $params );
	}

	/**
	 * Get reset endpoint parameters.
	 *
	 * @since 3.0.0
	 *
	 * @return array Reset parameters.
	 */
	protected function get_reset_params(): array {
		$params = array(
			'scope'   => array(
				'description' => __( 'Scope of settings to reset.', 'dokan-kits' ),
				'type'        => 'string',
				'enum'        => array_merge( dokan_kits()->get_settings_groups(), array( 'all' ) ),
				'default'     => 'all',
			),
			'confirm' => array(
				'description' => __( 'Confirmation that you want to reset the settings.', 'dokan-kits' ),
				'type'        => 'boolean',
				'required'    => true,
			),
		);

		/**
		 * Filter reset endpoint parameters.
		 *
		 * @since 3.0.5
		 *
		 * @param array $params Reset endpoint parameters.
		 */
		return apply_filters( 'dokan_kits_rest_reset_params', $params );
	}

	/**
	 * Get dashboard schema.
	 *
	 * @since 3.0.0
	 *
	 * @return array Dashboard schema.
	 */
	public function get_dashboard_schema(): array {
		$schema = array(
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
			'title'      => 'dokan_kits_dashboard',
			'type'       => 'object',
			'properties' => array_merge(
				$this->get_standard_response_schema(),
				array(
					'data' => array(
						'description' => __( 'Dashboard data.', 'dokan-kits' ),
						'type'        => 'object',
						'properties'  => array(
							'settings_count' => array(
								'description' => __( 'Number of configured settings.', 'dokan-kits' ),
								'type'        => 'integer',
								'minimum'     => 0,
							),
							'last_updated'   => array(
								'description' => __( 'Last updated timestamp.', 'dokan-kits' ),
								'type'        => 'string',
								'format'      => 'date-time',
							),
							'statistics'     => array(
								'description' => __( 'Dashboard statistics.', 'dokan-kits' ),
								'type'        => 'object',
							),
							'plugin'         => array(
								'description' => __( 'Plugin information.', 'dokan-kits' ),
								'type'        => 'object',
							),
						),
					),
				)
			),
		);

		/**
		 * Filter dashboard endpoint schema.
		 *
		 * @since 3.0.5
		 *
		 * @param array $schema Dashboard schema.
		 */
		return apply_filters( 'dokan_kits_rest_dashboard_schema', $schema );
	}

	/**
	 * Get statistics schema.
	 *
	 * @since 3.0.0
	 *
	 * @return array Statistics schema.
	 */
	public function get_statistics_schema(): array {
		$schema = array(
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
			'title'      => 'dokan_kits_statistics',
			'type'       => 'object',
			'properties' => array_merge(
				$this->get_standard_response_schema(),
				array(
					'data' => array(
						'description'          => __( 'Statistics data with metrics and their metadata.', 'dokan-kits' ),
						'type'                 => 'object',
						'additionalProperties' => array(
							'type'       => 'object',
							'properties' => array(
								'value'       => array(
									'description' => __( 'Statistic value.', 'dokan-kits' ),
									'type'        => array( 'string', 'number' ),
								),
								'label'       => array(
									'description' => __( 'Human-readable label.', 'dokan-kits' ),
									'type'        => 'string',
								),
								'description' => array(
									'description' => __( 'Detailed description.', 'dokan-kits' ),
									'type'        => 'string',
								),
								'icon'        => array(
									'description' => __( 'Icon class or identifier.', 'dokan-kits' ),
									'type'        => 'string',
								),
								'color'       => array(
									'description' => __( 'Color theme or class.', 'dokan-kits' ),
									'type'        => 'string',
								),
							),
						),
					),
				)
			),
		);

		/**
		 * Filter statistics endpoint schema.
		 *
		 * @since 3.0.5
		 *
		 * @param array $schema Statistics schema.
		 */
		return apply_filters( 'dokan_kits_rest_statistics_schema', $schema );
	}

	/**
	 * Get export schema.
	 *
	 * @since 3.0.0
	 *
	 * @return array Export schema.
	 */
	public function get_export_schema(): array {
		$schema = array(
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
			'title'      => 'dokan_kits_export',
			'type'       => 'object',
			'properties' => array_merge(
				$this->get_standard_response_schema(),
				array(
					'data' => array(
						'description' => __( 'Export data.', 'dokan-kits' ),
						'type'        => 'object',
						'properties'  => array(
							'data'      => array(
								'description' => __( 'Exported settings data.', 'dokan-kits' ),
								'type'        => array( 'object', 'string' ),
							),
							'format'    => array(
								'description' => __( 'Export format used.', 'dokan-kits' ),
								'type'        => 'string',
								'enum'        => array( 'json', 'array' ),
							),
							'groups'    => array(
								'description' => __( 'Settings groups exported.', 'dokan-kits' ),
								'type'        => 'array',
								'items'       => array( 'type' => 'string' ),
							),
							'count'     => array(
								'description' => __( 'Number of exported settings.', 'dokan-kits' ),
								'type'        => 'integer',
								'minimum'     => 0,
							),
							'timestamp' => array(
								'description' => __( 'Export timestamp.', 'dokan-kits' ),
								'type'        => 'string',
								'format'      => 'date-time',
							),
						),
					),
				)
			),
		);

		/**
		 * Filter export endpoint schema.
		 *
		 * @since 3.0.5
		 *
		 * @param array $schema Export schema.
		 */
		return apply_filters( 'dokan_kits_rest_export_schema', $schema );
	}

	/**
	 * Get import schema.
	 *
	 * @since 3.0.0
	 *
	 * @return array Import schema.
	 */
	public function get_import_schema(): array {
		$schema = array(
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
			'title'      => 'dokan_kits_import',
			'type'       => 'object',
			'properties' => array_merge(
				$this->get_standard_response_schema(),
				array(
					'data' => array(
						'description' => __( 'Import result data.', 'dokan-kits' ),
						'type'        => 'object',
						'properties'  => array(
							'imported'  => array(
								'description' => __( 'List of successfully imported settings.', 'dokan-kits' ),
								'type'        => 'array',
								'items'       => array( 'type' => 'string' ),
							),
							'errors'    => array(
								'description'          => __( 'Import errors by setting name.', 'dokan-kits' ),
								'type'                 => 'object',
								'additionalProperties' => array( 'type' => 'string' ),
							),
							'total'     => array(
								'description' => __( 'Total number of successfully imported settings.', 'dokan-kits' ),
								'type'        => 'integer',
								'minimum'     => 0,
							),
							'overwrite' => array(
								'description' => __( 'Whether existing settings were overwritten.', 'dokan-kits' ),
								'type'        => 'boolean',
							),
						),
					),
				)
			),
		);

		/**
		 * Filter import endpoint schema.
		 *
		 * @since 3.0.5
		 *
		 * @param array $schema Import schema.
		 */
		return apply_filters( 'dokan_kits_rest_import_schema', $schema );
	}

	/**
	 * Get reset schema.
	 *
	 * @since 3.0.0
	 *
	 * @return array Reset schema.
	 */
	public function get_reset_schema(): array {
		$schema = array(
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
			'title'      => 'dokan_kits_reset',
			'type'       => 'object',
			'properties' => array_merge(
				$this->get_standard_response_schema(),
				array(
					'data' => array(
						'description' => __( 'Reset operation result.', 'dokan-kits' ),
						'type'        => 'object',
						'properties'  => array(
							'scope'     => array(
								'description' => __( 'Scope of settings that were reset.', 'dokan-kits' ),
								'type'        => 'string',
								'enum'        => array_merge( dokan_kits()->get_settings_groups(), array( 'all' ) ),
								'default'     => 'all',
							),
							'message'   => array(
								'description' => __( 'Detailed reset result message.', 'dokan-kits' ),
								'type'        => 'string',
							),
							'timestamp' => array(
								'description' => __( 'Reset operation timestamp.', 'dokan-kits' ),
								'type'        => 'string',
								'format'      => 'date-time',
							),
						),
					),
				)
			),
		);

		/**
		 * Filter reset endpoint schema.
		 *
		 * @since 3.0.5
		 *
		 * @param array $schema Reset schema.
		 */
		return apply_filters( 'dokan_kits_rest_reset_schema', $schema );
	}
}
