<?php
/**
 * Mysterx helpers functions.
 *
 * @link       https://wpgenie.org
 * @since      1.0.0
 *
 * @package    Mysterx
 * @subpackage Mysterx/includes
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Get items for product
 *
 * @param  mixed $product_id product ID.
 * @param  mixed $context context.
 * @return array items.
 */
function mysterx_get_items_for_product( $product_id, $context = 'view' ) {

	global $wpdb;

	$results = wp_cache_get( 'mysterx_get_items_for_product' . $product_id, 'mysterx' );

	if ( false === $results || 'edit' === $context ) {

		$results = $wpdb->get_results(
			$wpdb->prepare(
				'SELECT * FROM %i
			WHERE product_id = %d
			ORDER BY `order` ASC',
				MYSTERX_TBL_ITEMS,
				$product_id
			),
			ARRAY_A
		);
		wp_cache_set( 'mysterx_get_items_for_product' . $product_id, $results, 'mysterx' );
	}

	return $results;
}

/**
 * Get user items
 *
 * @param  mixed $user_id user ID.
 * @param  mixed $context context.
 * @return array user items.
 */
function mysterx_get_user_items( $user_id, $context = 'view' ) {

	global $wpdb;

	$results = wp_cache_get( 'mysterx_get_user_items' . $user_id, 'mysterx' );

	if ( false === $results || 'edit' === $context ) {

		$results = $wpdb->get_results(
			$wpdb->prepare(
				'SELECT * FROM %i
				WHERE user_id = %d
				ORDER BY `date` ASC  ',
				MYSTERX_TBL_LOG,
				$user_id
			),
			ARRAY_A
		);
		wp_cache_set( 'mysterx_get_user_items' . $user_id, $results, 'mysterx' );
	}

	return $results;
}

/**
 * Upsert items for product
 *
 * @param  mixed $product_id product ID.
 * @param  mixed $items_json items json.
 * @return void
 */
function mysterx_upsert_items_for_product( $product_id, $items_json ) {

	global $wpdb;

	$exists = $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE product_id=%d LIMIT 1', MYSTERX_TBL_ITEMS, $product_id ) );

	if ( $exists ) {

		$wpdb->update( MYSTERX_TBL_ITEMS, array( 'items_json' => $items_json ), array( 'product_id' => $product_id ), array( '%s' ), array( '%d' ) );

	} else {

		$wpdb->insert(
			MYSTERX_TBL_ITEMS,
			array(
				'reward_id'  => $product_id,
				'items_json' => $items_json,
			),
			array( '%d', '%s' )
		);
	}
}

/**
 * Check if user can open box
 *
 * @param  mixed $user_id user ID.
 * @param  mixed $product_id product ID.
 * @return bool true if can open, false otherwise.
 */
function mysterx_check_if_user_can_open_box( $user_id, $product_id ) {

	global $wpdb;

	$exists = $wpdb->get_var(
		$wpdb->prepare(
			'SELECT 1
			FROM %i as l
			LEFT JOIN %i as o ON l.id = o.id
			WHERE l.user_id = %d
				AND l.product_id = %d
				AND o.opened_at IS NULL
			LIMIT 1',
			MYSTERX_TBL_LOG,
			MYSTERX_TBL_OPEN_LOG,
			$user_id,
			$product_id
		)
	);
	return ! is_null( $exists );
}

/**
 * Log box open event
 *
 * @param  mixed $user_id user ID.
 * @param  mixed $product_id product ID.
 * @param  mixed $order_id order ID.
 * @param  mixed $reward_id reward ID.
 * @return void
 */
function mysterx_log_open( $user_id, $product_id, $order_id, $reward_id ) {

	global $wpdb;

	$exists = $wpdb->get_var(
		$wpdb->prepare(
			'SELECT l.id
			FROM %i as l
			LEFT JOIN %i as o ON l.id = o.id
			WHERE l.user_id = %d
				AND l.product_id = %d
				AND o.opened_at IS NULL
			LIMIT 1',
			MYSTERX_TBL_LOG,
			MYSTERX_TBL_OPEN_LOG,
			$user_id,
			$product_id
		)
	);

	if ( $exists ) {

		$wpdb->insert(
			MYSTERX_TBL_OPEN_LOG,
			array(
				'id'         => $exists,
				'user_id'    => $user_id,
				'order_id'   => $order_id,
				'product_id' => $product_id,
				'reward_id'  => $reward_id,
			),
			array( '%d', '%d', '%d', '%d', '%d' )
		);
	}
}

/**
 * Get rarity class by weight
 *
 * @param  mixed $weight weight of the item.
 * @param  mixed $product_id product ID.
 * @param  mixed $reward_id reward ID.
 * @return string rarity class.
 */
function mysterx_get_rarity_class_by_weight( $weight, $product_id, $reward_id ) {
	$rarity     = 'common';
	$thresholds = apply_filters(
		'mysterx_rarity_thresholds',
		array(
			'common'    => 50,
			'uncommon'  => 30,
			'rare'      => 15,
			'epic'      => 4,
			'legendary' => 1,
		),
		$product_id,
		$reward_id
	);
	foreach ( $thresholds as $class => $threshold ) {
		if ( $weight >= $threshold ) {
			$rarity = $class;
			break;
		}
	}
	return $rarity;
}
