<?php
/**
 * EventTicketController
 *
 * Handles admin CRUD for event ticket types.
 * Does not extend BaseController — uses direct service/repo calls.
 * 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\PermissionMiddleware;
use RadiusTheme\RadiusBooking\Core\Container\Container;
use RadiusTheme\RadiusBooking\Repositories\EventTicketTypeRepository;
use WP_REST_Controller;
use WP_REST_Request;

/**
 * Class EventTicketController
 */
class EventTicketController extends WP_REST_Controller {

	/**
	 * Middleware stack for request processing.
	 *
	 * @var array
	 */
	protected array $middleware = array();

	/**
	 * EventTicketController constructor.
	 */
	public function __construct() {
		$this->middleware[] = new PermissionMiddleware( 'manage_options' );
	}

	/**
	 * Applies middleware stack to a request.
	 *
	 * @param WP_REST_Request $request The current request.
	 * @param callable        $handler Final handler after middleware.
	 *
	 * @return mixed
	 */
	protected function applyMiddleware( WP_REST_Request $request, callable $handler ) {
		$middlewareStack = array_reverse( $this->middleware );
		$next            = $handler;

		foreach ( $middlewareStack as $middleware ) {
			$next = function ( $request ) use ( $middleware, $next ) {
				return $middleware->handle( $request, $next );
			};
		}

		return $next( $request );
	}

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

		return $this->applyMiddleware(
			$request,
			function ( $request ) {
				$event_id = (int) $request->get_param( 'event_id' );

				$repo    = Container::resolve( EventTicketTypeRepository::class );
				$tickets = $repo->getByEventId( $event_id );

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

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

		return $this->applyMiddleware(
			$request,
			function ( $request ) {
				$data = $request->get_params();

				try {
					$repo   = Container::resolve( EventTicketTypeRepository::class );
					$ticket = $repo->create( $data );

					return ApiResponse::created( $ticket, esc_html__( 'Ticket type created successfully.', 'radius-booking' ) )->send();
				} catch ( \Exception $e ) {
					return ApiResponse::error( $e->getMessage(), 500 )->send();
				}
			}
		);
	}

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

		return $this->applyMiddleware(
			$request,
			function ( $request ) {
				$id   = (int) $request->get_param( 'id' );
				$data = $request->get_params();

				try {
					$repo = Container::resolve( EventTicketTypeRepository::class );

					if ( ! $repo->exists( $id ) ) {
						return ApiResponse::notFound( esc_html__( 'Ticket type not found.', 'radius-booking' ) )->send();
					}

					$repo->update( $id, $data );
					$ticket = $repo->find( $id );

					return ApiResponse::success( $ticket, esc_html__( 'Ticket type updated successfully.', 'radius-booking' ) )->send();
				} catch ( \Exception $e ) {
					return ApiResponse::error( $e->getMessage(), 500 )->send();
				}
			}
		);
	}

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

		return $this->applyMiddleware(
			$request,
			function ( $request ) {
				$id = (int) $request->get_param( 'id' );

				try {
					$repo = Container::resolve( EventTicketTypeRepository::class );

					if ( ! $repo->exists( $id ) ) {
						return ApiResponse::notFound( esc_html__( 'Ticket type not found.', 'radius-booking' ) )->send();
					}

					$repo->delete( $id );

					return ApiResponse::success( array(), esc_html__( 'Ticket type deleted successfully.', 'radius-booking' ) )->send();
				} catch ( \Exception $e ) {
					return ApiResponse::error( $e->getMessage(), 500 )->send();
				}
			}
		);
	}
}
