<?php
/**
 * Class PaymentController
 *
 * This class handles API requests related to payment management in the system.
 * It extends the BaseController class and provides functionality specific to
 * payments, including data transformation, validation, and dashboard data retrieval.
 */

namespace RadiusTheme\RadiusBooking\Controllers;

use RadiusTheme\RadiusBooking\Core\Api\ApiResponse;
use RadiusTheme\RadiusBooking\Core\Container\Container;
use WP_Error;
use WP_REST_Request;
use RadiusTheme\RadiusBooking\Abstracts\BaseController;
use RadiusTheme\RadiusBooking\Repositories\PaymentRepository;
use RadiusTheme\RadiusBooking\Resources\PaymentResource;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Api/Controllers/PaymentController.php
 * Payment API controller
 */
class PaymentController extends BaseController {

	/**
	 * Constructor for initializing the controller with a PaymentRepository instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct( new PaymentRepository() );
	}

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

	/**
	 * Transforms a collection of items into a collection of PaymentResource instances.
	 *
	 * @param array $items The array of items to be transformed.
	 *
	 * @return array The transformed collection as an array of PaymentResource instances.
	 */
	protected function transformCollection( array $items ): array {
		return PaymentResource::collection( $items );
	}

	/**
	 * Retrieves the validation rules based on the given request and context.
	 *
	 * @param mixed $request The incoming request containing data to validate.
	 * @param string $context The context for the validation rules, such as 'create' or 'update'. Default is 'create'.
	 *
	 * @return array An array of validation rules corresponding to the specified context.
	 */
	protected function getValidationRules( $request, string $context = 'create', ): array {
		$rules = array(
			// Add your fields below
		);

		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 'payment';
	}

	/**
	 * GET /payments — List paginated payments with multi-column search.
	 *
	 * @param WP_REST_Request $request The current REST request.
	 *
	 * @return WP_REST_Response
	 */
	public function index( WP_REST_Request $request ) {
		return $this->applyMiddleware(
			$request,
			function ( $request ) {
				$page    = $request->get_param( 'page' ) ? (int) $request->get_param( 'page' ) : 1;
				$perPage = $request->get_param( 'per_page' ) ? (int) $request->get_param( 'per_page' ) : 15;
				$orderBy = $request->get_param( 'order_by' ) ? $request->get_param( 'order_by' ) : 'created_at';
				$order   = $request->get_param( 'order' ) ? $request->get_param( 'order' ) : 'desc';
				$search  = $request->get_param( 'search' ) ? sanitize_text_field( $request->get_param( 'search' ) ) : null;
				$status  = $request->get_param( 'status' ) ? sanitize_text_field( $request->get_param( 'status' ) ) : null;
				$type    = $request->get_param( 'type' ) ? sanitize_text_field( $request->get_param( 'type' ) ) : null;

				$result = $this->repository->paginateWithSearch( $perPage, $page, $order, $orderBy, $search, $status, $type );

				$items = apply_filters( 'radius_booking_index_items_payment', $result->items, $this );

				return ApiResponse::success(
					$this->transformCollection( $items ),
					null,
					array(
						'pagination' => array(
							'current_page'   => $result->currentPage,
							'last_page'      => $result->lastPage,
							'per_page'       => $result->perPage,
							'total'          => $result->total,
							'has_more_pages' => $result->hasMorePages,
						),
					)
				)->send();
			}
		);
	}

	/**
	 * Retrieves dashboard data for the API response.
	 *
	 * @param WP_REST_Request $request The request object containing request parameters.
	 *
	 * @return mixed The processed response containing dashboard data with additional currency information.
	 */
	public function getDashboard( WP_REST_Request $request ) {
		return $this->applyMiddleware(
			$request,
			function () {
				$paymentRepository         = Container::resolve( PaymentRepository::class );
				$dashboardData             = $paymentRepository->getDashboardData();
				$dashboardData['currency'] = array(
					'currency_symbol' => get_rtrb_currency_symbol(),
					'currency'        => get_rtrb_currency(),
				);
				return ApiResponse::success(
					$dashboardData,
				)->send();
			}
		);
	}

}
