<?php
/**
 * CryptoFaucet.cc PHP SDK
 * Official SDK Synchronized with Core Ledger API
 */

class CryptoFaucetSDK {
    private $api_key;
    private $secret_key;
    private $merchant_wallet;
    private $api_url = "https://cryptofaucet.cc/api.php";
    private $payout_url = "https://cryptofaucet.cc/api_payout.php";

    /**
     * @param string $api_key Public API Key
     * @param string $secret_key Private Secret Key (optional, for payouts)
     * @param string $merchant_wallet Merchant Wallet ID (e.g. C100000)
     */
    public function __construct($api_key, $secret_key = '', $merchant_wallet = '') {
        $this->api_key = trim($api_key);
        $this->secret_key = trim($secret_key);
        
        // Enforce strict uppercase 'C' format for merchant wallet
        $wallet = trim($merchant_wallet);
        if (strpos(strtolower($wallet), 'c') === 0) {
            $wallet = 'C' . substr($wallet, 1);
        }
        $this->merchant_wallet = $wallet;
    }

    /**
     * Create a payment invoice (Deposit)
     */
    public function createInvoice($amount, $order_id) {
        $amount_fixed = number_format((float)$amount, 8, '.', '');
        
        $params = [
            'api_key'  => $this->api_key,
            'amount'   => $amount_fixed,
            'order_id' => (int)$order_id
        ];
        
        return $this->request($this->api_url, $params);
    }

    /**
     * Automated Payout to internal Wallet ID with Core Ledger Signature
     */
    public function createPayout($amount, $currency, $address) {
        if (empty($this->secret_key) || empty($this->merchant_wallet)) {
            return ['status' => 'error', 'message' => 'Secret Key and Merchant Wallet ID are required for payouts'];
        }

        // Constraints: 8 decimal places for accuracy
        $amount_fixed = number_format((float)$amount, 8, '.', '');

        // Sanitize target address to uppercase 'C'
        $clean_address = trim($address);
        if (strpos(strtolower($clean_address), 'c') === 0) {
            $clean_address = 'C' . substr($clean_address, 1);
        }

        // Core Node MD5 Hash Sign: secret_key + api_key + merchant_wallet + address + amount
        $hash_string = $this->secret_key . $this->api_key . $this->merchant_wallet . $clean_address . $amount_fixed;
        $payout_sign = md5($hash_string);

        $params = [
            'api_key'         => $this->api_key,         
            'payout_key'      => $this->secret_key,         
            'merchant_wallet' => $this->merchant_wallet,
            'address'         => $clean_address,   
            'amount'          => $amount_fixed,         
            'currency'        => strtoupper(str_ireplace(['USDT_TRC_20', 'USDT_TRC20', 'BNB_BEP20'], ['USDT', 'USDT', 'BNB'], $currency)),                
            'sign'            => $payout_sign,          
            'signature'       => $payout_sign           
        ];

        return $this->request($this->payout_url, $params);
    }

    private function request($url, $data) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        
        $response = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);

        if ($error) {
            return ['status' => 'error', 'message' => 'CURL Error: ' . $error];
        }

        return json_decode($response, true);
    }
}