<?php
/**
 * Gift Event Model
 *
 * @package     SGC\eGiftCard
 * @subpackage  Http\Model
 * @since       1.0.0
 */

namespace SGC\eGiftCard\Http\Model;

defined('ABSPATH') || exit; // Prevent direct file access.

/**
 * GiftEvent Class
 *
 * Handles database operations for gift events.
 *
 * @since 1.0.0
 */
class GiftEvent extends Model {
	/**
	 * The name of the database table.
	 *
	 * @var string
	 */
	protected $tableName = 'gift_events';

	/**
	 * Get random gift events with a mix of static and video types.
	 *
	 * Retrieves 9 random static events and 6 random video events.
	 *
	 * @since 1.0.0
	 *
	 * @return array Array of event objects.
	 */
	public static function getRandTenItem() {
		$model = static::getInstance();
		
		// Ensure table name is properly escaped for security.
		$table_name = esc_sql( $model->tableName );
		
		// Use prepare() method to safely construct SQL query.
		$query = $model->_wpdb->prepare(
			"(SELECT * FROM %i WHERE event_file_type = %s ORDER BY RAND() LIMIT %d)
			UNION
			(SELECT * FROM %i WHERE event_file_type = %s ORDER BY RAND() LIMIT %d)",
			$table_name,
			'static',
			9,
			$table_name,
			'video',
			6
		);
		
		/**
		 * Filter the random events query before execution.
		 *
		 * @since 1.0.0
		 * @param string $query The SQL query.
		 */
		$query = apply_filters( 'sgc_random_gift_events_query', $query );
		
		return $model->_wpdb->get_results( $query );
	}
}