import solc from 'solc';
import fs from 'fs';

const source = fs.readFileSync('FutureUSDT.sol', 'utf8');

const input = {
    language: 'Solidity',
    sources: {
        'FutureUSDT.sol': {
            content: source
        }
    },
    settings: {
        optimizer: {
            enabled: true,
            runs: 200
        },
        evmVersion: 'paris', // Use paris to avoid PUSH0
        outputSelection: {
            '*': {
                '*': ['abi', 'evm.bytecode']
            }
        }
    }
};

const output = JSON.parse(solc.compile(JSON.stringify(input)));

if (output.errors) {
    output.errors.forEach(err => {
        console.log(err.formattedMessage);
    });

    // Check for actual errors (not just warnings)
    const hasErrors = output.errors.some(err => err.severity === 'error');
    if (hasErrors) {
        process.exit(1);
    }
}

const contract = output.contracts['FutureUSDT.sol']['FutureUSDT'];
const bytecode = contract.evm.bytecode.object;
const abi = contract.abi;

fs.mkdirSync('build', { recursive: true });
fs.writeFileSync('build/FutureUSDT.bin', bytecode);
fs.writeFileSync('build/FutureUSDT.abi', JSON.stringify(abi, null, 2));

console.log('Compiled successfully!');
console.log('Bytecode length:', bytecode.length);
console.log('ABI functions:', abi.filter(x => x.type === 'function').map(x => x.name).join(', '));
