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

import {Script, console} from "forge-std/Script.sol";
import {OraclePriceFeed} from "../../contracts/reserve/OraclePriceFeed.sol";
import {MockPriceFeed} from "../../contracts/reserve/MockPriceFeed.sol";

/**
 * @title SetupPriceFeeds
 * @notice Script to set up price feeds for Reserve System
 * @dev Configures aggregators and initial prices for supported assets
 */
contract SetupPriceFeeds 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("=== Setup Price Feeds (ChainID 138) ===");
        console.log("Deployer:", deployer);
        console.log("");

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

        // Deploy OraclePriceFeed if not provided
        if (oraclePriceFeed == address(0)) {
            console.log("Deploying OraclePriceFeed...");
            OraclePriceFeed priceFeed = new OraclePriceFeed(admin, reserveSystem);
            oraclePriceFeed = address(priceFeed);
            console.log("OraclePriceFeed deployed at:", oraclePriceFeed);
        } else {
            console.log("Using existing OraclePriceFeed:", oraclePriceFeed);
        }

        OraclePriceFeed priceFeedContract = OraclePriceFeed(oraclePriceFeed);

        // Configuration: Asset addresses and initial prices
        // For production, use real Chainlink aggregators
        // For testing, deploy MockPriceFeed contracts

        bool useMockFeeds = vm.envOr("USE_MOCK_FEEDS", true);

        if (useMockFeeds) {
            console.log("");
            console.log("=== Deploying Mock Price Feeds ===");

            // Example assets with mock prices (in 8 decimals for Chainlink compatibility)
            // Gold (XAU) - $2000 per ounce
            address xauAsset = vm.envOr("XAU_ASSET", address(0x1111111111111111111111111111111111111111));
            if (xauAsset != address(0)) {
                console.log("Setting up XAU price feed...");
                MockPriceFeed xauFeed = new MockPriceFeed(2000 * 1e8, 8);
                // Multiplier: 1e10 to convert 8 decimals to 18 decimals
                vm.prank(admin);
                priceFeedContract.setAggregator(xauAsset, address(xauFeed), 1e10);
                console.log("XAU MockPriceFeed:", address(xauFeed));
            }

            // Example: USDC - $1 per token
            address usdcAsset = vm.envOr("USDC_ASSET", address(0x2222222222222222222222222222222222222222));
            if (usdcAsset != address(0)) {
                console.log("Setting up USDC price feed...");
                MockPriceFeed usdcFeed = new MockPriceFeed(1 * 1e8, 8);
                vm.prank(admin);
                priceFeedContract.setAggregator(usdcAsset, address(usdcFeed), 1e10);
                console.log("USDC MockPriceFeed:", address(usdcFeed));
            }

            // Example: ETH - $3000 per token
            address ethAsset = vm.envOr("ETH_ASSET", address(0x3333333333333333333333333333333333333333));
            if (ethAsset != address(0)) {
                console.log("Setting up ETH price feed...");
                MockPriceFeed ethFeed = new MockPriceFeed(3000 * 1e8, 8);
                vm.prank(admin);
                priceFeedContract.setAggregator(ethAsset, address(ethFeed), 1e10);
                console.log("ETH MockPriceFeed:", address(ethFeed));
            }

            console.log("");
            console.log("=== Updating Price Feeds ===");

            // Update all price feeds
            address[] memory assets = new address[](3);
            if (xauAsset != address(0)) assets[0] = xauAsset;
            if (usdcAsset != address(0)) assets[1] = usdcAsset;
            if (ethAsset != address(0)) assets[2] = ethAsset;

            vm.prank(admin);
            priceFeedContract.updateMultiplePriceFeeds(assets);

            console.log("Price feeds updated successfully");
        } else {
            console.log("");
            console.log("=== Configuring Real Chainlink Aggregators ===");
            console.log("Set USE_MOCK_FEEDS=false and provide aggregator addresses");
            console.log("Example environment variables:");
            console.log("  XAU_AGGREGATOR=<chainlink_xau_usd_aggregator>");
            console.log("  USDC_AGGREGATOR=<chainlink_usdc_usd_aggregator>");
            console.log("  ETH_AGGREGATOR=<chainlink_eth_usd_aggregator>");

            // Configure real aggregators if provided
            address xauAggregator = vm.envOr("XAU_AGGREGATOR", address(0));
            address xauAsset = vm.envOr("XAU_ASSET", address(0));
            if (xauAggregator != address(0) && xauAsset != address(0)) {
                vm.prank(admin);
                priceFeedContract.setAggregator(xauAsset, xauAggregator, 1e10);
                console.log("XAU aggregator configured:", xauAggregator);
            }

            address usdcAggregator = vm.envOr("USDC_AGGREGATOR", address(0));
            address usdcAsset = vm.envOr("USDC_ASSET", address(0));
            if (usdcAggregator != address(0) && usdcAsset != address(0)) {
                vm.prank(admin);
                priceFeedContract.setAggregator(usdcAsset, usdcAggregator, 1e10);
                console.log("USDC aggregator configured:", usdcAggregator);
            }

            address ethAggregator = vm.envOr("ETH_AGGREGATOR", address(0));
            address ethAsset = vm.envOr("ETH_ASSET", address(0));
            if (ethAggregator != address(0) && ethAsset != address(0)) {
                vm.prank(admin);
                priceFeedContract.setAggregator(ethAsset, ethAggregator, 1e10);
                console.log("ETH aggregator configured:", ethAggregator);
            }
        }

        console.log("");
        console.log("=== Setup Complete ===");
        console.log("OraclePriceFeed:", oraclePriceFeed);
        console.log("");
        console.log("=== Export to .env ===");
        console.log("export ORACLE_PRICE_FEED=", vm.toString(oraclePriceFeed));

        vm.stopBroadcast();
    }
}

