import { PassportSDK } from '../src/PassportSDK';
import { defineChain } from 'thirdweb/chains';
import { createThirdwebClient } from 'thirdweb';

// 创建链配置
const tRexTestnet = defineChain({
  id: 1962,
  name: 'T-Rex Testnet',
  nativeCurrency: {
    decimals: 18,
    name: 'Ether',
    symbol: 'ETH',
  },
  rpc: 'https://testnetrpc.trex.xyz',
  blockExplorers: [
    {
      name: 'T-Rex Explorer',
      url: 'https://explorer.trex.com',
    },
  ],
  testnet: true,
});

// 创建 thirdweb 客户端
const client = createThirdwebClient({
  clientId: 'your-client-id',
});

// 创建 Passport SDK 实例
const passportSDK = new PassportSDK({
  chain: tRexTestnet,
  registryAddress: '0x1B326360Ec9E3cEF6129173D35b86a6803e5751F',
  client: client,
});

// 使用示例
async function example() {
  const walletAddress = '0x...'; // 用户钱包地址
  
  try {
    // 1. 检查钱包是否有 Passport
    console.log('Checking if wallet has passport...');
    const hasPassport = await passportSDK.checkWalletHasPassport(walletAddress);
    
    if (hasPassport.hasPassport) {
      console.log('Passport found!');
      console.log('Passport ID:', hasPassport.passportId);
      console.log('Passport Address:', hasPassport.passportAddress);
      
      // 2. 获取 Passport 详细信息
      const passportInfo = await passportSDK.getPassportInfo(hasPassport.passportAddress!);
      console.log('Bound Wallets:', passportInfo.boundWallets);
      console.log('Wallet Count:', passportInfo.walletCount);
      
      // 3. 获取待处理绑定钱包列表
      const pendingWallets = await passportSDK.getPendingBindWallets(hasPassport.passportAddress!, walletAddress);
      console.log('Pending Bind Wallets:', pendingWallets);
      
      // 4. 查询待处理绑定请求
      const pendingRequest = await passportSDK.getPendingBindRequest(hasPassport.passportAddress!, walletAddress);
      if (pendingRequest.exists) {
        console.log('Pending request found from:', pendingRequest.requester);
      }
      
    } else {
      console.log('No passport found for this wallet');
      
      // 5. 预测 Passport 地址
      const predictedAddress = await passportSDK.predictPassportAddress(walletAddress);
      console.log('Predicted Passport Address:', predictedAddress);
    }
    
  } catch (error) {
    console.error('Error:', error);
  }
}

// 交易示例（需要用户签名）
async function transactionExample() {
  const walletAddress = '0x...'; // 用户钱包地址
  const passportAddress = '0x...'; // Passport 地址
  
  // 注意：实际使用中需要提供真实的账户对象
  // const account = privateKeyToAccount('0xYOUR_PRIVATE_KEY');
  // 或者从钱包连接中获取账户
  
  try {
    // 方式1: 只准备交易，不发送（原有方式）
    console.log('Creating passport...');
    const createTransaction = await passportSDK.createPassport();
    console.log('Prepared transaction:', createTransaction.preparedTransaction);
    // 然后可以使用 React Hook 的 sendTransaction 发送：
    // const result = await sendTransaction(createTransaction.preparedTransaction);
    
    // 方式2: 直接发送交易（新功能）
    console.log('Creating passport with auto-send...');
    const createResult = await passportSDK.createPassport({ 
      sendTransaction: true,
      // account: account // 传入账户用于签名
    });
    console.log('Transaction sent! Hash:', createResult.transactionHash);
    
    // 2. 请求绑定钱包
    console.log('Requesting to bind wallet...');
    const bindResult = await passportSDK.requestBindWallet(passportAddress, walletAddress, {
      sendTransaction: true,
      // account: account
    });
    console.log('Bind request sent! Hash:', bindResult.transactionHash);
    
    // 3. 解绑钱包
    console.log('Unbinding wallet...');
    const unbindResult = await passportSDK.unbindWallet(passportAddress, {
      sendTransaction: true,
      // account: account
    });
    console.log('Unbind transaction sent! Hash:', unbindResult.transactionHash);
    
    // 4. 接受绑定请求
    console.log('Accepting bind request...');
    const acceptResult = await passportSDK.acceptBindRequest(passportAddress, {
      sendTransaction: true,
      // account: account
    });
    console.log('Accept transaction sent! Hash:', acceptResult.transactionHash);
    
    // 5. 拒绝绑定请求
    console.log('Rejecting bind request...');
    const rejectResult = await passportSDK.rejectBindRequest(passportAddress, {
      sendTransaction: true,
      // account: account
    });
    console.log('Reject transaction sent! Hash:', rejectResult.transactionHash);
    
    // 6. 取消绑定请求
    console.log('Cancelling bind request...');
    const cancelResult = await passportSDK.cancelBindRequest(passportAddress, walletAddress, {
      sendTransaction: true,
      // account: account
    });
    console.log('Cancel transaction sent! Hash:', cancelResult.transactionHash);
    
  } catch (error) {
    console.error('Error:', error);
  }
}

// 运行示例
// example();
// transactionExample(); 