<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://webequipe.com
 * @since      1.0.0
 *
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/admin
 */

use Webequipe\SpinWinWheel\Traits\Form_Error;

/**
 * Winner Handler class.
 */
class Webequipe_Spin_Win_Wheel_Winner {

	use Form_Error;

	/**
	 * Plugin page handler.
	 *
	 * @return void
	 */
	public function winner_page() {
		// Check user capabilities.
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'webequipe-spin-win-wheel' ) );
		}

		// Verify nonce before trusting routing parameters.
		$has_valid_nonce = isset( $_REQUEST['_wpnonce'] )
			&& wp_verify_nonce(
				sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ),
				'wsww_spin_action'
			);

		// Only honor action/id when nonce is valid; otherwise show the list.
		$action = ( $has_valid_nonce && isset( $_GET['action'] ) )
			? sanitize_key( wp_unslash( $_GET['action'] ) )
			: 'list';

		$id = ( $has_valid_nonce && isset( $_GET['id'] ) )
			? absint( wp_unslash( $_GET['id'] ) )
			: 0;

		switch ( $action ) {
			case 'view':
				$winner   = webequipe_sww_get_winner_detail( $id );
				$template = __DIR__ . '/partials/webequipe-spin-win-wheel-winner-view.php';
				break;

			default:
				$template = __DIR__ . '/partials/webequipe-spin-win-wheel-winner-list.php';
				break;
		}

		if ( file_exists( $template ) ) {
			include $template;
		}
	}

	/**
	 * Handle the form.
	 *
	 * @return void
	 */
	public function form_handler() {
		// Reserved for future use.
	}

	/**
	 * Delete spin handler.
	 *
	 * @return void
	 */
	public function delete_spin() {
		// Reserved for future use.
	}
}
