<?php
/**
 * 247ChatMate — Platform Services Compatibility Layer
 *
 * Read-only compatibility helpers for optional 247 Platform Services support.
 *
 * This file intentionally does not create a hard dependency on Platform Services.
 * If TPS_API is unavailable, all helpers return safe empty/fallback values and
 * existing ChatMate behaviour continues unchanged.
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'matewa_platform_services_available' ) ) {
	/**
	 * Check whether 247 Platform Services API is available.
	 *
	 * @return bool
	 */
	function matewa_platform_services_available() {
		return class_exists( 'TPS_API' );
	}
}

if ( ! function_exists( 'matewa_platform_call' ) ) {
	/**
	 * Safely call a read-only TPS_API method when available.
	 *
	 * @param string $method Method name on TPS_API.
	 * @param array  $args   Optional method arguments.
	 * @param mixed  $fallback Fallback value.
	 * @return mixed
	 */
	function matewa_platform_call( $method, $args = array(), $fallback = array() ) {
		if ( ! matewa_platform_services_available() || ! is_string( $method ) || '' === $method ) {
			return $fallback;
		}

		if ( ! method_exists( 'TPS_API', $method ) || ! is_callable( array( 'TPS_API', $method ) ) ) {
			return $fallback;
		}

		try {
			$result = call_user_func_array( array( 'TPS_API', $method ), is_array( $args ) ? $args : array() );
			return null === $result ? $fallback : $result;
		} catch ( Throwable $e ) {
			return $fallback;
		}
	}
}

if ( ! function_exists( 'matewa_platform_context' ) ) {
	/**
	 * Read current platform context from Platform Services when available.
	 *
	 * @return array
	 */
	function matewa_platform_context() {
		$result = matewa_platform_call( 'context', array(), array() );
		return is_array( $result ) ? $result : array();
	}
}

if ( ! function_exists( 'matewa_platform_price' ) ) {
	/**
	 * Read a product price from Platform Services when available.
	 *
	 * @param string $product_id Product ID.
	 * @param string $currency   Currency code.
	 * @return array
	 */
	function matewa_platform_price( $product_id = '247chatmate', $currency = 'GBP' ) {
		$product_id = is_string( $product_id ) ? sanitize_key( $product_id ) : '247chatmate';
		$currency   = is_string( $currency ) ? strtoupper( sanitize_key( $currency ) ) : 'GBP';

		$result = matewa_platform_call( 'get_product_price', array( $product_id, $currency ), array() );
		return is_array( $result ) ? $result : array();
	}
}

if ( ! function_exists( 'matewa_platform_route' ) ) {
	/**
	 * Read a route from Platform Services when available.
	 *
	 * @param string $route_id Route ID.
	 * @return array|string
	 */
	function matewa_platform_route( $route_id = '' ) {
		$route_id = is_string( $route_id ) ? sanitize_key( $route_id ) : '';
		return matewa_platform_call( 'get_route', array( $route_id ), array() );
	}
}

if ( ! function_exists( 'matewa_platform_health' ) ) {
	/**
	 * Read Platform Services health when available.
	 *
	 * @return array
	 */
	function matewa_platform_health() {
		$result = matewa_platform_call( 'health', array(), array() );
		return is_array( $result ) ? $result : array();
	}
}
