<?php
/** REST endpoints for license status and lifecycle. */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/** Map private license failures to intentional public HTTP semantics. */
function wpagent_companion_license_rest_error_status( WP_Error $error ): int {
	$code = $error->get_error_code();
	if ( in_array( $code, array( 'wpagent_license_bad_key' ), true ) ) {
		return 400;
	}
	if ( in_array( $code, array( 'wpagent_license_missing', 'wpagent_license_activation_exists' ), true ) ) {
		return 409;
	}
	if ( in_array( $code, array(
		'wpagent_license_invalid',
		'wpagent_license_activation_mismatch',
		'wpagent_license_not_granted',
		'wpagent_license_expired',
		'wpagent_license_wrong_organization',
		'wpagent_license_wrong_benefit',
	), true ) ) {
		return 403;
	}
	if ( in_array( $code, array( 'wpagent_license_network', 'wpagent_license_temporarily_unavailable' ), true ) ) {
		return 503;
	}
	return 502;
}

/** Add REST status data without changing the stable, machine-readable error code. */
function wpagent_companion_license_rest_error( WP_Error $error ): WP_Error {
	$data = $error->get_error_data();
	$data = is_array( $data ) ? $data : array();
	if ( ! isset( $data['status'] ) ) {
		$data['status'] = wpagent_companion_license_rest_error_status( $error );
		$error->add_data( $data );
	}
	return $error;
}

function wpagent_companion_register_license_routes(): void {
	register_rest_route(
		'wp-agent/v1',
		'/license',
		array(
			'methods'             => 'GET',
			'permission_callback' => static fn() => current_user_can( 'edit_posts' ),
			'callback'            => static fn() => rest_ensure_response( wpagent_companion_license_public_status() ),
		)
	);

	foreach ( array( 'activate', 'refresh', 'deactivate' ) as $action ) {
		$route = array(
			'methods'             => 'POST',
			'permission_callback' => static fn() => current_user_can( 'manage_options' ),
			'callback'            => 'wpagent_companion_license_rest_' . $action,
		);
		if ( 'activate' === $action ) {
			$route['args'] = array(
				'key' => array(
					'required' => true,
					'type'     => 'string',
				),
			);
		}
		register_rest_route(
			'wp-agent/v1',
			'/license/' . $action,
			$route
		);
	}
}
add_action( 'rest_api_init', 'wpagent_companion_register_license_routes' );

function wpagent_companion_license_rest_activate( WP_REST_Request $request ) {
	$key = $request->get_param( 'key' );
	if ( ! is_string( $key ) ) {
		return wpagent_companion_license_rest_error(
			new WP_Error( 'wpagent_license_bad_key', __( 'Der Lizenzschlüssel hat kein gültiges Format.', 'wp-agent-companion' ) )
		);
	}
	$result = wpagent_companion_license_activate( $key );
	return is_wp_error( $result ) ? wpagent_companion_license_rest_error( $result ) : rest_ensure_response( wpagent_companion_license_public_status() );
}

function wpagent_companion_license_rest_refresh() {
	$result = wpagent_companion_license_refresh();
	return is_wp_error( $result ) ? wpagent_companion_license_rest_error( $result ) : rest_ensure_response( wpagent_companion_license_public_status() );
}

function wpagent_companion_license_rest_deactivate() {
	$result = wpagent_companion_license_deactivate();
	return is_wp_error( $result ) ? wpagent_companion_license_rest_error( $result ) : new WP_REST_Response( null, 204 );
}
