<?php
/**
 * Api/Controllers/CategoryController.php
 * Handles API operations related to categories.
 * Manages resource transformation, validation, and data retrieval for category entities.
 */

namespace RadiusTheme\RadiusBooking\Controllers;

use RadiusTheme\RadiusBooking\Core\Api\ApiResponse;
use RadiusTheme\RadiusBooking\Core\Api\Middleware\AuthMiddleware;
use RadiusTheme\RadiusBooking\Core\Api\Middleware\PermissionMiddleware;
use RadiusTheme\RadiusBooking\Core\Container\Container;
use RadiusTheme\RadiusBooking\Repositories\ServiceRepository;
use WP_REST_Request;
use RadiusTheme\RadiusBooking\Abstracts\BaseController;
use RadiusTheme\RadiusBooking\Core\Api\Validation\Rules\RequiredRule;
use RadiusTheme\RadiusBooking\Repositories\CategoryRepository;
use RadiusTheme\RadiusBooking\Resources\CategoryResource;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Api/Controllers/CategoryController.php
 * Category API controller
 */
class CategoryController extends BaseController {

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

	/**
	 * Transforms the given item into an array representation.
	 *
	 * @param mixed $item The item to be transformed.
	 *
	 * @return array The transformed representation of the item.
	 */
	protected function transformItem( $item ): array {
		return ( new CategoryResource() )->transform( $item );
	}

	/**
	 * Transforms a collection of items into a formatted resource collection.
	 *
	 * @param array $items The collection of items to be transformed.
	 *
	 * @return array The transformed resource collection.
	 */
	protected function transformCollection( array $items ): array {
		return CategoryResource::collection( $items );
	}

	/**
	 * Retrieves the validation rules based on the provided request and context.
	 *
	 * @param mixed $request The incoming request containing data to be validated.
	 * @param string $context The context in which validation is applied (e.g., 'create', 'update').
	 *
	 * @return array An array of validation rules to be applied.
	 */
	protected function getValidationRules( $request, string $context = 'create' ): array {
		$rules = array(
			// Add your fields below
			'name' => array( new RequiredRule() ),
		);

		return $rules;
	}
	/**
	 * Retrieves the resource type for this controller.
	 *
	 * @return string The resource type, which is 'user' in this case.
	 */
	protected function getResourceType(): string {
		return 'category';
	}

	/**
	 * Handles the retrieval of all categories and processes the request through middleware.
	 *
	 * @param WP_REST_Request $request The REST request instance containing query parameters and other request data.
	 *
	 * @return mixed The response after processing the middleware and retrieving categories.
	 */
	public function getAllCategory( WP_REST_Request $request ) {
		return $this->applyMiddleware(
			$request,
			function () {
				$item = $this->repository->all();

				if ( ! $item ) {
					return ApiResponse::notFound()->send();
				}
				/**
				 * Filter the single item before returning it.
				 */

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

}
