<?php
/**
 * Pos Warehouse Database Model
 *
 * @package Vite_Reward_Lite\Models\Database
 */

namespace Vite_Reward_Lite\Models\Database;

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

use AIOSEO\Vendor\Monolog\Handler\NullHandler;
use Vite_Reward_Lite\Core\Vite_Reward_Lite_Model;

/**
 * Class Mapbd_pos_cash_drawer_log
 *
 * @package Vitepos\Models\Database
 */
class Mapbd_Reward_Log extends Vite_Reward_Lite_Model {
	/**
	 * Its property id
	 *
	 * @var int
	 */
	public $id;
	/**
	 * Its property cash_drawer_id
	 *
	 * @var int
	 */
	public $user_id;
	/**
	 * Its property ref_id
	 *
	 * @var double
	 */
	public $point_val;
	/**
	 * Its property ref_id
	 *
	 * @var double
	 */
	public $prev_point;
	/**
	 * Its property ref_id
	 *
	 * @var string
	 */
	public $ref_val;
	/**
	 * Its property note
	 *
	 * @var String
	 */
	public $msg;
	/**
	 * Its property type
	 *
	 * @var String
	 */
	public $type;
	/**
	 * Its property type
	 *
	 * @var String
	 */
	public $is_mature;
	/**
	 * Its property ref_type
	 *
	 * @var String
	 */
	public $ref_type;
	/**
	 * Its property entry_time
	 *
	 * @var String
	 */
	public $entry_date;
	/**
	 * Its property entry_time
	 *
	 * @var String
	 */
	public $mature_date;

	/**
	 * Its property ex param
	 *
	 * @var String
	 */
	public $ex1_param;

	/**
	 * Its property ex2 param
	 *
	 * @var String
	 */
	public $ex2_param;

	/**
	 * Mapbd_pos_cash_drawer_log constructor.
	 */
	public function __construct() {
		parent::__construct();
		$this->set_validation();
		$this->table_name     = 'apbd_reward_log';
		$this->primary_key    = 'id';
		$this->unique_key     = array();
		$this->multi_key      = array();
		$this->auto_inc_field = array( 'id' );
		$this->app_base_name  = 'reward-point';
	}

	/**
	 * The do field value filter is generated by appsbd
	 *
	 * @param mixed $name It is name param.
	 * @param mixed $post_value It is post_value param.
	 * @param mixed $is_xs_clean It is is_xs_clean param.
	 */
	protected function do_field_value_filter( $name, &$post_value, $is_xs_clean ) {
		if ( 'prev_point' == $name ) {
			$post_value = appsbd_wc_amount( $post_value );
		}
	}

	/**
	 * The set validation is generated by appsbd
	 */
	public function set_validation() {
		$this->validations = array(
			'id'          => array(
				'Text' => 'Id',
				'Rule' => 'max_length[10]|integer',
			),
			'point_val'   => array(
				'Text' => 'Point value',
				'Rule' => 'max_length[11]',
			),
			'prev_point'  => array(
				'Text' => 'Previous Point',
				'Rule' => 'max_length[11]',
			),
			'user_id'     => array(
				'Text' => 'User Id',
				'Rule' => 'max_length[50]',
			),
			'msg'         => array(
				'Text' => 'Note',
				'Rule' => 'max_length[255]',
			),
			'ref_val'     => array(
				'Text' => 'Note',
				'Rule' => 'max_length[100]',
			),
			'type'        => array(
				'Text' => 'Log Type',
				'Rule' => 'max_length[1]',
			),
			'ref_type'    => array(
				'Text' => 'Ref Type',
				'Rule' => 'max_length[2]',
			),
			'entry_date'  => array(
				'Text' => 'Entry Time',
				'Rule' => 'max_length[20]',
			),
			'mature_date' => array(
				'Text' => 'Mature Date',
				'Rule' => 'max_length[20]',
			),

		);
	}

	/**
	 * The get property raw options is generated by appsbd
	 *
	 * @param mixed   $property Its the property.
	 * @param boolean $is_with_select False if with select.
	 *
	 * @return array|string[]
	 */
	public function get_property_raw_options( $property, $is_with_select = false ) {
		$return_obj = array();
		switch ( $property ) {
			case 'type':
				$return_obj = array(
					'I' => 'In',
					'O' => 'Out',
				);
				break;
			case 'ref_type':
				$return_obj = array(
					'O' => 'Order',
					'U' => 'User Id',
					'P' => 'Product ID',
				);
				break;
			case 'is_mature':
				$return_obj = array(
					'Y' => 'Yes',
					'N' => 'No',
				);
				break;
			default:
		}
		if ( $is_with_select ) {
			return array_merge( array( '' => 'Select' ), $return_obj );
		}

		return $return_obj;
	}

	/**
	 * The AddLog is generated by appsbd
	 *
	 * @param mixed  $type Its type.
	 * @param mixed  $user_id Its user_id.
	 * @param mixed  $prev_point Its prev_point.
	 * @param mixed  $point_val Its point_val.
	 * @param mixed  $msg Its msg.
	 * @param string $ref_val Its msg.
	 * @param string $ref_type Its ref_type.
	 * @param string $is_mature Its is_mature.
	 * @param string $ex1_param Its ex1_param.
	 * @param string $ex2_param Its ex2_param.
	 *
	 * @return Mapbd_Reward_Log|null
	 */
	public static function AddLog(
		$type,
		$user_id,
		$prev_point,
		$point_val,
		$msg,
		$ref_val = '',
		$ref_type = '',
		$is_mature = 'Y',
		$ex1_param = '',
		$ex2_param = ''
	) {
		$newobj = new self();
		$newobj->user_id( $user_id );
		$newobj->point_val( $point_val );
		$newobj->prev_point( $prev_point );
		$newobj->msg( $msg );
		$newobj->ref_val( $ref_val );
		$newobj->ref_type( $ref_type );
		$newobj->type( $type );
		$newobj->entry_date( gmdate( 'Y-m-d H:i:s' ) );
		$newobj->is_mature( $is_mature );
		$newobj->ex1_param( $ex1_param );
		$newobj->ex2_param( $ex2_param );
		if ( $newobj->save() ) {
			return $newobj;
		}

		return null;
	}

	/**
	 * The get current points is generated by appsbd
	 *
	 * @param mixed $user_id User Id.
	 *
	 * @return float|int
	 */
	public static function get_current_points( $user_id ) {
		$logs = new self();
		$logs->user_id( $user_id );
		$records = $logs->select_all_grid_data();
		if ( count( $records ) > 0 ) {
			$last_record    = end( $records );
			$current_points = 'I' == $last_record->type ? $last_record->prev_point + $last_record->point_val : $last_record->prev_point - $last_record->point_val;

			return appsbd_wc_amount( $current_points );
		}

		return 0;
	}

	/**
	 * The get logs by duration is generated by appsbd
	 *
	 * @param mixed $user_id It is user_id param.
	 * @param mixed $days It is days param.
	 *
	 * @return array
	 */
	public static function get_logs_by_duration( $user_id, $days = 7 ) {
		$current_date = gmdate( 'Y-m-d 23:59:59' );
		$current_date = esc_sql( $current_date );
		$last_date    = gmdate( 'Y-m-d 00:00:00', strtotime( $days . ' days ago' ) );
		$prev_date    = esc_sql( $last_date );
		$log          = new self();
		$log->user_id( $user_id );
		$log->entry_date( "BETWEEN '$prev_date' and '$current_date'", true );
		$logs = $log->select_all(
			'id,user_id,entry_date,
prev_point,point_val,ref_type,ref_val,type',
			'entry_date',
			'DESC'
		);
		$arr  = array();
		if ( ! empty( $logs ) ) {
			foreach ( $logs as $log ) {
				$data             = new \stdClass();
				$data->id         = $log->id;
				$data->user_id    = $log->user_id;
				$data->entry_date = appsbd_get_wp_datetime_with_format( $log->entry_date );
				$data->prev_point = $log->prev_point;
				$data->point_val  = $log->point_val;
				$data->ref_type   = $log->ref_type;
				$data->ref_val    = $log->ref_val;
				$data->type       = $log->type;
				$arr[]            = $data;
			}
		}

		return $arr;
	}

	/**
	 * The last five logs is generated by appsbd
	 *
	 * @param mixed $user_id It is user_id param.
	 *
	 * @return array
	 */
	public static function last_five_logs( $user_id ) {
		$log = new self();
		$log->user_id( $user_id );
		$logs = $log->select_all(
			'id,user_id,entry_date,
prev_point,point_val,ref_type,ref_val,type',
			'id',
			'DESC',
			5
		);
		$arr  = array();
		if ( ! empty( $logs ) ) {
			foreach ( $logs as $log ) {
				$data             = new \stdClass();
				$data->id         = $log->id;
				$data->user_id    = $log->user_id;
				$data->entry_date = appsbd_get_wp_datetime_with_format( $log->entry_date );
				$data->prev_point = $log->prev_point;
				$data->point_val  = $log->point_val;
				$data->ref_type   = $log->ref_type;
				$data->ref_val    = $log->ref_val;
				$data->type       = $log->type;
				$arr[]            = $data;
			}
		}

		return $arr;
	}

	/**
	 * The create db table is generated by appsbd
	 */
	public static function create_db_table() {
		$this_obj = new static();
		$table    = $this_obj->db->prefix . $this_obj->table_name;
		if ( $this_obj->db->get_var( "show tables like '{$table}'" ) != $table ) {
			$sql = "CREATE TABLE `{$table}` (
					`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  					`user_id` int(11) unsigned NOT NULL,
  					`ref_val` varchar(100)  NOT NULL DEFAULT '',
  					`ref_type` char(2) NOT NULL DEFAULT 'O' COMMENT 'radio(U=User,O=Order,P=Product)',
  					`type` char(1) NOT NULL COMMENT 'radio(I=In,O=Out)',
  					`entry_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  					`msg` char(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
  					`prev_point` decimal (10,2)  NOT NULL DEFAULT 0,
  					`point_val` decimal (10,2) unsigned NOT NULL DEFAULT 0,
  					`is_mature` char(1) NOT NULL DEFAULT 'N' COMMENT 'radio(Y=Yes,N=No)',
					`mature_date` timestamp(0) NULL,
  					`ex1_param` char(64) NOT NULL DEFAULT '' COMMENT 'extra fields',
  					`ex2_param` char(64) NOT NULL DEFAULT '' COMMENT 'extra fields',  					
  					PRIMARY KEY (`id`)
					)";
			require_once ABSPATH . 'wp-admin/includes/upgrade.php';
			dbDelta( $sql );
		}
	}
}
