<?php
/**
 * Vite Reward Customer Modulr
 *
 * @package Vite_Reward_Lite\Libs
 */

namespace Vite_Reward_Lite\Libs;

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

use Appsbd_Lite\V5\Core\BaseModel;

/**
 * Class Vite_Reward_Customer
 *
 * @package Vite_Reward_Lite\Libs
 */
class Vite_Reward_Customer extends BaseModel {
	/**
	 * The set search param is generated by appsbd
	 *
	 * @param mixed $src_props It is src_props param.
	 * @param mixed $filter_param It is filter_param param.
	 */
	public static function set_search_param( $src_props, &$filter_param ) {
		foreach ( $src_props as $src_prop ) {
			if ( ! empty( $src_prop['prop'] ) && isset( $src_prop['val'] ) ) {
				if ( ! empty( $src_prop['val'] ) && is_string( $src_prop['val'] ) ) {
					$src_prop['val'] = trim( $src_prop['val'] );
				}
				if ( ! empty( $src_prop['prop'] ) && is_string( $src_prop['prop'] ) ) {
					$src_prop['prop'] = trim( $src_prop['prop'] );
				}
				if ( 'username' == $src_prop['prop'] ) {
					$filter_param['search']         = $src_prop['val'];
					$filter_param['search_columns'] = array( 'user_login', 'user_email' );

				} elseif ( '*' == $src_prop['prop'] ) {
					add_action( 'pre_get_users', '\Vite_Reward_Lite\Libs\Vite_Reward_Customer::user_meta_or_search', 9999 );
					$filter_param['_meta_or_search'] = esc_attr( $src_prop['val'] );
					$filter_param['search_columns']  = array( 'user_login', 'user_email', 'first_name', 'last_name' );

					$first_name = explode( ' ', $src_prop['val'] );
					if ( ! empty( $first_name[0] ) ) {
						$first_name = $first_name[0];
					} else {
						$first_name = $src_prop['val'];
					}

					$filter_param['meta_query'] = array(
						'relation' => 'OR',
						array(
							'key'     => 'billing_phone',
							'value'   => $src_prop['val'],
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'first_name',
							'value'   => $first_name,
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'last_name',
							'value'   => $src_prop['val'],
							'compare' => 'LIKE',
						),

					);
				} elseif ( 'email' == $src_prop['prop'] ) {
					$filter_param['search']         = $src_prop['val'];
					$filter_param['search_columns'] = array( 'user_email' );
				} elseif ( in_array( $src_prop['prop'], array( 'first_name', 'last_name' ) ) && ! empty( $src_prop['val'] ) ) {
					$filter_param['meta_key']     = $src_prop['prop'];
					$filter_param['meta_query'][] = array(
						'key'     => $src_prop['prop'],
						'value'   => $src_prop['val'],
						'compare' => 'LIKE',
					);
				} elseif ( 'outlet_id' == $src_prop['prop'] ) {
					$filter_param['meta_key']     = $src_prop['prop'];
					$filter_param['meta_query'][] = array(
						'key'     => $src_prop['prop'],
						'value'   => '"(' . $src_prop['val'] . ')"',
						'compare' => 'REGEXP',
					);
				}
			}
		}
	}

	/**
	 * The set sort param is generated by appsbd
	 *
	 * @param mixed $props It is props param.
	 * @param mixed $sort_param It is sort_param param.
	 */
	public static function set_sort_param( $props, &$sort_param ) {
		foreach ( $props as $prop ) {
			if ( ! empty( $prop['prop'] ) ) {
				$prop['prop'] = strtolower( trim( $prop['prop'] ) );
				$prop['ord']  = strtolower( trim( $prop['ord'] ) );
				if ( in_array( $prop['ord'], array( 'asc', 'desc' ) ) ) {
					$sort_param['orderby'] = $prop['prop'];
					$sort_param['order']   = $prop['ord'];
				}
			}
		}
	}

	/**
	 * The user meta or search is generated by appsbd
	 *
	 * @param \WP_User_Query $q Its query object of user.
	 */
	public static function user_meta_or_search( $q ) {
		$search = $q->get( '_meta_or_search' );
		if ( ! empty( $search ) ) {
			add_filter(
				'get_meta_sql',
				function ( $sql ) use ( $search ) {
					$appdb = self::get_db_object();

					static $nr = 0;
					if ( 0 != $nr++ ) {
						return $sql;
					}
					$where        = sprintf(
						' AND ( %s OR %s OR %s ) ',
						$appdb->prepare( "{$appdb->users}.user_nicename like '%%%s%%'", $search ),
						$appdb->prepare( "{$appdb->users}.user_email like '%%%s%%'", $search ),
						mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
					);
					$sql['where'] = $where;
					return $sql;
				}
			);
		}
	}
}
