<?php
if (!defined('ABSPATH')) exit;

final class SPG_API {

  /**
   * =========================
   * API KEYS (BACKEND ONLY)
   * =========================
   * Put your keys here. Users will never see them in WP admin settings.
   * Leave empty if you want to rely on public limits (not recommended).
   */

  // EVM scanners
  private const ETHERSCAN_KEY   = 'NZ6Y3AZJZQ796VHV6BFPS7BUU1XV22XYX3'; // e.g. 'YOUR_ETHERSCAN_KEY'
  private const BSCSCAN_KEY     = 'NZ6Y3AZJZQ796VHV6BFPS7BUU1XV22XYX3';
  private const POLYGONSCAN_KEY = 'NZ6Y3AZJZQ796VHV6BFPS7BUU1XV22XYX3';
  private const BASESCAN_KEY    = 'NZ6Y3AZJZQ796VHV6BFPS7BUU1XV22XYX3';

  // TronGrid (optional)
  private const TRONGRID_KEY    = '';

  // Blockfrost (Cardano)
  private const BLOCKFROST_PROJECT_ID = 'mainnetCsdqai5BddlIy8mW7M3zjs6RBidiJ8mT';

  // NOWNodes (XVG Blockbook)
  private const XVG_NOWNODES_KEY = '0f6208a1-0480-4e1a-80f5-1d8bb167049a';

  /**
   * =========================
   * ENDPOINTS (BACKEND ONLY)
   * =========================
   */

  // --- EVM scanners (full /api paths) ---
  public static function evm_scan_api(string $net): string {
    $net = strtoupper(preg_replace('/[^A-Z0-9]/', '', (string)$net));
    switch ($net) {
      case 'BEP20':   return 'https://api.bscscan.com/api';
      case 'POLYGON': return 'https://api.polygonscan.com/api';
      case 'BASE':    return 'https://api.basescan.org/api';
      case 'ERC20':
      default:        return 'https://api.etherscan.io/api';
    }
  }

  // Return correct API key for the EVM network
  public static function evm_scan_key(string $net): string {
    $net = strtoupper(preg_replace('/[^A-Z0-9]/', '', (string)$net));

    // use network-specific keys if provided; fallback to etherscan key if you want
    if ($net === 'BEP20')   return self::BSCSCAN_KEY     !== '' ? self::BSCSCAN_KEY     : self::ETHERSCAN_KEY;
    if ($net === 'POLYGON') return self::POLYGONSCAN_KEY !== '' ? self::POLYGONSCAN_KEY : self::ETHERSCAN_KEY;
    if ($net === 'BASE')    return self::BASESCAN_KEY    !== '' ? self::BASESCAN_KEY    : self::ETHERSCAN_KEY;

    return self::ETHERSCAN_KEY;
  }

  // --- TronGrid ---
  public static function trongrid_trc20_transactions_url(string $address): string {
    $address = trim((string)$address);
    return 'https://api.trongrid.io/v1/accounts/' . rawurlencode($address) . '/transactions/trc20';
  }

  public static function trongrid_headers(): array {
    if (self::TRONGRID_KEY === '') return [];
    return ['TRON-PRO-API-KEY' => self::TRONGRID_KEY];
  }

  // --- Stellar Horizon ---
  public static function horizon_base(): string {
    return 'https://horizon.stellar.org';
  }

  // --- XRPSCAN ---
  public static function xrpscan_account_transactions_url(string $address): string {
    $address = trim((string)$address);
    return 'https://api.xrpscan.com/api/v1/account/' . rawurlencode($address) . '/transactions';
  }

  // --- Solana RPC ---
  public static function sol_rpc_url(): string {
    return 'https://api.mainnet-beta.solana.com';
  }

  // --- Blockfrost Cardano ---
  public static function blockfrost_base(): string {
    return 'https://cardano-mainnet.blockfrost.io/api/v0';
  }

  public static function blockfrost_headers(): array {
    if (self::BLOCKFROST_PROJECT_ID === '') return [];
    return [
      'project_id' => self::BLOCKFROST_PROJECT_ID,
      'Accept'     => 'application/json',
    ];
  }

  // --- XVG Blockbook (NOWNodes) ---
  public static function xvg_blockbook_base(): string {
    return 'https://xvg-blockbook.nownodes.io';
  }

  public static function xvg_blockbook_headers(): array {
    if (self::XVG_NOWNODES_KEY === '') return [];
    // send both to avoid "key ignored" issues
    return [
      'Accept'    => 'application/json',
      'api-key'   => self::XVG_NOWNODES_KEY,
      'X-API-Key' => self::XVG_NOWNODES_KEY,
    ];
  }

  // --- chain.so ---
  public static function chainso_get_tx_received_url(string $net, string $address): string {
    return 'https://chain.so/api/v2/get_tx_received/' . rawurlencode($net) . '/' . rawurlencode($address);
  }

  // --- MAZA explorer ---
  public static function maza_explorer_base(): string {
    return 'https://mazacha.in';
  }

  /**
   * Optional helpers so gateway/pollers can decide if "auto-confirm is available".
   */
  public static function has_evm_key(): bool {
    return self::ETHERSCAN_KEY !== '' || self::BSCSCAN_KEY !== '' || self::POLYGONSCAN_KEY !== '' || self::BASESCAN_KEY !== '';
  }

  public static function has_blockfrost(): bool {
    return self::BLOCKFROST_PROJECT_ID !== '';
  }

  public static function has_xvg_key(): bool {
    return self::XVG_NOWNODES_KEY !== '';
  }

  public static function has_trongrid_key(): bool {
    return self::TRONGRID_KEY !== '';
  }
}
