// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Script, console} from "forge-std/Script.sol";
import {RegulatedEntityRegistry} from "../../../contracts/vault/RegulatedEntityRegistry.sol";
import {XAUOracle} from "../../../contracts/vault/XAUOracle.sol";
import {RateAccrual} from "../../../contracts/vault/RateAccrual.sol";
import {Ledger} from "../../../contracts/vault/Ledger.sol";
import {Liquidation} from "../../../contracts/vault/Liquidation.sol";
import {CollateralAdapter} from "../../../contracts/vault/adapters/CollateralAdapter.sol";
import {eMoneyJoin} from "../../../contracts/vault/adapters/eMoneyJoin.sol";
import {Aggregator} from "../../../contracts/oracle/Aggregator.sol";

/// @notice Deploy vault system core on ALL Mainnet (651940) without VaultFactory (forge broadcast workaround).
contract DeployVaultSystem651940Core is Script {
    uint256 public constant DEFAULT_LIQUIDATION_RATIO = 10000;
    uint256 public constant DEFAULT_CREDIT_MULTIPLIER = 50000;
    uint256 public constant DEFAULT_DEBT_CEILING = 1000000e18;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address admin = vm.addr(pk);
        address treasury = vm.envOr("TREASURY_ADDRESS", admin);
        address ethAddress = address(0);

        vm.startBroadcast(pk);

        console.log("=== [651940] Vault system core ===");
        RegulatedEntityRegistry entityRegistry = new RegulatedEntityRegistry(admin);
        console.log("RegulatedEntityRegistry:", address(entityRegistry));

        Aggregator ethPriceFeed = new Aggregator("ETH/XAU", admin, 3600, 50);
        ethPriceFeed.addTransmitter(admin);
        ethPriceFeed.updateAnswer(0.05e18);
        console.log("ETH Price Feed:", address(ethPriceFeed));

        Aggregator btcPriceFeed = new Aggregator("BTC/XAU", admin, 3600, 50);
        btcPriceFeed.addTransmitter(admin);
        btcPriceFeed.updateAnswer(0.5e18);
        console.log("BTC Price Feed:", address(btcPriceFeed));

        XAUOracle xauOracle = new XAUOracle(admin);
        xauOracle.addPriceFeed(address(ethPriceFeed), 10000);
        xauOracle.addPriceFeed(address(btcPriceFeed), 10000);
        xauOracle.updatePrice();
        console.log("XAU Oracle:", address(xauOracle));

        RateAccrual rateAccrual = new RateAccrual(admin);
        rateAccrual.setInterestRate(address(0), 500);
        console.log("Rate Accrual:", address(rateAccrual));

        Ledger ledger = new Ledger(admin, address(xauOracle), address(rateAccrual));
        console.log("Ledger:", address(ledger));

        Liquidation liquidation = new Liquidation(admin, address(ledger), address(xauOracle), treasury);
        console.log("Liquidation:", address(liquidation));

        CollateralAdapter collateralAdapter = new CollateralAdapter(admin, address(ledger));
        console.log("Collateral Adapter:", address(collateralAdapter));

        eMoneyJoin eMoneyJoinContract = new eMoneyJoin(admin);
        console.log("eMoney Join:", address(eMoneyJoinContract));

        ledger.setRiskParameters(
            ethAddress, DEFAULT_DEBT_CEILING, DEFAULT_LIQUIDATION_RATIO, DEFAULT_CREDIT_MULTIPLIER
        );

        vm.stopBroadcast();
        console.log("=== [651940] core complete (VaultFactory separate) ===");
    }
}
