/**
 * Deploy TestUSDT to KAVA Testnet
 *
 * Usage:
 *   npx ts-node scripts/deploy-test-usdt-kava.ts <PRIVATE_KEY>
 *
 * Example:
 *   npx ts-node scripts/deploy-test-usdt-kava.ts 0xabc123...
 *
 * Get KAVA Testnet TKAVA from: https://faucet.kava.io
 */

import { ethers, ContractFactory } from 'ethers';

// KAVA Testnet 配置
const KAVA_TESTNET_RPC = 'https://evm.testnet.kava.io';
const KAVA_TESTNET_CHAIN_ID = 2221;

// TestUSDT 合约 ABI
const CONTRACT_ABI = [
  'constructor()',
  'function name() view returns (string)',
  'function symbol() view returns (string)',
  'function decimals() view returns (uint8)',
  'function totalSupply() view returns (uint256)',
  'function balanceOf(address) view returns (uint256)',
  'function transfer(address to, uint256 amount) returns (bool)',
  'function approve(address spender, uint256 amount) returns (bool)',
  'function allowance(address owner, address spender) view returns (uint256)',
  'function transferFrom(address from, address to, uint256 amount) returns (bool)',
  'function mint(uint256 amount)',
  'function mintUsdt(uint256 usdtAmount)',
  'function mintTo(address to, uint256 amount)',
  'function faucet()',
  'function owner() view returns (address)',
  'event Transfer(address indexed from, address indexed to, uint256 value)',
  'event Approval(address indexed owner, address indexed spender, uint256 value)',
];

// TestUSDT_Flat.sol 编译后的 bytecode
const CONTRACT_BYTECODE = `[encoded data, 12356 characters]`;

async function main() {
  const privateKey = process.argv[2];

  if (!privateKey) {
    console.error(`
❌ Usage: npx ts-node scripts/deploy-test-usdt-kava.ts <PRIVATE_KEY>

Steps:
1. Get TKAVA from https://faucet.kava.io
2. Export your private key from MetaMask
3. Run: npx ts-node scripts/deploy-test-usdt-kava.ts 0xYourPrivateKey
    `);
    process.exit(1);
  }

  console.log('🚀 Deploying TestUSDT to KAVA Testnet...\n');

  // 连接到 KAVA Testnet
  const provider = new ethers.JsonRpcProvider(KAVA_TESTNET_RPC, {
    chainId: KAVA_TESTNET_CHAIN_ID,
    name: 'kava-testnet',
  });

  // 创建钱包
  const wallet = new ethers.Wallet(privateKey, provider);
  console.log(`📍 Deployer address: ${wallet.address}`);

  // 检查余额
  const balance = await provider.getBalance(wallet.address);
  console.log(`💰 Balance: ${ethers.formatEther(balance)} TKAVA`);

  if (balance < ethers.parseEther('0.01')) {
    console.error('\n❌ Insufficient TKAVA. Get some from https://faucet.kava.io');
    process.exit(1);
  }

  // 部署合约
  console.log('\n📦 Deploying contract...');
  const factory = new ContractFactory(CONTRACT_ABI, CONTRACT_BYTECODE, wallet);
  const contract = await factory.deploy();

  console.log(`⏳ Waiting for confirmation...`);
  console.log(`   Transaction: https://testnet.kavascan.com/tx/${contract.deploymentTransaction()?.hash}`);

  await contract.waitForDeployment();
  const contractAddress = await contract.getAddress();

  console.log(`
✅ TestUSDT deployed successfully on KAVA Testnet!

📋 Contract Address: ${contractAddress}
🔗 KavaScan: https://testnet.kavascan.com/address/${contractAddress}

Next steps:
1. Update .env: KAVA_USDT_CONTRACT=${contractAddress}
2. Call faucet() to mint 10,000 USDT for testing
3. Or call mintUsdt(100000) to mint 100,000 USDT
  `);
}

main().catch((error) => {
  console.error('❌ Deployment failed:', error.message);
  process.exit(1);
});
