<?php

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\UserRepository;
use RadiusTheme\RadiusBooking\Resources\UserResource;
use RadiusTheme\RadiusBooking\Services\CustomerService;
use WP_REST_Request;
use RadiusTheme\RadiusBooking\Abstracts\BaseController;

/**
 * Api/Controllers/CustomerPanelController.php
 * CustomerPanel API controller
 */
class CustomerPanelController extends BaseController {

	public function __construct() {
		parent::__construct( new UserRepository() );
		$this->middleware[] = new AuthMiddleware();
		$this->middleware[] = new PermissionMiddleware( 'radius_booking_customer' );
	}

	protected function transformItem( $item ): array {
		return ( new UserResource() )->transform( $item );
	}

	protected function transformCollection( array $items ): array {
		return userResource::collection( $items );
	}

	/**
	 * Validation rules for create and update.
	 *
	 * @param string $context 'create' or 'update'
	 *
	 * @return array
	 */
	protected function getValidationRules( $request, string $context = 'create' ): array {
		$rules = [
			// Add your fields below
			// 'field_name' => [ new RequiredRule(), new IntegerRule() ],
		];

		if ( $context === 'update' ) {
			// Make some fields optional
			// unset( $rules['field_name'] );
		}

		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 "customer_panel";
	}

	public function customerDashboard( WP_REST_Request $request ) {
		return $this->applyMiddleware( $request, function ( $request ) {
			$customer_id      = $request->get_param( 'user_id' );
			$page             = $request->get_param( 'page' );
			$per_page         = $request->get_param( 'per_page' );
			$customer_service = Container::resolve( CustomerService::class );
			$dashboard_data   = $customer_service->getDashboardData( $customer_id, $page, $per_page );

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

	}

	public function customerProfile( WP_REST_Request $request ) {
		return $this->applyMiddleware( $request, function ( $request ) {
			$user_id = $request->get_param( 'user_id' );
			$employee_service = Container::resolve( CustomerService::class );
			$profile_data     = $employee_service->getProfileData( $user_id );
			return ApiResponse::success( $profile_data, 'Profile info fetch successfully' )->send();
		} );

	}

	public function customerUpdateProfile( WP_REST_Request $request ) {
		return $this->applyMiddleware( $request, function ( $request ) {
			$customer_service = Container::resolve( CustomerService::class );
			$profile_data     = $customer_service->updateProfileData( $request );
			return ApiResponse::success( $profile_data, 'Profile info updated successfully' )->send();
		} );

	}

	public function customerChangePassword( WP_REST_Request $request ) {
		return $this->applyMiddleware( $request, function ( $request ) {
			$customer_service = Container::resolve( CustomerService::class );
			$profile_data     = $customer_service->changePassword( $request );
			if( $profile_data['success'] === false ) {
				return ApiResponse::error( $profile_data['message'] ?? 'Password change failed' )->send();
			}
			return ApiResponse::success( $profile_data, 'Password changed successfully' )->send();
		} );
	}
}
