<?php

/**
 * All Multi Currency related functions.
 *
 * @package  WooZo Inventory
 * @category Zoho Integration
 * @author   Roadmap Studios
 * @link     https://commercebird.com
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class CMBIRD_Multicurrency_Zoho {

	private $config;
	public function __construct() {

		$config = array(

			'MulticurrencyZI' => array(
				'OID'    => get_option( 'cmbird_zoho_inventory_oid' ),
				'APIURL' => get_option( 'cmbird_zoho_inventory_url' ),

			),

		);

		$this->config = $config;
	}

	/**
	 * Retrieve the currency ID for a given user currency.
	 *
	 * Execute a cURL call to retrieve all currencies from Zoho Inventory,
	 * and then loop through the response to find the matching currency code.
	 * If a match is found, update the user meta with the currency ID and code.
	 *
	 * @param string $user_currency The user currency to retrieve the ID for.
	 * @param int    $userid The user ID to update the meta for.
	 * @param object $order The order object to update the meta for (if applicable).
	 *
	 * @return int The currency ID.
	 */
	public function zoho_currency_data( $user_currency, $userid = '', $order = 0 ) {
		$zoho_inventory_oid = $this->config['MulticurrencyZI']['OID'];
		$zoho_inventory_url = $this->config['MulticurrencyZI']['APIURL'];

		$cache_key  = 'cmbird_zoho_inventory_currencies';
		$currencies = wp_cache_get( $cache_key );
		if ( false === $currencies ) {
			$currencies = get_option( $cache_key );
			if ( empty( $currencies ) ) {
				$url                      = $zoho_inventory_url . 'inventory/v1/settings/currencies?organization_id=' . $zoho_inventory_oid;
				$execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
				$response                 = $execute_curl_call_handle->execute_curl_call_get( $url );
				if ( isset( $response->code ) && ( 0 === $response->code || '0' === $response->code ) && ! empty( $response->currencies ) ) {
					$currencies = $response->currencies;
					// filter out unnecessary data before caching. Only store currency_id and currency_code.
					$currencies = array_map(
						function ( $currency ) {
									return (object) array(
										'currency_id'   => $currency->currency_id,
										'currency_code' => $currency->currency_code,
									);
						},
						$currencies
					);
					update_option( $cache_key, $currencies, false );
				}
			}
			wp_cache_set( $cache_key, $currencies );
		}

		if ( empty( $currencies ) ) {
			return 0;
		}

		$currency_id = 0;
		foreach ( $currencies as $value ) {
			if ( $value->currency_code === $user_currency ) {
				$currency_id = $value->currency_id;
				if ( '' === $userid ) {
					$order->update_meta_data( 'zi_currency_id', $value->currency_id );
					$order->update_meta_data( 'zi_currency_code', $value->currency_code );
				} else {
					update_user_meta( $userid, 'zi_currency_id', $value->currency_id );
					update_user_meta( $userid, 'zi_currency_code', $value->currency_code );
				}
				break;
			}
		}

		return $currency_id;
	}
}
