<?php
/**
 * Mock data service for admin previews and sample content.
 *
 * @package RewardsWP
 */

namespace Awesomemotive\Rewardswp\Services;

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

use Awesomemotive\Rewardswp\Factories\RewardTypesFactory;
use Awesomemotive\Rewardswp\Helpers\IconHelper;
use Awesomemotive\Rewardswp\Models\Member;
use Awesomemotive\Rewardswp\Models\Reward;
use DI\Container;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;
use function Awesomemotive\Rewardswp\Helpers\get_points_currency_plural;

/**
 * Provides mock data for admin previews (settings page widget).
 *
 * This generates a preview Member and a preview list of rewards using the
 * RewardTypes interfaces so labels/icons remain in sync with real RewardTypes.
 */
class MockDataService {
	/**
	 * DI container instance.
	 *
	 * @var Container
	 */
	private Container $container;

	/**
	 * Reward types factory instance.
	 *
	 * @var RewardTypesFactory
	 */
	private RewardTypesFactory $reward_types_factory;

	/**
	 * Icon helper instance.
	 *
	 * @var IconHelper
	 */
	private IconHelper $icon_helper;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param Container          $container            The DI container instance.
	 * @param RewardTypesFactory $reward_types_factory The reward types factory.
	 * @param IconHelper         $icon_helper          The icon helper instance.
	 */
	public function __construct( Container $container, RewardTypesFactory $reward_types_factory, IconHelper $icon_helper ) {
		$this->container            = $container;
		$this->reward_types_factory = $reward_types_factory;
		$this->icon_helper          = $icon_helper;
	}

	/**
	 * Build a non-persisted preview member object based on the current WP user.
	 */
	public function get_preview_member(): Member {
		$current_user = wp_get_current_user();
		/**
		 * Member model instance.
		 *
		 * @var Member $member
		 */
		$member = $this->container->get( Member::class );
		$member->hydrate(
			[
				'id'                  => 0,
				'user_id'             => (int) $current_user->ID,
				'email'               => (string) $current_user->user_email,
				'first_name'          => (string) $current_user->first_name,
				'last_name'           => (string) $current_user->last_name,
				'referral_code'       => 'preview',
				'status'              => 'active',
				'total_referrals'     => 0,
				'completed_referrals' => 0,
			]
		);

		return $member;
	}

	/**
	 * Build a small set of available rewards formatted for Alpine.js from RewardTypes.
	 *
	 * Structure mirrors AdvocateController::prepare_reward_for_alpine().
	 *
	 * @return array[]
	 */
	public function get_preview_available_rewards(): array {
		$definitions = [ 'fixed_discount', 'percentage_discount' ];
		$rewards     = [];

		foreach ( $definitions as $type_key ) {
			$type_instance = $this->reward_types_factory->get_reward_type( $type_key );
			if ( ! $type_instance ) {
				continue;
			}

			/**
			 * Reward model instance.
			 *
			 * @var Reward $fake_reward
			 */
			$fake_reward = $this->container->get( Reward::class );
			$fake_reward->hydrate(
				[
					'id'     => 0,
					'amount' => 'percentage_discount' === $type_key ? 10 : 5,
					// Non-persisted metadata example: coupon_code
				]
			);
			$type_instance->set_reward( $fake_reward );

			// Build icon htmls via IconHelper to match UI.
			ob_start();
			$this->icon_helper->render_icon( $type_instance->get_icon_name(), 'w-8 h-8' );
			$icon_html = ob_get_clean();

			ob_start();
			$this->icon_helper->render_icon( $type_instance->get_icon_name(), 'w-8 h-8 text-green-600' );
			$icon_html_green = ob_get_clean();

			$points_label = get_points_currency_plural();

			$rewards[] = [
				'id'              => 'percentage_discount' === $type_key ? 2 : 1,
				'title'           => $type_instance->get_panel_title(),
				'description'     => method_exists( $type_instance, 'get_panel_instructions' ) ? $type_instance->get_panel_instructions() : $type_instance->get_panel_title(),
				'coupon_code'     => 'percentage_discount' === $type_key ? 'DEMO10' : 'DEMO5',
				'is_applied'      => false,
				'expiration_text' => '',
				'icon_html'       => $icon_html,
				'icon_html_green' => $icon_html_green,
				'points_cost'     => 'percentage_discount' === $type_key ? 1000 : 500,
				'points_label'    => $points_label,
			];
		}

		return $rewards;
	}
}
