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

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

/**
 * @title DeployKeeper
 * @notice Deployment script for PriceFeedKeeper contract
 */
contract DeployKeeper 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");
        address deployer = vm.addr(deployerPrivateKey);
        console.log("=== Deploy Price Feed Keeper (ChainID 138) ===");
        console.log("Deployer:", deployer);
        console.log("");

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

        if (oraclePriceFeed == address(0)) {
            console.log("ORACLE_PRICE_FEED not set in .env. Skipping keeper deployment.");
            console.log("Set ORACLE_PRICE_FEED (from SetupPriceFeeds.s.sol) or RESERVE_KEEPER (existing keeper) in .env.");
            return;
        }

        vm.startBroadcast(deployerPrivateKey);
        console.log("Deploying PriceFeedKeeper...");
        PriceFeedKeeper keeper = new PriceFeedKeeper(admin, oraclePriceFeed);
        console.log("PriceFeedKeeper deployed at:", address(keeper));

        // Track assets if provided
        address xauAsset = vm.envOr("XAU_ASSET", address(0));
        address usdcAsset = vm.envOr("USDC_ASSET", address(0));
        address ethAsset = vm.envOr("ETH_ASSET", address(0));

        // Note: Asset tracking and role granting should be done after deployment
        // via separate transactions to avoid prank issues in broadcast mode
        address keeperAddress = vm.envOr("KEEPER_ADDRESS", deployer);
        
        // Grant keeper role (admin is deployer, so we can call directly)
        if (admin == deployer) {
            keeper.grantRole(keeper.KEEPER_ROLE(), keeperAddress);
            console.log("Keeper role granted to:", keeperAddress);
        } else {
            console.log("Note: Grant keeper role manually to:", keeperAddress);
        }
        
        // Note: Track assets after deployment via separate script or manual calls
        if (xauAsset != address(0) || usdcAsset != address(0) || ethAsset != address(0)) {
            console.log("");
            console.log("Note: Track assets after deployment:");
            if (xauAsset != address(0)) {
                console.log("  keeper.trackAsset(", xauAsset, ")");
            }
            if (usdcAsset != address(0)) {
                console.log("  keeper.trackAsset(", usdcAsset, ")");
            }
            if (ethAsset != address(0)) {
                console.log("  keeper.trackAsset(", ethAsset, ")");
            }
        }
        console.log("");
        console.log("Keeper role granted to:", keeperAddress);

        console.log("");
        console.log("=== Deployment Summary ===");
        console.log("PriceFeedKeeper:", address(keeper));
        console.log("OraclePriceFeed:", oraclePriceFeed);
        console.log("Admin:", admin);
        console.log("Keeper Address:", keeperAddress);
        console.log("");
        console.log("=== Export to .env ===");
        console.log("export PRICE_FEED_KEEPER_ADDRESS=", vm.toString(address(keeper)));

        vm.stopBroadcast();
    }
}

