import { ethers, upgrades } from "hardhat";
import * as fs from "fs";
import * as path from "path";

/**
 * 配置 Lending 借贷池参数并部署代理
 * 包含：设置工厂、配置市场参数、添加抵押资产、部署 Lending 代理
 */
async function main() {
    const [deployer] = await ethers.getSigners();
    console.log("\n==========================================");
    console.log("⚙️  配置 Lending 借贷池");
    console.log("==========================================");
    console.log("配置账户:", deployer.address, "\n");

    // ========== 读取部署信息 ==========
    const deploymentsPath = path.join(__dirname, "../../deployments-lending.json");
    if (!fs.existsSync(deploymentsPath)) {
        throw new Error("未找到部署信息文件，请先运行 07-deployLending.ts");
    }
    
    const network = await ethers.provider.getNetwork();
    const chainId = network.chainId.toString();
    const deployments = JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"))[chainId];
    
    if (!deployments) {
        throw new Error(`未找到网络 ${chainId} 的部署信息`);
    }

    console.log("📋 使用已部署的合约:");
    console.log("  LendingFactory:", deployments.lendingFactory);
    console.log("  Configurator:", deployments.configuratorProxy);
    console.log("  Lending Impl:", deployments.lendingImpl, "\n");

    const configurator = await ethers.getContractAt("Configurator", deployments.configuratorProxy);
    const lendingFactory = await ethers.getContractAt("LendingFactory", deployments.lendingFactory);

    // ========== 第一阶段：配置外部代币和价格源 ==========
    console.log("⚙️  Phase 1: 配置外部代币和价格源");
    
    // 这里使用示例地址，实际部署时需要替换为真实地址
    // 如果你已有 WUSD 等合约，请从部署文件中读取
    const USDC = {
        address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // Mainnet USDC (示例)
        decimals: 6
    };
    
    const WETH = {
        address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // Mainnet WETH (示例)
        decimals: 18
    };
    
    // 价格预言机地址（需要部署或使用 Chainlink）
    const usdcPriceFeed = "0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6"; // Chainlink USDC/USD (示例)
    const ethPriceFeed = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";  // Chainlink ETH/USD (示例)
    
    console.log("  基础资产 (USDC):", USDC.address);
    console.log("  抵押资产 (WETH):", WETH.address);
    console.log("  USDC 价格源:", usdcPriceFeed);
    console.log("  ETH 价格源:", ethPriceFeed, "\n");

    // ========== 第二阶段：准备配置参数 ==========
    console.log("⚙️  Phase 2: 准备市场配置参数");
    
    // 使用占位符地址作为 Lending 代理地址
    const LENDING_PROXY_PLACEHOLDER = ethers.ZeroAddress;
    
    const configuration = {
        baseToken: USDC.address,
        baseTokenPriceFeed: usdcPriceFeed,
        
        // 利率模型参数（年化利率，18位精度）
        // 注意：这些年化利率会在 initialize 时自动转换为每秒利率
        // 转换公式：perSecondRate = perYearRate / 31,536,000
        supplyKink: ethers.parseUnits("0.8", 18),                        // 80% 利用率拐点
        supplyPerYearInterestRateSlopeLow: ethers.parseUnits("0.03", 18),   // 3% APY
        supplyPerYearInterestRateSlopeHigh: ethers.parseUnits("0.4", 18),   // 40% APY
        supplyPerYearInterestRateBase: ethers.parseUnits("0", 18),          // 0% 基础
        
        borrowKink: ethers.parseUnits("0.8", 18),                        // 80% 利用率拐点
        borrowPerYearInterestRateSlopeLow: ethers.parseUnits("0.05", 18),   // 5% APY
        borrowPerYearInterestRateSlopeHigh: ethers.parseUnits("1.5", 18),   // 150% APY
        borrowPerYearInterestRateBase: ethers.parseUnits("0.015", 18),      // 1.5% 基础
        
        storeFrontPriceFactor: ethers.parseUnits("0.5", 18),             // 50% 清算折扣
        trackingIndexScale: ethers.parseUnits("1", 15),                  // 10^15 比例
        baseBorrowMin: ethers.parseUnits("100", USDC.decimals),          // 最小借 100 USDC
        targetReserves: ethers.parseUnits("5000000", USDC.decimals),     // 目标储备 500 万
        
        assetConfigs: [
            {
                asset: WETH.address,
                priceFeed: ethPriceFeed,
                decimals: WETH.decimals,
                borrowCollateralFactor: ethers.parseUnits("0.80", 18),       // 80% LTV
                liquidateCollateralFactor: ethers.parseUnits("0.85", 18),    // 85% 清算线
                liquidationFactor: ethers.parseUnits("0.95", 18),            // 95% (配合 storeFrontPriceFactor 产生折扣)
                supplyCap: ethers.parseUnits("100000", WETH.decimals)        // 最多 10 万 ETH
            }
        ]
    };
    
    console.log("✅ 配置参数已准备\n");

    // ========== 第三阶段：设置工厂和配置 ==========
    console.log("⚙️  Phase 3: 设置工厂和配置参数");
    
    console.log("  设置工厂合约...");
    const setFactoryTx = await configurator.setFactory(LENDING_PROXY_PLACEHOLDER, deployments.lendingFactory);
    await setFactoryTx.wait();
    console.log("  ✅ 工厂已设置");
    
    console.log("  设置市场配置...");
    const setConfigTx = await configurator.setConfiguration(LENDING_PROXY_PLACEHOLDER, configuration);
    await setConfigTx.wait();
    console.log("  ✅ 配置已设置\n");

    // ========== 第四阶段：部署 Lending 代理 ==========
    console.log("⚙️  Phase 4: 部署 Lending 代理");
    
    console.log("  获取配置信息...");
    const config = await configurator.getConfiguration(LENDING_PROXY_PLACEHOLDER);
    
    console.log("  部署 Lending 代理...");
    const Lending = await ethers.getContractFactory("Lending");
    
    // 使用 upgrades 插件部署 UUPS 代理
    const lending = await upgrades.deployProxy(
        Lending,
        [config],
        {
            kind: "uups",
            initializer: "initialize"
        }
    );
    await lending.waitForDeployment();
    const lendingProxyAddress = await lending.getAddress();
    console.log("  ✅ Lending Proxy 已部署:", lendingProxyAddress);
    
    // 获取实现合约地址（验证）
    const lendingImplAddress = await upgrades.erc1967.getImplementationAddress(lendingProxyAddress);
    console.log("  ✅ Lending Implementation (验证):", lendingImplAddress, "\n");

    // ========== 第五阶段：验证部署 ==========
    console.log("✨ Phase 5: 验证部署");
    console.log("  Lending Owner:", await lending.owner());
    console.log("  Lending Paused:", await lending.paused());
    console.log("  Base Token:", await lending.baseToken());
    console.log("  Total Supply:", ethers.formatUnits(await lending.totalSupply(), USDC.decimals));
    console.log("  Total Borrow:", ethers.formatUnits(await lending.totalBorrow(), USDC.decimals));
    
    const supplyRate = await lending.getSupplyRate();
    const borrowRate = await lending.getBorrowRate();
    // 注意：返回的是每秒利率，需要乘以一年的秒数得到 APY
    const SECONDS_PER_YEAR = 365 * 24 * 60 * 60;
    console.log("  Supply Rate (per second):", ethers.formatUnits(supplyRate, 18));
    console.log("  Supply APY:", ethers.formatUnits(BigInt(supplyRate) * BigInt(SECONDS_PER_YEAR), 18));
    console.log("  Borrow Rate (per second):", ethers.formatUnits(borrowRate, 18));
    console.log("  Borrow APY:", ethers.formatUnits(BigInt(borrowRate) * BigInt(SECONDS_PER_YEAR), 18), "\n");

    // ========== 保存部署信息 ==========
    deployments.lendingProxy = lendingProxyAddress;
    deployments.configTimestamp = new Date().toISOString();
    
    const allDeployments = JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"));
    allDeployments[chainId] = deployments;
    fs.writeFileSync(deploymentsPath, JSON.stringify(allDeployments, null, 2));
    
    console.log("💾 配置信息已保存到:", deploymentsPath);

    // ========== 配置总结 ==========
    console.log("\n🎉 配置总结:");
    console.log("=====================================");
    console.log("Lending Proxy:             ", lendingProxyAddress);
    console.log("Base Token:                ", USDC.address);
    console.log("Collateral Assets:         ", configuration.assetConfigs.length);
    console.log("Supply Kink:               ", "80%");
    console.log("Borrow Kink:               ", "80%");
    console.log("Min Borrow:                ", "100 USDC");
    console.log("=====================================\n");
    
    console.log("✅ Lending 借贷池已完全配置完成！");
    console.log("📝 后续步骤:");
    console.log("  1. 用户可以调用 supply() 存入 USDC");
    console.log("  2. 用户可以调用 supplyCollateral() 存入 WETH");
    console.log("  3. 用户可以调用 borrow() 借出 USDC");
    console.log("  4. 清算人可以调用 absorb() 清算不良贷款");
    console.log("  5. 清算人可以调用 buyCollateral() 购买清算抵押品\n");
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

