<?php
defined( 'ABSPATH' ) || exit;

/* ======================================================================
   247ChatMate — Core Helper Functions
   Pure utilities only.
   No hooks. No output. No side effects.
   Safe to load early.
====================================================================== */

/* ----------------------------------------------------------------------
   FAST OPTION GETTER
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_opt' ) ) {
	function matewa_opt( $key, $default = '' ) {
		$matewa_value = get_option( $key );
		return ( false !== $matewa_value ) ? $matewa_value : $default;
	}
}

/* ----------------------------------------------------------------------
   SANITISE CHIP ARRAY
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_sanitise_chips' ) ) {
	function matewa_sanitise_chips( $chips ) {
		if ( ! is_array( $chips ) ) {
			return array();
		}

		$matewa_out = array();

		foreach ( $chips as $matewa_chip ) {
			$matewa_chip = trim( wp_kses_post( $matewa_chip ) );

			if ( '' !== $matewa_chip ) {
				$matewa_out[] = $matewa_chip;
			}
		}

		return $matewa_out;
	}
}

/* ----------------------------------------------------------------------
   CLEAN TEXT ARRAY (ALIAS)
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_clean_text_array' ) ) {
	function matewa_clean_text_array( $array ) {
		return matewa_sanitise_chips( $array );
	}
}

/* ----------------------------------------------------------------------
   JSON ENCODE CHIPS (UNICODE SAFE)
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_encode_chips' ) ) {
	function matewa_encode_chips( $chips ) {
		return wp_json_encode(
			array_values( (array) $chips ),
			JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
		);
	}
}

/* ----------------------------------------------------------------------
   RTL DETECTION
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_is_rtl' ) ) {
	function matewa_is_rtl() {
		if ( function_exists( 'is_rtl' ) && is_rtl() ) {
			return true;
		}

		$matewa_locale    = get_locale();
		$matewa_rtl_langs = array( 'ar', 'fa', 'he', 'ur' );

		foreach ( $matewa_rtl_langs as $matewa_lang ) {
			if ( 0 === stripos( $matewa_locale, $matewa_lang ) ) {
				return true;
			}
		}

		return false;
	}
}

/* ----------------------------------------------------------------------
   CLEAN WHATSAPP PHONE NUMBER
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_clean_phone' ) ) {
	function matewa_clean_phone( $number ) {
		$matewa_number = preg_replace( '/\D+/', '', (string) $number );
		return $matewa_number ? $matewa_number : '';
	}
}

/* ----------------------------------------------------------------------
   FORMAT WHATSAPP NUMBER (ALIAS)
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_format_whatsapp' ) ) {
	function matewa_format_whatsapp( $number ) {
		return matewa_clean_phone( $number );
	}
}

/* ----------------------------------------------------------------------
   PROFILE BLOCK BUILDER (HTML STRING)
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_build_profile_block' ) ) {
	function matewa_build_profile_block( $name, $title, $avatar_url ) {
		if ( ! $name && ! $title && ! $avatar_url ) {
			return '';
		}

		$matewa_avatar_html = $avatar_url
			? '<img class="matewa-profile-avatar" src="' . esc_url( $avatar_url ) . '" alt="" />'
			: '';

		$matewa_name_html = $name
			? '<div class="matewa-profile-name">' . esc_html( $name ) . '</div>'
			: '';

		$matewa_title_html = $title
			? '<div class="matewa-profile-title">' . esc_html( $title ) . '</div>'
			: '';

		return '
			<div class="matewa-profile-block">
				' . $matewa_avatar_html . '
				<div class="matewa-profile-text">
					' . $matewa_name_html . '
					' . $matewa_title_html . '
				</div>
			</div>
		';
	}
}

/* ----------------------------------------------------------------------
   FEATURE ACCESS ENGINE (MODE SAFE)
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_feature_allowed' ) ) {
	function matewa_feature_allowed( $feature ) {
		$matewa_mode = function_exists( 'matewa_get_mode' )
			? matewa_get_mode()
			: MATEWA_MODE_FREE;

		if ( defined( 'MATEWA_MODE_MASTER' ) && MATEWA_MODE_MASTER === $matewa_mode ) {
			return true;
		}

		$matewa_pro_like_modes = array();

		if ( defined( 'MATEWA_MODE_PRO' ) ) {
			$matewa_pro_like_modes[] = MATEWA_MODE_PRO;
		}

		if ( defined( 'MATEWA_MODE_TRIAL' ) ) {
			$matewa_pro_like_modes[] = MATEWA_MODE_TRIAL;
		}

		if ( defined( 'MATEWA_MODE_SOFT_TRIAL' ) ) {
			$matewa_pro_like_modes[] = MATEWA_MODE_SOFT_TRIAL;
		}

		switch ( $feature ) {
			case 'chips':
			case 'profile':
			case 'job_title':
			case 'avatar':
			case 'branding':
				return in_array( $matewa_mode, $matewa_pro_like_modes, true );

			case 'analytics_basic':
				return ( defined( 'MATEWA_MODE_FREE' ) && MATEWA_MODE_FREE !== $matewa_mode );

			case 'analytics_advanced':
				$matewa_advanced_modes = array();

				if ( defined( 'MATEWA_MODE_PRO' ) ) {
					$matewa_advanced_modes[] = MATEWA_MODE_PRO;
				}

				if ( defined( 'MATEWA_MODE_MASTER' ) ) {
					$matewa_advanced_modes[] = MATEWA_MODE_MASTER;
				}

				return in_array( $matewa_mode, $matewa_advanced_modes, true );

			default:
				return false;
		}
	}
}

/* ----------------------------------------------------------------------
   SOFT TRIAL ACTIVE CHECK
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_soft_trial_active' ) ) {
	function matewa_soft_trial_active() {
		if ( ! function_exists( 'matewa_get_mode' ) ) {
			return false;
		}

		if ( ! defined( 'MATEWA_MODE_SOFT_TRIAL' ) ) {
			return false;
		}

		return ( MATEWA_MODE_SOFT_TRIAL === matewa_get_mode() );
	}
}

/* ----------------------------------------------------------------------
   SOFT TRIAL MESSAGE (HTML STRING)
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_soft_trial_message' ) ) {
	function matewa_soft_trial_message() {
		if ( ! matewa_soft_trial_active() ) {
			return '';
		}

		if ( ! defined( 'MATEWA_SOFT_TRIAL_DAYS' ) ) {
			define( 'MATEWA_SOFT_TRIAL_DAYS', 7 );
		}

		$matewa_default_start = time();

		if ( defined( 'MATEWA_SOFT_TRIAL_START' ) ) {
			$matewa_start = intval( matewa_opt( MATEWA_SOFT_TRIAL_START, $matewa_default_start ) );
		} else {
			$matewa_start = $matewa_default_start;
		}

		$matewa_elapsed   = (int) floor( ( time() - $matewa_start ) / DAY_IN_SECONDS );
		$matewa_remaining = max( 0, MATEWA_SOFT_TRIAL_DAYS - $matewa_elapsed );

		return '<div class="matewa-soft-trial-msg">' .
			sprintf(
				/* translators: %d: number of days remaining in the soft trial period. */
				esc_html__(
					'Your free pass has ended. You are now in a soft trial with %d days remaining.',
					'247chatmate'
				),
				$matewa_remaining
			) .
		'</div>';
	}
}

/* ----------------------------------------------------------------------
   PAGE BUILDER DETECTION (EDIT MODE ONLY)
---------------------------------------------------------------------- */
if ( ! function_exists( 'matewa_builder_active' ) ) {
	function matewa_builder_active() {
		$matewa_elementor_preview = filter_input( INPUT_GET, 'elementor-preview', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$matewa_action            = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$matewa_et_fb             = filter_input( INPUT_GET, 'et_fb', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$matewa_bricks            = filter_input( INPUT_GET, 'bricks', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$matewa_fl_builder        = filter_input( INPUT_GET, 'fl_builder', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

		if (
			( defined( 'ELEMENTOR_VERSION' ) && ! empty( $matewa_elementor_preview ) ) ||
			( defined( 'ELEMENTOR_VERSION' ) && 'elementor' === $matewa_action )
		) {
			return true;
		}

		if ( ! empty( $matewa_et_fb ) ) {
			return true;
		}

		if ( ! empty( $matewa_bricks ) ) {
			return true;
		}

		if ( ! empty( $matewa_fl_builder ) ) {
			return true;
		}

		return false;
	}
}