<?php
/**
 * Panel service for visibility checks and program status evaluation.
 *
 * @package RewardsWP
 */

namespace Awesomemotive\Rewardswp\Services;

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

use Awesomemotive\Rewardswp\Settings\Options\Points\EarnActionsOption;
use Awesomemotive\Rewardswp\Settings\Options\Points\PointsEnabledOption;
use Awesomemotive\Rewardswp\Settings\Options\Points\SpendActionsOption;
use Awesomemotive\Rewardswp\Settings\Options\Referrals\EnabledOption;
use Awesomemotive\Rewardswp\Settings\Options\Referrals\RewardsOption;
use function Awesomemotive\Rewardswp\Helpers\get_option_object;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;
use function Awesomemotive\Rewardswp\Helpers\get_rewardswp_plan_tier;
use function Awesomemotive\Rewardswp\Helpers\is_current_user_a_member;

/**
 * Service class for handling panel visibility and program status checks.
 *
 * @since 1.1.0
 */
class PanelService {

	/**
	 * Check if the referrals program is active and properly configured.
	 *
	 * @since 1.1.0
	 * @return bool True if referrals program is active and configured.
	 */
	public function is_referrals_program_active(): bool {
		// Check if referrals are enabled.
		if ( ! get_option_value( EnabledOption::class ) ) {
			return false;
		}

		// Check if there's at least one reward type configured for advocates.
		$rewards_option = get_option_value( RewardsOption::class );
		return ! empty( $rewards_option['advocate']['type'] );
	}

	/**
	 * Check if the points program is active and properly configured.
	 *
	 * @since 1.1.0
	 * @return bool True if points program is active and configured.
	 */
	public function is_points_program_active(): bool {
		// Check if points program is enabled.
		if ( ! get_option_value( PointsEnabledOption::class ) ) {
			return false;
		}

		// Check if there's at least one enabled earn action.
		$earn_actions_option     = get_option_object( EarnActionsOption::class );
		$has_enabled_earn_action = false;
		if ( $earn_actions_option ) {
			$enabled_earn_actions    = $earn_actions_option->get_enabled_actions();
			$has_enabled_earn_action = ! empty( $enabled_earn_actions );
		}

		// Check if there's at least one enabled spend action.
		$spend_actions_option     = get_option_object( SpendActionsOption::class );
		$has_enabled_spend_action = false;
		if ( $spend_actions_option ) {
			$enabled_spend_actions    = $spend_actions_option->get_enabled_actions();
			$has_enabled_spend_action = ! empty( $enabled_spend_actions );
		}

		// Points program is active if both earn and spend actions are configured.
		return $has_enabled_earn_action && $has_enabled_spend_action;
	}

	/**
	 * Check if any rewards program is active and the panel should be displayed.
	 *
	 * The panel is displayed if either the referrals program or points program
	 * is active and properly configured.
	 *
	 * @since 1.1.0
	 * @return bool True if the panel should be displayed.
	 */
	public function should_display_panel(): bool {
		$is_active = $this->is_referrals_program_active() || $this->is_points_program_active();

		/**
		 * Returns if the program is currently active or not.
		 * Note: This filter also controls whether the panel should be shown to non-members.
		 *
		 * @since 1.1.0
		 *
		 * @param bool $is_active Whether the program is active.
		 */
		return apply_filters( 'rewardswp_is_program_active', $is_active );
	}

	/**
	 * Check if friends can claim rewards in the referrals program.
	 *
	 * @since 1.1.0
	 * @return bool True if friends can claim rewards.
	 */
	public function can_friends_claim_rewards(): bool {
		// Check if referrals are enabled.
		if ( ! get_option_value( EnabledOption::class ) ) {
			return false;
		}

		// Friend rewards require Pro license.
		if ( get_rewardswp_plan_tier() === 'lite' ) {
			return false;
		}

		// Check if there's a reward type configured for friends.
		$rewards_option = get_option_value( RewardsOption::class );
		return ! (
			empty( $rewards_option['friend'] ) ||
			empty( $rewards_option['friend']['type'] ) ||
			'unknown' === $rewards_option['friend']['type'] ||
			'points' === $rewards_option['friend']['type'] // Points are only for advocates, not friends.
		);
	}

	/**
	 * Check if a reward is available for claiming/display.
	 *
	 * Base implementation always returns true.
	 * Pro version checks Free Product stock status.
	 *
	 * @since 1.2.0
	 *
	 * @param array|null $reward The reward configuration array.
	 *
	 * @return bool True if the reward is available, false otherwise.
	 */
	public function is_reward_available( ?array $reward ): bool {
		// Base implementation: all rewards are available.
		// Pro overrides this to check Free Product stock status.
		return ! empty( $reward );
	}

	/**
	 * Check if the referrals section should be displayed in the panel.
	 *
	 * @since 1.1.0
	 * @return bool True if referrals section should be displayed.
	 */
	public function should_display_referrals_section(): bool {
		return is_current_user_a_member() && $this->is_referrals_program_active();
	}

	/**
	 * Check if the points section should be displayed in the panel.
	 *
	 * @since 1.1.0
	 * @return bool True if points section should be displayed.
	 */
	public function should_display_points_section(): bool {
		return is_current_user_a_member() && $this->is_points_program_active();
	}

	/**
	 * Get a summary of active programs.
	 *
	 * @since 1.1.0
	 * @return array Array containing status of each program.
	 */
	public function get_programs_status(): array {
		return [
			'referrals_active' => $this->is_referrals_program_active(),
			'points_active'    => $this->is_points_program_active(),
			'panel_visible'    => $this->should_display_panel(),
		];
	}
}
