<?php

namespace WPDRMS\ASP\Maintenance;

use Exception;
use WP_Error;
use WP_REST_Response;
use WPDRMS\PluginCore\Rest\AbstractRest;

class MaintenanceRoute extends AbstractRest {
	public function registerRoutes( string $route_namespace ): void {
		register_rest_route(
			$route_namespace,
			'maintenance/reset',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'reset' ),
				'permission_callback' => array( $this, 'allowOnlyAdmins' ),
			)
		);

		register_rest_route(
			$route_namespace,
			'maintenance/wipe',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'wipe' ),
				'permission_callback' => array( $this, 'allowOnlyAdmins' ),
			)
		);
	}

	/**
	 * @return WP_Error|WP_REST_Response
	 */
	public function reset() {
		try {
			wd_asp()->init->pluginReset();
			return new WP_REST_Response(
				array(
					'success' => true,
					'message' => 'Plugin data reset successfully.',
				),
				200
			);
		} catch ( Exception $e ) {
			return new WP_Error( 'asp_maintenance_error', $e->getMessage(), array( 'status' => 500 ) );
		}
	}

	/**
	 * @return WP_Error|WP_REST_Response
	 */
	public function wipe() {
		try {
			wd_asp()->init->pluginWipe();
			return new WP_REST_Response(
				array(
					'success' => true,
					'message' => 'Plugin wiped. Redirecting...',
				),
				200
			);
		} catch ( Exception $e ) {
			return new WP_Error( 'asp_maintenance_error', $e->getMessage(), array( 'status' => 500 ) );
		}
	}
}
