<?php
/**
 * Lite-mode ETH transaction checker (polls etherscan.io).
 *
 * Matches incoming transfers by `to` + value (converted from wei).
 * Updates `cryptoniq_is_paid` or `cryptoniq_is_failed` so the AJAX checker
 * can return a unified response shape to the front-end (same as PRO mode).
 */

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

function cryptoniq_coin_check_eth( $order ) {
    if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
        return;
    }

    $status = $order->get_status();
    if ( in_array( $status, array( 'failed', 'cancelled' ), true ) ) {
        return;
    }

    $wallet = $order->get_meta( 'cryptoniq_coin_wallet' );
    $amount = $order->get_meta( 'cryptoniq_coin_amount' );
    $id     = $order->get_id();

    if ( ! $wallet || ! $amount ) {
        return;
    }

    $time_start   = (int) $order->get_meta( 'cryptoniq_time_start' );
    $time_counter = 60 * 60 * 3;
    $time_end     = $time_start + $time_counter;
    $time_current = time();

    $request = cryptoniq_remote_get( 'ETH', $wallet );

    if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) !== 200 ) {
        return; // explorer hiccup, try again next poll
    }

    $content = json_decode( wp_remote_retrieve_body( $request ), true );
    $txs     = isset( $content['result'] ) && is_array( $content['result'] ) ? $content['result'] : array();

    // Convert ETH amount (decimal string) to wei for exact comparison.
    $amount_str = (string) $amount;
    if ( strpos( $amount_str, '.' ) !== false ) {
        $parts        = explode( '.', $amount_str );
        $after_dec    = strlen( $parts[1] );
    } else {
        $after_dec = 0;
    }
    $num_without_decimal = str_replace( '.', '', $amount_str );
    $num_multiply        = 18 - $after_dec;
    $expected_wei        = $num_without_decimal * pow( 10, $num_multiply );
	
    foreach ( $txs as $tx ) {
        $to       = isset( $tx['to'] ) ? strtolower( $tx['to'] ) : '';
        $value    = isset( $tx['value'] ) ? $tx['value'] : '0';
        $tx_hash  = isset( $tx['hash'] ) ? $tx['hash'] : '';
        $confs    = isset( $tx['confirmations'] ) ? (int) $tx['confirmations'] : 0;
        $rec_ok   = isset( $tx['txreceipt_status'] ) ? (string) $tx['txreceipt_status'] : '0';

        if ( $to !== strtolower( $wallet ) ) {
            continue;
        }
        if ( (float) $value !== (float) $expected_wei ) {
            continue;
        }
        // Skip already-claimed txs.
        if ( $tx_hash && cryptoniq_check_txid( $tx_hash, $id ) === 1 ) {
            continue;
        }

        if ( $time_current <= $time_end ) {
            if ( $tx_hash ) {
                cryptoniq_tx_data_update( $tx_hash, $id );
            }
            if ( $confs >= 1 && $rec_ok === '1' ) {
                $order_status = cryptoniq_get_option( 'order_status' ) ?: 'processing';
                Cryptoniq_AJAX::order_status_update( $id, $tx_hash, $order_status );
            }
            return;
        }
    }

    // Window expired with no successful match → fail.
    if ( $time_current > $time_end ) {
        Cryptoniq_AJAX::order_status_update( $id, '', 'failed' );
    }
}
