<?php
/**
 * Setup Wizard Controller.
 *
 * Handles REST API endpoints for the setup wizard.
 *
 * @package RadiusTheme\RadiusBooking\Controllers
 * @since 1.0.0
 */

namespace RadiusTheme\RadiusBooking\Controllers;

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

use RadiusTheme\RadiusBooking\Abstracts\BaseController;
use RadiusTheme\RadiusBooking\Core\Api\ApiResponse;
use RadiusTheme\RadiusBooking\Core\Api\Middleware\PermissionMiddleware;
use RadiusTheme\RadiusBooking\Core\Container\Container;
use RadiusTheme\RadiusBooking\Repositories\SettingsRepository;
use RadiusTheme\RadiusBooking\Services\SetupWizardService;
use WP_REST_Request;

/**
 * Class SetupWizardController
 */
class SetupWizardController extends BaseController {

	/**
	 * Wizard service instance.
	 *
	 * @var SetupWizardService
	 */
	private SetupWizardService $wizardService;

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct( Container::resolve( SettingsRepository::class ) );
		$this->wizardService = Container::resolve( SetupWizardService::class );
		$this->middleware[]  = new PermissionMiddleware( 'manage_options' );
	}

	/**
	 * Get wizard status and current settings state.
	 *
	 * @param WP_REST_Request $request REST request.
	 *
	 * @return \WP_REST_Response
	 */
	public function status( WP_REST_Request $request ) {
		return $this->applyMiddleware(
			$request,
			function () {
				try {
					$state = $this->wizardService->getWizardState();
					return ApiResponse::success(
						$state,
						__( 'Wizard state retrieved successfully.', 'radius-booking' )
					)->send();
				} catch ( \Exception $e ) {
					return ApiResponse::error( $e->getMessage(), 500 )->send();
				}
			}
		);
	}

	/**
	 * Save a single wizard step.
	 *
	 * @param WP_REST_Request $request REST request.
	 *
	 * @return \WP_REST_Response
	 */
	public function saveStep( WP_REST_Request $request ) {
		return $this->applyMiddleware(
			$request,
			function ( $request ) {
				try {
					$step = $request->get_param( 'step' );
					$data = $request->get_json_params();

					$result = $this->wizardService->saveStep( $step, $data );

					if ( ! $result ) {
						return ApiResponse::error(
							__( 'Invalid wizard step.', 'radius-booking' ),
							400
						)->send();
					}

					return ApiResponse::success(
						array(
							'step'  => $step,
							'saved' => true,
						),
						__( 'Step saved successfully.', 'radius-booking' )
					)->send();
				} catch ( \Exception $e ) {
					return ApiResponse::error( $e->getMessage(), 500 )->send();
				}
			}
		);
	}

	/**
	 * Mark setup as complete.
	 *
	 * @param WP_REST_Request $request REST request.
	 *
	 * @return \WP_REST_Response
	 */
	public function complete( WP_REST_Request $request ) {
		return $this->applyMiddleware(
			$request,
			function () {
				try {
					$this->wizardService->completeSetup();
					return ApiResponse::success(
						array( 'setup_completed' => true ),
						__( 'Setup completed successfully.', 'radius-booking' )
					)->send();
				} catch ( \Exception $e ) {
					return ApiResponse::error( $e->getMessage(), 500 )->send();
				}
			}
		);
	}

	/**
	 * Create current admin user as staff member.
	 *
	 * @param WP_REST_Request $request REST request.
	 *
	 * @return \WP_REST_Response
	 */
	public function createStaff( WP_REST_Request $request ) {
		return $this->applyMiddleware(
			$request,
			function () {
				try {
					$result = $this->wizardService->createAdminAsStaff();

					if ( ! $result['success'] ) {
						return ApiResponse::error( $result['message'], 400 )->send();
					}

					return ApiResponse::success(
						$result,
						$result['message']
					)->send();
				} catch ( \Exception $e ) {
					return ApiResponse::error( $e->getMessage(), 500 )->send();
				}
			}
		);
	}

	/**
	 * Transform a single item for API response.
	 *
	 * @param mixed $item The item to transform.
	 *
	 * @return array
	 */
	protected function transformItem( $item ): array {
		return is_array( $item ) ? $item : array();
	}

	/**
	 * Transform a collection of items for API response.
	 *
	 * @param array $items The items to transform.
	 *
	 * @return array
	 */
	protected function transformCollection( array $items ): array {
		return $items;
	}

	/**
	 * Get the resource type identifier.
	 *
	 * @return string
	 */
	protected function getResourceType(): string {
		return 'setup-wizard';
	}

	/**
	 * Get validation rules for the resource.
	 *
	 * @param mixed  $request The request object.
	 * @param string $context The validation context.
	 *
	 * @return array
	 */
	protected function getValidationRules( $request, string $context = 'create' ) {
		return array();
	}
}