<?php
/**
 * Reward Badge Model
 *
 * @package Vite_Reward_Lite\Models\Database
 */

namespace Vite_Reward_Lite\Models\Database;

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

use Vite_Reward_Lite\Core\Vite_Reward_Lite_Model;
use Sabberworm\CSS\Property\Charset;

/**
 * Class Mapbd_Reward_Badge
 *
 * @package Vite_Reward_Lite\Models\Database
 */
class Mapbd_Reward_Badge extends Vite_Reward_Lite_Model {
	/**
	 * Its property id
	 *
	 * @var int
	 */
	public $id;
	/**
	 * Its property name
	 *
	 * @var String
	 */
	public $name;
	/**
	 * Its property description
	 *
	 * @var String
	 */
	public $description;
	/**
	 * Its property threshold_points
	 *
	 * @var int
	 */
	public $threshold_points;
	/**
	 * Its property welcome_points
	 *
	 * @var int
	 */
	public $welcome_points;
	/**
	 * Its property cal_val
	 *
	 * @var double
	 */
	public $cal_val;
	/**
	 * Its property cal_type
	 *
	 * @var Charset
	 */
	public $cal_type;
	/**
	 * Its property is_default
	 *
	 * @var Charset
	 */
	public $is_default;
	/**
	 * Its property is_additional
	 *
	 * @var Charset
	 */
	public $is_additional;
	/**
	 * Its property img_id
	 *
	 * @var int
	 */
	public $img_id;
	/**
	 * Its property status
	 *
	 * @var Charset
	 */
	public $status;

	/**
	 * Reward_Badge constructor.
	 */
	public function __construct() {
		parent::__construct();
		$this->set_validation();
		$this->table_name     = 'apbd_reward_badge';
		$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 set validation is generated by appsbd
	 */
	public function set_validation() {
		$this->validations = array(
			'id'               => array(
				'Text' => 'Id',
				'Rule' => 'max_length[10]|integer',
			),
			'name'             => array(
				'Text' => 'Name',
				'Rule' => 'max_length[100]',
			),
			'description'             => array(
				'Text' => 'Description',
				'Rule' => 'max_length[255]',
			),
			'threshold_points' => array(
				'Text' => 'Threshold Points',
				'Rule' => 'max_length[11]',
			),
			'welcome_points'   => array(
				'Text' => 'Welcome Points',
				'Rule' => 'max_length[11]',
			),
			'cal_val'          => array(
				'Text' => 'Calculate Value',
				'Rule' => 'max_length[11]|double',
			),
			'cal_type'         => array(
				'Text' => 'Calculation Type',
				'Rule' => 'max_length[1]',
			),
			'is_default'       => array(
				'Text' => 'Is Default',
				'Rule' => 'max_length[1]',
			),
			'is_additional'    => array(
				'Text' => 'Additional Calculation',
				'Rule' => 'required|max_length:1',
			),
			'img_id'           => array(
				'Text' => 'Image Id',
				'Rule' => 'max_length[11]',
			),
			'status'           => array(
				'Text' => 'Status',
				'Rule' => 'required|max_length:1',
			),
		);
	}

	/**
	 * 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,
  					  `name` char(100) CHARACTER SET utf8 NOT NULL,
  					  `description` char(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
					  `threshold_points` int(11) NOT NULL DEFAULT '0',
					  `welcome_points` int(11) NOT NULL DEFAULT '0',					  
					  `cal_val` decimal (5,2) NOT NULL DEFAULT 0,				  
					  `cal_type` char(1) NOT NULL DEFAULT 'P' COMMENT 'radio(P=Percentage,A=Addition)',
					  `is_default` char(1) NOT NULL DEFAULT 'N' COMMENT 'bool(Y=Yes,N=No)',
					  `is_additional` char(1) NOT NULL DEFAULT 'Y' COMMENT 'bool(Y=Yes,N=No)',
					  `img_id` int(11)  NOT NULL DEFAULT 0,
					  `status` char(1) NOT NULL DEFAULT 'A' COMMENT 'bool(A=Active,I=Inactive)',
					  PRIMARY KEY (`id`)
					)";
			require_once ABSPATH . 'wp-admin/includes/upgrade.php';
			dbDelta( $sql );
		}
	}

	/**
	 * The delete by id is generated by appsbd
	 *
	 * @param int $id Its user id.
	 */
	public static function delete_by_id( $id ) {
		return self::delete_by_key_value( 'id', $id );
	}
	/**
	 * The delete by id is generated by appsbd
	 *
	 * @param int $id Its user id.
	 */
	public static function get_badge_name_by_id( $id ) {
		$title = '';
		if ( ! empty( $id ) ) {
			$obj = new self();
			$obj->id( $id );
			if ( $obj->select( 'id,name' ) ) {
				$title = $obj->name;
			}
		}
		return $title;
	}
}
