#!/bin/bash
# Generate Remix IDE deployment instructions for MockLinkToken

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$PROJECT_ROOT/scripts/lib/address-inventory.sh"
cd "$PROJECT_ROOT"

load_explorer_runtime_env

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
ACCOUNT=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "")

echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     REMIX IDE DEPLOYMENT INSTRUCTIONS                       ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo "If automated deployment fails, use Remix IDE:"
echo ""
echo "1. Go to: https://remix.ethereum.org"
echo ""
echo "2. Create new file: MockLinkToken.sol"
echo ""
echo "3. Paste this contract code:"
echo ""
cat << 'CONTRACT_EOF'
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

contract MockLinkToken {
    string public name = "Chainlink Token";
    string public symbol = "LINK";
    uint8 public decimals = 18;
    
    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;
    uint256 public totalSupply;
    
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    
    function mint(address to, uint256 amount) external {
        balanceOf[to] += amount;
        totalSupply += amount;
        emit Transfer(address(0), to, amount);
    }
    
    function transfer(address to, uint256 amount) external returns (bool) {
        require(balanceOf[msg.sender] >= amount, "insufficient balance");
        balanceOf[msg.sender] -= amount;
        balanceOf[to] += amount;
        emit Transfer(msg.sender, to, amount);
        return true;
    }
    
    function transferFrom(address from, address to, uint256 amount) external returns (bool) {
        require(balanceOf[from] >= amount, "insufficient balance");
        require(allowance[from][msg.sender] >= amount, "insufficient allowance");
        balanceOf[from] -= amount;
        balanceOf[to] += amount;
        allowance[from][msg.sender] -= amount;
        emit Transfer(from, to, amount);
        return true;
    }
    
    function approve(address spender, uint256 amount) external returns (bool) {
        allowance[msg.sender][spender] = amount;
        emit Approval(msg.sender, spender, amount);
        return true;
    }
}
CONTRACT_EOF

echo ""
echo "4. Compile the contract:"
echo "   - Go to 'Solidity Compiler' tab"
echo "   - Set compiler version to 0.8.19 or compatible"
echo "   - Click 'Compile MockLinkToken.sol'"
echo ""
echo "5. Deploy the contract:"
echo "   - Go to 'Deploy & Run Transactions' tab"
echo "   - Environment: 'Injected Provider - MetaMask' (or 'Custom' with RPC)"
echo "   - Network: ChainID 138"
echo "   - RPC URL: $RPC_URL"
echo "   - Account: $ACCOUNT"
echo "   - Contract: MockLinkToken"
echo "   - Click 'Deploy'"
echo ""
echo "6. After deployment:"
echo "   - Copy the deployed contract address"
echo "   - Mint tokens: call mint($ACCOUNT, 1000000000000000000000000)"
echo "   - Update config/address-inventory.json: LINK_TOKEN=<deployed_address>"
echo ""
echo "7. Alternative: Use Custom RPC in Remix"
echo "   - Environment: 'Custom'"
echo "   - RPC URL: $RPC_URL"
echo "   - Chain ID: 138"
echo "   - Currency Symbol: ETH"
echo "   - Use the same private key from your effective environment (import account in MetaMask first)"
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     QUICK REFERENCE                                          ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo "Network Configuration:"
echo "  RPC URL: $RPC_URL"
echo "  Chain ID: 138"
echo "  Account: $ACCOUNT"
echo ""
echo "After deployment, update config/address-inventory.json:"
echo "  LINK_TOKEN=<deployed_address>"
echo ""
echo "Then run:"
echo "  ./scripts/fund-bridge-contracts.sh 10"
echo ""
