<?php
/**
 * EventRegistrationController
 *
 * Handles public event registration endpoints (register, cancel, lookup).
 * Does not extend BaseController — no standard CRUD needed.
 * All methods are guarded by rtrb_is_module_active('event').
 *
 * @package RadiusTheme\RadiusBooking\Controllers
 * @since   1.3.0
 */

namespace RadiusTheme\RadiusBooking\Controllers;

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

use RadiusTheme\RadiusBooking\Core\Api\ApiResponse;
use RadiusTheme\RadiusBooking\Core\Api\Middleware\RateLimitMiddleware;
use RadiusTheme\RadiusBooking\Core\Container\Container;
use RadiusTheme\RadiusBooking\Repositories\EventRegistrationRepository;
use RadiusTheme\RadiusBooking\Services\EventRegistrationService;
use WP_REST_Controller;
use WP_REST_Request;

/**
 * Class EventRegistrationController
 */
class EventRegistrationController extends WP_REST_Controller {

	/**
	 * Register for an event (public, rate-limited).
	 *
	 * @param WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response|\WP_Error
	 */
	public function register( WP_REST_Request $request ) {
		if ( ! rtrb_is_module_active( 'event' ) ) {
			return ApiResponse::error( esc_html__( 'Event module is not active.', 'radius-booking' ), 403 )->send();
		}

		// Rate limit: 10 requests per minute per IP.
		$rate_limit = new RateLimitMiddleware( 10, 60 );
		$check      = $rate_limit->handle(
			$request,
			function ( $req ) {
				return true;
			}
		);

		if ( true !== $check ) {
			return ApiResponse::error( esc_html__( 'Too many requests. Please slow down.', 'radius-booking' ), 429 )->send();
		}

		$data = $request->get_params();
		$slug = sanitize_text_field( $data['slug'] ?? '' );
		unset( $data['slug'] );

		try {
			$service = Container::resolve( EventRegistrationService::class );
			$result  = $service->registerForEvent( $slug, $data );

			return ApiResponse::created( $result, esc_html__( 'Registration successful.', 'radius-booking' ) )->send();
		} catch ( \Exception $e ) {
			return ApiResponse::error( $e->getMessage(), 400 )->send();
		}
	}

	/**
	 * Cancel an event registration (public).
	 *
	 * @param WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response|\WP_Error
	 */
	public function cancel( WP_REST_Request $request ) {
		if ( ! rtrb_is_module_active( 'event' ) ) {
			return ApiResponse::error( esc_html__( 'Event module is not active.', 'radius-booking' ), 403 )->send();
		}

		$data = $request->get_params();

		try {
			$service = Container::resolve( EventRegistrationService::class );
			$result  = $service->cancelRegistration( $data );

			return ApiResponse::success( $result, esc_html__( 'Registration cancelled successfully.', 'radius-booking' ) )->send();
		} catch ( \Exception $e ) {
			return ApiResponse::error( $e->getMessage(), 400 )->send();
		}
	}

	/**
	 * Get a registration by registration number (public).
	 *
	 * @param WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response|\WP_Error
	 */
	public function getByRegistrationNumber( WP_REST_Request $request ) {
		if ( ! rtrb_is_module_active( 'event' ) ) {
			return ApiResponse::error( esc_html__( 'Event module is not active.', 'radius-booking' ), 403 )->send();
		}

		$registration_number = sanitize_text_field( $request->get_param( 'registration_number' ) );

		if ( empty( $registration_number ) ) {
			return ApiResponse::validationError(
				array( 'registration_number' => esc_html__( 'Registration number is required.', 'radius-booking' ) )
			)->send();
		}

		$repo         = Container::resolve( EventRegistrationRepository::class );
		$registration = $repo->findByRegistrationNumber( $registration_number );

		if ( ! $registration ) {
			return ApiResponse::notFound( esc_html__( 'Registration not found.', 'radius-booking' ) )->send();
		}

		return ApiResponse::success( $registration )->send();
	}
}
