<?php
/**
 * User REST Controller
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Controllers\Rest_API
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Controllers\Rest_API;

use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Abstracts\Base_Rest_Controller;
use MOWCPS\Common\Utils\Response_Builder;
use MOWCPS\Common\Handlers\Rest_API\User_Handler;

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

/**
 * Class User_Rest_Controller
 *
 * Handles user authentication and basic user operations
 */
class User_Rest_Controller extends Base_Rest_Controller {

	/**
	 * User handler instance
	 *
	 * @var User_Handler
	 */
	private $handler;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->handler = new User_Handler();
	}

	/**
	 * Handle user login
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function login( $request ) {
		$email    = sanitize_email( wp_unslash( $request->get_param( 'email' ) ) );
		$password = wp_unslash( $request->get_param( 'password' ) );

		if ( empty( $email ) || empty( $password ) ) {
			return Response_Builder::error( Constants::HTTP_BAD_REQUEST, 'Email and password are required.' );
		}

		$result = $this->handler->login( $email, $password );
		return $this->handle_api_response( $result );
	}

	/**
	 * Handle user contact us
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function contact_us( $request ) {
		$email        = sanitize_email( wp_unslash( $request->get_param( 'email' ) ) );
		$phone        = sanitize_text_field( wp_unslash( $request->get_param( 'phone' ) ) );
		$query        = sanitize_textarea_field( wp_unslash( $request->get_param( 'query' ) ) );
		$request_type = sanitize_text_field( wp_unslash( $request->get_param( 'request_type' ) ) );

		if ( empty( $email ) || empty( $query ) ) {
			return Response_Builder::error( 'Email and query are required.', Constants::HTTP_BAD_REQUEST );
		}

		$result = $this->handler->contact_us( $email, $phone, $query, $request_type );
		return $this->handle_api_response( $result );
	}

	/**
	 * Handle user trial request
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function trial_request( $request ) {
		$email         = sanitize_email( wp_unslash( $request->get_param( 'email' ) ) );
		$suppliers     = array_map( 'sanitize_text_field', wp_unslash( $request->get_param( 'suppliers' ) ) );
		$documentation = sanitize_text_field( wp_unslash( $request->get_param( 'documentation' ) ) );
		$use_case      = sanitize_textarea_field( wp_unslash( $request->get_param( 'use_case' ) ) );

		if ( empty( $email ) ) {
			return Response_Builder::error( 'Email is required.', Constants::HTTP_BAD_REQUEST );
		}

		if ( empty( $suppliers ) ) {
			return Response_Builder::error( 'Suppliers are required.', Constants::HTTP_BAD_REQUEST );
		}

		$result = $this->handler->trial_request( $email, $suppliers, $documentation, $use_case );
		return $this->handle_api_response( $result );
	}

	/**
	 * Handle user details
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function get_user_details( $request ) {
		$result = $this->handler->get_user_details();
		return $this->handle_api_response( $result );
	}
}
