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

/* ============================================================
   247ChatMate — Helper Functions
   FINAL • CLEAN • FUTURE-SAFE
   ------------------------------------------------------------
   • No rendering side-effects
   • No frontend assumptions
   • Widget V2 compatible
   • Safe for upgrades + migrations
=============================================================== */

/* ============================================================
   FAST SAFE OPTION GETTER
=============================================================== */

if ( ! function_exists( 'matewa_get' ) ) {
	function matewa_get( $key, $default = '' ) {
		$matewa_value = get_option( $key, $default );
		return ( false === $matewa_value || null === $matewa_value ) ? $default : $matewa_value;
	}
}

/* ============================================================
   RTL LANGUAGE CHECK (PURE)
   ------------------------------------------------------------
   • Explicit input only
   • No global state
   • Widget JS is authoritative
=============================================================== */

if ( ! function_exists( 'matewa_is_rtl' ) ) {
	function matewa_is_rtl( $lang ) {
		$matewa_rtl_langs = array( 'ar', 'fa', 'he', 'ur' );
		$matewa_lang      = strtolower( substr( (string) $lang, 0, 2 ) );

		return in_array( $matewa_lang, $matewa_rtl_langs, true );
	}
}

/* ============================================================
   ENVIRONMENT MODE (ADMIN ONLY)
=============================================================== */

if ( ! function_exists( 'matewa_env' ) ) {
	function matewa_env() {
		return matewa_get( 'matewa_env_mode', 'production' );
	}
}

if ( ! function_exists( 'matewa_mode_label' ) ) {
	function matewa_mode_label() {
		return strtoupper( matewa_env() );
	}
}

if ( ! function_exists( 'matewa_mode_badge' ) ) {
	function matewa_mode_badge() {
		$matewa_mode = matewa_env();
		$matewa_bg   = '#4CAF50';

		if ( 'debug' === $matewa_mode ) {
			$matewa_bg = '#ff9800';
		}

		if ( 'sandbox' === $matewa_mode ) {
			$matewa_bg = '#03A9F4';
		}

		return sprintf(
			'<span style="
				display:inline-block;
				padding:4px 9px;
				border-radius:4px;
				font-size:11px;
				font-weight:600;
				color:#fff;
				background:%s;
				margin-left:6px;
			">%s</span>',
			esc_attr( $matewa_bg ),
			esc_html( strtoupper( $matewa_mode ) )
		);
	}
}

/* ============================================================
   CHIP SANITISER (LEGACY / MIGRATION SAFE)
=============================================================== */

if ( ! function_exists( 'matewa_sanitise_chips' ) ) {
	function matewa_sanitise_chips( $chips ) {
		if ( ! is_array( $chips ) ) {
			return array();
		}

		$matewa_clean = array();

		foreach ( $chips as $matewa_chip ) {
			$matewa_text = trim( sanitize_text_field( $matewa_chip ) );

			if ( '' !== $matewa_text ) {
				$matewa_clean[] = $matewa_text;
			}
		}

		return $matewa_clean;
	}
}

/* ============================================================
   ADMIN — VERTICAL DROPDOWN (REMOTE + i18n)
=============================================================== */

if ( ! function_exists( 'matewa_render_vertical_dropdown' ) ) {
	function matewa_render_vertical_dropdown() {
		if ( ! function_exists( 'matewa_get_verticals' ) ) {
			echo '<em>' . esc_html__( 'Verticals unavailable', '247chatmate' ) . '</em>';
			return;
		}

		$matewa_data      = matewa_get_verticals();
		$matewa_verticals = $matewa_data['verticals'] ?? array();
		$matewa_default   = $matewa_data['default_vertical'] ?? 'general';
		$matewa_current   = get_option( 'matewa_vertical_selected', '' );

		echo '<select name="matewa_vertical_selected" id="matewa_vertical_selected">';

		foreach ( $matewa_verticals as $matewa_vertical_id => $matewa_vertical ) {
			$matewa_label_key = $matewa_vertical['label_key'] ?? ( 'vertical.' . $matewa_vertical_id );

			if ( function_exists( 'matewa_get_i18n_label' ) ) {
				$matewa_label = matewa_get_i18n_label( $matewa_label_key );
			} else {
				$matewa_label = ucwords( str_replace( '_', ' ', $matewa_vertical_id ) );
			}

			printf(
				'<option value="%s" %s>%s</option>',
				esc_attr( $matewa_vertical_id ),
				selected( '' !== $matewa_current ? $matewa_current : $matewa_default, $matewa_vertical_id, false ),
				esc_html( $matewa_label )
			);
		}

		echo '</select>';
	}
}

/* ============================================================
   DEBUG LOGGER (NO-OP IN REPO BUILD)
=============================================================== */

if ( ! function_exists( 'matewa_log' ) ) {
	function matewa_log( $message ) {
		unset( $message );
		return;
	}
}