<?php
/**
 * User REST Controller (Standard Plan)
 *
 * @category   WordPress_Plugin
 * @package    Plans
 * @subpackage Standard\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\Standard\Controllers\Rest_API;

use MOWCPS\Common\Constants\Constants;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use MOWCPS\Common\Utils\Response_Builder;
use MOWCPS\Standard\Handlers\Rest_API\User_Handler;
use MOWCPS\Common\Controllers\Rest_API\User_Rest_Controller as Common_User_Rest_Controller;

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

/**
 * Class User_Rest_Controller
 *
 * Handles user registration and feedback for standard plan
 * Extends common controller to inherit login and contact_us functionality
 */
class User_Rest_Controller extends Common_User_Rest_Controller {
	/**
	 * Standard-specific handler instance
	 *
	 * @var User_Handler
	 */
	private $handler;

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

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

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

		if ( $password !== $confirm_password ) {
			return Response_Builder::error( 'Password and confirm password do not match.', Constants::HTTP_BAD_REQUEST );
		}

		if ( strlen( $password ) < 8 || strlen( $confirm_password ) < 8 ) {
			return Response_Builder::error( 'Password must be at least 8 characters long.', Constants::HTTP_BAD_REQUEST );
		}

		$result = $this->handler->register_user( $email, $password, $confirm_password, $phone );

		return $this->handle_api_response( $result, 'User registered successfully.' );
	}

	/**
	 * Handle user feedback submission
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function submit_feedback( $request ) {
		$reason          = sanitize_text_field( wp_unslash( $request->get_param( 'reason' ) ) );
		$comments        = sanitize_textarea_field( wp_unslash( $request->get_param( 'comments' ) ) );
		$user_email      = sanitize_email( wp_unslash( $request->get_param( 'user_email' ) ) );
		$custom_feedback = sanitize_textarea_field( wp_unslash( $request->get_param( 'custom_feedback' ) ) );
		$request_contact = (bool) $request->get_param( 'request_contact' );
		$user_config     = $request->get_param( 'user_config' );
		$plugin_version  = sanitize_text_field( wp_unslash( $request->get_param( 'plugin_version' ) ) );
		$timestamp       = sanitize_text_field( wp_unslash( $request->get_param( 'timestamp' ) ) );
		$source          = sanitize_text_field( wp_unslash( $request->get_param( 'source' ) ) );

		$form_data = array(
			'reason'          => $reason,
			'comments'        => $comments,
			'user_email'      => $user_email,
			'custom_feedback' => $custom_feedback,
			'request_contact' => $request_contact,
			'user_config'     => $user_config ? $user_config : array(),
			'plugin_version'  => $plugin_version,
			'timestamp'       => $timestamp,
			'source'          => $source,
		);

		$result = $this->handler->submit_feedback( $form_data );

		return $this->handle_api_response( $result, 'Feedback submitted successfully.' );
	}
}
