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

import {Script, console} from "forge-std/Script.sol";
import {ReserveSystem} from "../../contracts/reserve/ReserveSystem.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/**
 * @title ConfigureInitialReserves
 * @notice Script to configure initial reserve assets for Reserve System
 * @dev Sets up supported assets, liquidity flags, and initial deposits
 */
contract ConfigureInitialReserves is Script {
    function run() external {
        uint256 chainId = block.chainid;
        require(chainId == 138, "This script is for ChainID 138 only");

        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(deployerPrivateKey);

        address deployer = vm.addr(deployerPrivateKey);
        console.log("=== Configure Initial Reserves (ChainID 138) ===");
        console.log("Deployer:", deployer);
        console.log("");

        // Load addresses from environment
        address reserveSystem = vm.envAddress("RESERVE_SYSTEM");
        address admin = vm.envOr("RESERVE_ADMIN", deployer);
        address reserveManager = vm.envOr("RESERVE_MANAGER", deployer);

        ReserveSystem reserve = ReserveSystem(reserveSystem);

        console.log("=== Adding Supported Assets ===");

        // Asset 1: Gold (XAU) - Liquid asset
        address xauAsset = vm.envOr("XAU_ASSET", address(0));
        if (xauAsset != address(0)) {
            console.log("Adding XAU asset:", xauAsset);
            vm.prank(admin);
            reserve.addSupportedAsset(xauAsset, true); // Liquid
            console.log("XAU added as liquid asset");
        }

        // Asset 2: USDC - Liquid asset
        address usdcAsset = vm.envOr("USDC_ASSET", address(0));
        if (usdcAsset != address(0)) {
            console.log("Adding USDC asset:", usdcAsset);
            vm.prank(admin);
            reserve.addSupportedAsset(usdcAsset, true); // Liquid
            console.log("USDC added as liquid asset");
        }

        // Asset 3: ETH - Liquid asset
        address ethAsset = vm.envOr("ETH_ASSET", address(0));
        if (ethAsset != address(0)) {
            console.log("Adding ETH asset:", ethAsset);
            vm.prank(admin);
            reserve.addSupportedAsset(ethAsset, true); // Liquid
            console.log("ETH added as liquid asset");
        }

        // Asset 4: WETH (if different from ETH)
        address wethAsset = vm.envOr("WETH_ASSET", address(0));
        if (wethAsset != address(0) && wethAsset != ethAsset) {
            console.log("Adding WETH asset:", wethAsset);
            vm.prank(admin);
            reserve.addSupportedAsset(wethAsset, true); // Liquid
            console.log("WETH added as liquid asset");
        }

        // Asset 5: Sovereign Instruments (example - less liquid)
        address sovereignAsset = vm.envOr("SOVEREIGN_ASSET", address(0));
        if (sovereignAsset != address(0)) {
            console.log("Adding Sovereign Instrument asset:", sovereignAsset);
            vm.prank(admin);
            reserve.addSupportedAsset(sovereignAsset, false); // Less liquid
            console.log("Sovereign Instrument added as less liquid asset");
        }

        console.log("");
        console.log("=== Initial Reserve Deposits ===");
        console.log("Note: Ensure assets are approved and sufficient balance exists");

        // Deposit XAU reserves (if configured)
        if (xauAsset != address(0)) {
            uint256 xauAmount = vm.envOr("XAU_INITIAL_DEPOSIT", uint256(0));
            if (xauAmount > 0) {
                console.log("Depositing XAU reserves:", xauAmount);
                IERC20(xauAsset).approve(reserveSystem, xauAmount);
                vm.prank(reserveManager);
                reserve.depositReserve(xauAsset, xauAmount);
                console.log("XAU deposit complete");
            }
        }

        // Deposit USDC reserves (if configured)
        if (usdcAsset != address(0)) {
            uint256 usdcAmount = vm.envOr("USDC_INITIAL_DEPOSIT", uint256(0));
            if (usdcAmount > 0) {
                console.log("Depositing USDC reserves:", usdcAmount);
                IERC20(usdcAsset).approve(reserveSystem, usdcAmount);
                vm.prank(reserveManager);
                reserve.depositReserve(usdcAsset, usdcAmount);
                console.log("USDC deposit complete");
            }
        }

        // Deposit ETH reserves (if configured)
        if (ethAsset != address(0)) {
            uint256 ethAmount = vm.envOr("ETH_INITIAL_DEPOSIT", uint256(0));
            if (ethAmount > 0) {
                console.log("Depositing ETH reserves:", ethAmount);
                IERC20(ethAsset).approve(reserveSystem, ethAmount);
                vm.prank(reserveManager);
                reserve.depositReserve(ethAsset, ethAmount);
                console.log("ETH deposit complete");
            }
        }

        // Deposit WETH reserves (if configured)
        if (wethAsset != address(0)) {
            uint256 wethAmount = vm.envOr("WETH_INITIAL_DEPOSIT", uint256(0));
            if (wethAmount > 0) {
                console.log("Depositing WETH reserves:", wethAmount);
                IERC20(wethAsset).approve(reserveSystem, wethAmount);
                vm.prank(reserveManager);
                reserve.depositReserve(wethAsset, wethAmount);
                console.log("WETH deposit complete");
            }
        }

        console.log("");
        console.log("=== Reserve Balances ===");

        if (xauAsset != address(0)) {
            uint256 xauBalance = reserve.getReserveBalance(xauAsset);
            console.log("XAU Reserve Balance:", xauBalance);
        }

        if (usdcAsset != address(0)) {
            uint256 usdcBalance = reserve.getReserveBalance(usdcAsset);
            console.log("USDC Reserve Balance:", usdcBalance);
        }

        if (ethAsset != address(0)) {
            uint256 ethBalance = reserve.getReserveBalance(ethAsset);
            console.log("ETH Reserve Balance:", ethBalance);
        }

        if (wethAsset != address(0)) {
            uint256 wethBalance = reserve.getReserveBalance(wethAsset);
            console.log("WETH Reserve Balance:", wethBalance);
        }

        console.log("");
        console.log("=== Configuration Complete ===");
        console.log("");
        console.log("=== Supported Assets ===");
        address[] memory supportedAssets = reserve.getSupportedAssets();
        for (uint256 i = 0; i < supportedAssets.length; i++) {
            console.log("Asset", i, ":", supportedAssets[i]);
        }

        vm.stopBroadcast();
    }
}

