/**
 * Trustless Bridge Relayer Service
 * 
 * Monitors Deposit events from Lockbox138 on ChainID 138
 * Submits claims to InboxETH on Ethereum Mainnet with required bonds
 * 
 * Permissionless: Anyone can run this service to relay deposits
 */

const { ethers } = require('ethers');
require('dotenv').config();

// Configuration
const CONFIG = {
  // ChainID 138 (Besu)
  CHAIN138_RPC: process.env.CHAIN138_RPC_URL || 'https://rpc.d-bis.org',
  LOCKBOX138_ADDRESS: process.env.LOCKBOX138_ADDRESS,
  
  // Ethereum Mainnet
  ETHEREUM_RPC: process.env.ETHEREUM_RPC_URL || 'https://eth.llamarpc.com',
  INBOX_ETH_ADDRESS: process.env.INBOX_ETH_ADDRESS,
  BOND_MANAGER_ADDRESS: process.env.BOND_MANAGER_ADDRESS,
  
  // Relayer configuration
  RELAYER_PRIVATE_KEY: process.env.RELAYER_PRIVATE_KEY,
  MIN_BOND_BALANCE: ethers.parseEther(process.env.MIN_BOND_BALANCE || '10'), // Minimum ETH balance to maintain
  MAX_CLAIMS_PER_EPOCH: parseInt(process.env.MAX_CLAIMS_PER_EPOCH || '100'),
  
  // Polling interval (milliseconds)
  POLL_INTERVAL: parseInt(process.env.POLL_INTERVAL || '5000'), // 5 seconds
};

// Lockbox138 ABI (simplified - Deposit event)
const LOCKBOX138_ABI = [
  "event Deposit(uint256 indexed depositId, address indexed asset, uint256 amount, address indexed recipient, bytes32 nonce, address depositor, uint256 timestamp)"
];

// InboxETH ABI
const INBOX_ETH_ABI = [
  "function submitClaim(uint256 depositId, address asset, uint256 amount, address recipient, bytes calldata proof) external payable returns (uint256 bondAmount)",
  "function getClaimStatus(uint256 depositId) external view returns (bool exists, bool finalized, bool challenged, uint256 challengeWindowEnd)",
  "function getClaim(uint256 depositId) external view returns (tuple(uint256 depositId, address asset, uint256 amount, address recipient, address relayer, uint256 timestamp, bool exists))"
];

// BondManager ABI
const BOND_MANAGER_ABI = [
  "function getRequiredBond(uint256 depositAmount) external view returns (uint256)"
];

class TrustlessBridgeRelayer {
  constructor() {
    // Initialize providers
    this.chain138Provider = new ethers.JsonRpcProvider(CONFIG.CHAIN138_RPC);
    this.ethereumProvider = new ethers.JsonRpcProvider(CONFIG.ETHEREUM_RPC);
    
    // Initialize wallet (relayer)
    if (!CONFIG.RELAYER_PRIVATE_KEY) {
      throw new Error('RELAYER_PRIVATE_KEY environment variable is required');
    }
    this.wallet = new ethers.Wallet(CONFIG.RELAYER_PRIVATE_KEY, this.ethereumProvider);
    
    // Initialize contracts
    this.lockbox138 = new ethers.Contract(CONFIG.LOCKBOX138_ADDRESS, LOCKBOX138_ABI, this.chain138Provider);
    this.inboxETH = new ethers.Contract(CONFIG.INBOX_ETH_ADDRESS, INBOX_ETH_ABI, this.wallet);
    this.bondManager = new ethers.Contract(CONFIG.BOND_MANAGER_ADDRESS, BOND_MANAGER_ABI, this.ethereumProvider);
    
    // Track processed deposits
    this.processedDeposits = new Set();
    this.claimsThisEpoch = 0;
    this.epochStartTime = Date.now();
    this.EPOCH_DURATION = 3600000; // 1 hour in milliseconds
  }

  async start() {
    console.log('Starting Trustless Bridge Relayer...');
    console.log('Relayer Address:', this.wallet.address);
    console.log('ChainID 138 RPC:', CONFIG.CHAIN138_RPC);
    console.log('Ethereum RPC:', CONFIG.ETHEREUM_RPC);
    console.log('Lockbox138:', CONFIG.LOCKBOX138_ADDRESS);
    console.log('InboxETH:', CONFIG.INBOX_ETH_ADDRESS);
    
    // Check relayer balance
    await this.checkBalance();
    
    // Start monitoring
    this.monitorDeposits();
  }

  async checkBalance() {
    const balance = await this.ethereumProvider.getBalance(this.wallet.address);
    console.log('Relayer Balance:', ethers.formatEther(balance), 'ETH');
    
    if (balance < CONFIG.MIN_BOND_BALANCE) {
      console.warn(`Warning: Balance below minimum (${ethers.formatEther(CONFIG.MIN_BOND_BALANCE)} ETH)`);
      console.warn('Please fund the relayer address to continue relaying claims');
    }
  }

  async monitorDeposits() {
    console.log('Monitoring Deposit events from Lockbox138...');
    
    // Listen for new Deposit events
    this.lockbox138.on('Deposit', async (depositId, asset, amount, recipient, nonce, depositor, timestamp, event) => {
      try {
        await this.handleDeposit(depositId, asset, amount, recipient, nonce, depositor, timestamp);
      } catch (error) {
        console.error('Error handling deposit:', error);
      }
    });

    // Also poll for past events (catch up on missed deposits)
    setInterval(async () => {
      try {
        await this.pollPastDeposits();
        await this.resetEpochIfNeeded();
      } catch (error) {
        console.error('Error polling deposits:', error);
      }
    }, CONFIG.POLL_INTERVAL);
  }

  async pollPastDeposits() {
    // Get recent blocks
    const currentBlock = await this.chain138Provider.getBlockNumber();
    const fromBlock = Math.max(currentBlock - 100, 0); // Last 100 blocks
    
    const filter = this.lockbox138.filters.Deposit();
    const events = await this.lockbox138.queryFilter(filter, fromBlock, currentBlock);
    
    for (const event of events) {
      const { depositId, asset, amount, recipient, nonce, depositor, timestamp } = event.args;
      await this.handleDeposit(depositId, asset, amount, recipient, nonce, depositor, timestamp);
    }
  }

  async handleDeposit(depositId, asset, amount, recipient, nonce, depositor, timestamp) {
    const depositIdStr = depositId.toString();
    
    // Skip if already processed
    if (this.processedDeposits.has(depositIdStr)) {
      return;
    }

    console.log(`\n=== New Deposit Detected ===`);
    console.log('Deposit ID:', depositIdStr);
    console.log('Asset:', asset);
    console.log('Amount:', ethers.formatEther(amount), 'ETH');
    console.log('Recipient:', recipient);
    console.log('Depositor:', depositor);
    console.log('Timestamp:', new Date(Number(timestamp) * 1000).toISOString());

    // Check if claim already exists on Ethereum
    const [exists] = await this.inboxETH.getClaimStatus(depositId);
    if (exists) {
      console.log('Claim already exists on Ethereum, skipping...');
      this.processedDeposits.add(depositIdStr);
      return;
    }

    // Check epoch limits
    if (this.claimsThisEpoch >= CONFIG.MAX_CLAIMS_PER_EPOCH) {
      console.log('Epoch limit reached, skipping...');
      return;
    }

    // Submit claim
    try {
      await this.submitClaim(depositId, asset, amount, recipient);
      this.processedDeposits.add(depositIdStr);
      this.claimsThisEpoch++;
    } catch (error) {
      console.error('Failed to submit claim:', error);
    }
  }

  async submitClaim(depositId, asset, amount, recipient) {
    console.log('Submitting claim to InboxETH...');
    
    // Get required bond amount
    const requiredBond = await this.bondManager.getRequiredBond(amount);
    console.log('Required Bond:', ethers.formatEther(requiredBond), 'ETH');
    
    // Check balance
    const balance = await this.ethereumProvider.getBalance(this.wallet.address);
    if (balance < requiredBond) {
      throw new Error(`Insufficient balance. Required: ${ethers.formatEther(requiredBond)} ETH, Have: ${ethers.formatEther(balance)} ETH`);
    }
    
    // Submit claim
    const tx = await this.inboxETH.submitClaim(
      depositId,
      asset,
      amount,
      recipient,
      '0x', // Empty proof for optimistic model
      { value: requiredBond }
    );
    
    console.log('Transaction submitted:', tx.hash);
    const receipt = await tx.wait();
    console.log('Transaction confirmed in block:', receipt.blockNumber);
    console.log('Claim submitted successfully!');
  }

  async resetEpochIfNeeded() {
    const now = Date.now();
    if (now - this.epochStartTime >= this.EPOCH_DURATION) {
      console.log('Resetting epoch...');
      this.claimsThisEpoch = 0;
      this.epochStartTime = now;
    }
  }
}

// Start relayer
if (require.main === module) {
  const relayer = new TrustlessBridgeRelayer();
  relayer.start().catch(console.error);
}

module.exports = TrustlessBridgeRelayer;
