<?php
/**
 * Its Reward Point Module
 *
 * @package Vite_Reward_Lite\Modules
 */

namespace Vite_Reward_Lite\Modules;

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

use Appsbd_Lite\V5\libs\API_Response;
use Appsbd_Lite\V5\libs\AppInput;
use Vite_Reward_Lite\Core\Vite_Reward_Module_Lite;
use Vite_Reward_Lite\Libs\Vite_Rule_Group;
use Vite_Reward_Lite\Libs\Vite_Rule_Item;

/**
 * Class User_Reward
 *
 * @package Vite_Reward_Lite\Modules
 */
class Vite_User_Reward extends Vite_Reward_Module_Lite {

	/**
	 * The initialize is generated by appsbd
	 */
	public function initialize() {
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
		add_filter( 'appsbd/vite-reward/filter/rules', array( $this, 'set_rules' ) );
		add_action( 'user_register', array( $this, 'post_user_reg_redirect' ), 10, 1 );

		/**
		 * It's for check
		 * add_action( 'appsbd/vite-reward/action/update-rule-user', array( $this, 'update_rule' ) );
		 * */
	}

	/**
	 * The post user reg redirect is generated by appsbd
	 *
	 * @param mixed $user_id registered user id.
	 */
	public function post_user_reg_redirect( $user_id ) {
		$user = get_userdata( $user_id );
		if ( 'subscriber' == $user->roles[0] || 'customer' == $user->roles[0] ) {
			$found_rule = Vite_Reward_Rules::get_active_rule( 'SU' );
			if ( ! empty( $found_rule ) ) {
				$this->user_reg_action( $found_rule, $user );
			}
		}
	}
	/**
	 * The per order action is generated by appsbd
	 *
	 * @param mixed    $found_rule Founded Rule.
	 * @param \WP_User $user User.
	 */
	public function user_reg_action( $found_rule, &$user ) {
		$rule_points = $found_rule->points;
		if ( $rule_points > 0 ) {
			$ref_val = 0;
			if ( ! empty( $user->ID ) ) {
				if ( get_current_user_id() != $user->ID ) {
					$ref_val = get_current_user_id();
				}
				Vite_Reward_Settings::update_user_points( $user->ID, 'U', $ref_val, $rule_points, $this->__( 'By user registration' ) );
			}
		}
	}
	/**
	 * The update rule is generated by appsbd
	 *
	 * @param API_Response $response Its the response.
	 */
	public function update_rule( $response ) {
		$data = AppInput::get_posted_data();
		Vite_Rule_Group::save_rules_data( $data );
		$this->add_info( 'Successfully updated' );
		$response->set_response( true );
	}
	/**
	 * The set rules is generated by appsbd
	 *
	 * @param mixed $rules Its the rules.
	 *
	 * @return mixed
	 */
	public function set_rules( $rules ) {

		$sign_up     = new Vite_Rule_Item( 'SU', $this->__( 'User Sign Up' ), $this->__( 'Reward new users with points upon registration, encouraging them to join your store.' ), 'N' );
		$daily_up    = new Vite_Rule_Item( 'DL', $this->__( 'Daily Login' ), $this->__( 'Incentivize regular visits by rewarding users with points for logging in daily.' ), 'R' );
		$ref_point   = new Vite_Rule_Item( 'RP', $this->__( 'Referral Points' ), $this->__( 'Encourage customers to refer friends by offering points for successful referrals.' ), 'R' );
		$scl_sharing = new Vite_Rule_Item( 'SC', $this->__( 'Social Sharing Points' ), $this->__( 'If a user share their views or experience as feedbacks, he/she will get this reward.' ), 'R' );

		$group = new Vite_Rule_Group( 'user', $this->__( 'User Rules' ) );
		$group->add_rule_item( $sign_up );
		$group->add_rule_item( $daily_up );

		$rules = array_merge( $rules, $group->get_rules() );
		return $rules;
	}
}
