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

import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "../../vendor/openzeppelin/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "../../emoney/interfaces/IeMoneyToken.sol";

/**
 * @title DepositToken
 * @notice Token representing supplied collateral position (aToken equivalent)
 * @dev Minted when M0 collateral is deposited, burned on withdrawal
 * Extends eMoneyToken pattern but simpler - no policy manager, just mint/burn control
 */
contract DepositToken is Initializable, ERC20Upgradeable, AccessControlUpgradeable, UUPSUpgradeable {
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");

    address public vault;
    address public collateralAsset;
    uint8 private _decimalsStorage;

    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() {
        _disableInitializers();
    }

    /**
     * @notice Initialize the deposit token (5-arg for backward compatibility; decimals = 18)
     */
    function initialize(
        string memory name,
        string memory symbol,
        address vault_,
        address collateralAsset_,
        address admin
    ) external initializer {
        _initializeWithDecimals(name, symbol, vault_, collateralAsset_, admin, 18);
    }

    /**
     * @notice Initialize with explicit decimals (ERC-20 DEX-ready; decimals match underlying)
     * @param decimals_ Token decimals (e.g. 6 for stablecoins)
     */
    function initializeWithDecimals(
        string memory name,
        string memory symbol,
        address vault_,
        address collateralAsset_,
        address admin,
        uint8 decimals_
    ) external initializer {
        _initializeWithDecimals(name, symbol, vault_, collateralAsset_, admin, decimals_ > 0 ? decimals_ : 18);
    }

    function _initializeWithDecimals(
        string memory name,
        string memory symbol,
        address vault_,
        address collateralAsset_,
        address admin,
        uint8 decimals_
    ) internal {
        __ERC20_init(name, symbol);
        __AccessControl_init();
        __UUPSUpgradeable_init();
        vault = vault_;
        collateralAsset = collateralAsset_;
        _decimalsStorage = decimals_;
        _grantRole(DEFAULT_ADMIN_ROLE, admin);
    }

    /**
     * @notice Returns token decimals (matches underlying for DEX compatibility)
     */
    function decimals() public view override returns (uint8) {
        return _decimalsStorage;
    }

    /**
     * @notice Mint deposit tokens (only by vault)
     * @param to Recipient address
     * @param amount Amount to mint
     */
    function mint(address to, uint256 amount) external onlyRole(MINTER_ROLE) {
        _mint(to, amount);
    }

    /**
     * @notice Burn deposit tokens (only by vault)
     * @param from Source address
     * @param amount Amount to burn
     */
    function burn(address from, uint256 amount) external onlyRole(BURNER_ROLE) {
        _burn(from, amount);
    }

    /**
     * @notice Authorize upgrade (UUPS)
     */
    function _authorizeUpgrade(address newImplementation) internal override onlyRole(DEFAULT_ADMIN_ROLE) {}
}
