<?php

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

/**
 * Internal: GET an api.cryptoniq.io endpoint, picking the right path + auth
 * based on whether a license is active.
 *
 * No license → /{endpoint}/lite (no auth, limited dataset: BTC+ETH / EUR only)
 * Licensed   → /{endpoint}      (X-Cryptoniq-License header, full dataset)
 */
function cryptoniq_api_get( $endpoint ) {
    $is_pro = cryptoniq_is_pro();
    $url    = CRYPTONIQ_API_URL . $endpoint . ( $is_pro ? '' : '/lite' );

    $args = array( 'timeout' => 15 );
    if ( $is_pro ) {
        $args['headers'] = array(
            'X-Cryptoniq-License' => cryptoniq_get_option( 'license_key' ),
        );
    }

    $request = wp_remote_get( $url, $args );
    if ( is_wp_error( $request ) ) {
        return false;
    }

    $code = wp_remote_retrieve_response_code( $request );
    if ( $code !== 200 ) {
        return false;
    }

    $data = json_decode( wp_remote_retrieve_body( $request ), true );
    return is_array( $data ) ? $data : false;
}

function cryptoniq_get_currency_prices() {
    $data = cryptoniq_api_get( 'currencies' );
    // Lite returns only EUR; PRO returns many. Either way, at least EUR
    // must be present for the price-conversion code to work.
    if ( ! is_array( $data ) || ! isset( $data['EUR'] ) ) {
        return false;
    }
    return $data;
}

function cryptoniq_get_coin_prices() {
    $data = cryptoniq_api_get( 'prices' );
    // Lite returns BTC+ETH; PRO returns all coins. At minimum BTC must
    // be present (the cron / fallback paths assume it).
    if ( ! is_array( $data ) || ! isset( $data['BTC'] ) ) {
        return false;
    }
    return $data;
}
	
function cryptoniq_get_price( $coin = 'BTC', $amount = '', $currency = '', $random = false, $mask = true ) {
	if (!$mask) {
		$pws = $pwe = '';
	} else {
		$pws = '<span class="cryptoniq-price-mask">';
		$pwe = '</span>';		
	}
	
	if ( ! isset( $amount ) || ! is_numeric( $amount ) ) {
		return $pws . '0.00000000' . $pwe;
	}

	$base_currency   = get_woocommerce_currency();
	$currency_prices = get_option( 'cryptoniq_currency_prices' );
	$coin_prices     = get_option( 'cryptoniq_coin_prices' );

	// Convert to USD if base currency is not USD
	if ( $currency && isset( $currency_prices[ $currency ] ) ) {
		$amount = $amount / $currency_prices[ $currency ];
	} elseif ( $base_currency !== 'USD' && $base_currency !== 'BTC' && isset( $currency_prices[ $base_currency ] ) ) {
		$amount = $amount / $currency_prices[ $base_currency ];
	}

	// Check if stablecoin
	$is_stable = strpos( $coin, 'USDT' ) !== false || strpos( $coin, 'USDC' ) !== false;

	// Conversion to coin
	if ( $is_stable ) {
		$price = $amount;
	} elseif ( isset( $coin_prices[ $coin ] ) && $coin_prices[ $coin ] > 0 ) {
		if ( $base_currency === 'BTC' ) {
			$price = $amount;
		} else {
			$price = $amount / $coin_prices[ $coin ];
		}
	} else {
		return $pws . '0.00000000' . $pwe; // fallback if no rate
	}

	// Add a slight randomizer for realism (optional)
	$round_length = 100000000;
	if ( $coin === 'TRX' || $is_stable ) {
		$round_length = 1000000;
	} elseif ( $coin === 'SOL' ) {
		$round_length = 1000000000;
	}
	
	$extra_random_number = $random ? rand( 100, 999 ) / $round_length : 0;
	if ( $coin === 'BTC' && $random ) {
		$extra_random_number = rand( 10, 99 ) / $round_length;
	}

	$coins_num = $price + $extra_random_number;

	// Format based on coin
	if ( $coin === 'TRX' || $is_stable ) {
		$coin_format = number_format( $coins_num, 6, '.', '' );
	} elseif ( $coin === 'SOL' ) {
		$coin_format = number_format( $coins_num, 9, '.', '' );
	} else {
		$coin_format = number_format( $coins_num, 8, '.', '' );
	}
	
	return $pws . esc_html( $coin_format ). $pwe;
}