<?php
/**
 * Instance Manager Class.
 *
 * @package automation-web-platform
 */

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

/**
 * Class WAWP_Instance_Manager.
 * Handles operations related to WhatsApp instances/accounts.
 */
class WAWP_Instance_Manager {
	/**
	 * Local cache for instances.
	 *
	 * @var array|null
	 */
	private $instances_cache = null;

	/**
	 * Get the instance data table name.
	 *
	 * @return string
	 */
	public function get_table_name() {
		return WAWP()->db->tables['instance_data'];
	}

	/**
	 * Get all instances from the database.
	 *
	 * @return array
	 */
	public function get_all_instances() {
		global $wpdb;
		$cache_key = 'wawp_all_instances';
		$results   = WAWP()->db->get_results_cached( $wpdb->prepare( 'SELECT * FROM %i', $this->get_table_name() ), $cache_key, 'wawp_instances', 300 );

		return array_map(
			function ( $row ) {
				return (object) $row;
			},
			$results
		);
	}

	/**
	 * Get an instance by its internal ID.
	 *
	 * @param int $id Internal DB ID.
	 * @return object|null
	 */
	public function get_instance_by_id( $id ) {
		global $wpdb;
		$cache_key = 'wawp_instance_id_' . (int) $id;
		$row       = WAWP()->db->get_results_cached( $wpdb->prepare( "SELECT * FROM %i WHERE id = %d AND status = 'online'", $this->get_table_name(), (int) $id ), $cache_key, 'wawp_instances', 300 );

		return ! empty( $row ) ? (object) $row[0] : null;
	}

	/**
	 * Get an instance by its external instance_id.
	 *
	 * @param string $instance_id External instance ID.
	 * @return object|null
	 */
	public function get_instance_by_instance_id( $instance_id ) {
		global $wpdb;
		$cache_key = 'wawp_instance_ext_' . md5( (string) $instance_id );
		$row       = WAWP()->db->get_results_cached( $wpdb->prepare( 'SELECT * FROM %i WHERE instance_id = %s', $this->get_table_name(), $instance_id ), $cache_key, 'wawp_instances', 300 );

		return ! empty( $row ) ? (object) $row[0] : null;
	}

	/**
	 * Get the first available online instance.
	 *
	 * @return object|null
	 */
	public function get_first_online_instance() {
		global $wpdb;
		$cache_key = 'wawp_first_online_instance';
		$row       = WAWP()->db->get_results_cached( $wpdb->prepare( "SELECT * FROM %i WHERE status = 'online' LIMIT 1", $this->get_table_name() ), $cache_key, 'wawp_instances', 60 );

		return ! empty( $row ) ? (object) $row[0] : null;
	}

	/**
	 * Get all online instances.
	 *
	 * @return array
	 */
	public function get_online_instances() {
		$all = $this->get_all_instances();

		return array_values(
			array_filter(
				$all,
				function ( $inst ) {
					return 'online' === strtolower( $inst->status );
				}
			)
		);
	}

	/**
	 * Get online instances with transient caching.
	 *
	 * @param int $ttl Cache time in seconds.
	 * @return array
	 */
	public function get_online_instances_cached( $ttl = 60 ) {
		if ( null !== $this->instances_cache ) {
			return $this->instances_cache;
		}

		$key  = 'wawp_online_instances';
		$data = get_transient( $key );
		if ( false !== $data ) {
			$this->instances_cache = $data;

			return $data;
		}

		$data = $this->get_online_instances();
		set_transient( $key, $data, $ttl );
		$this->instances_cache = $data;

		return $data;
	}

	/**
	 * Get just the instance_ids of online instances.
	 *
	 * @return array
	 */
	public function get_online_instance_ids() {
		$instances = $this->get_online_instances_cached();
		$ids       = array();
		foreach ( $instances as $inst ) {
			$ids[] = $inst->instance_id;
		}

		return array_unique( $ids );
	}

	/**
	 * Get access token for a specific instance.
	 *
	 * @param string $instance_id External instance ID.
	 * @return string|null
	 */
	public function get_access_token( $instance_id ) {
		global $wpdb;
		$cache_key = 'wawp_token_' . md5( (string) $instance_id );
		$token     = wp_cache_get( $cache_key, 'wawp_instances' );

		if ( false === $token ) {
			$token = WAWP()->db->get_var( $wpdb->prepare( "SELECT access_token FROM %i WHERE instance_id = %s AND status = 'online'", $this->get_table_name(), $instance_id ) );
			wp_cache_set( $cache_key, $token, 'wawp_instances', 300 );
		}

		return $token;
	}
}
