// File: src/Create/contracts/v1/BasicMintModule.sol



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

import {IBasicMintModule, IConfigurable} from "create/interfaces/v1/IBasicMintModule.sol";
import {IMintPayout} from "create/interfaces/v1/IMintPayout.sol";
import {IMintContract} from "create/interfaces/v1/IMintContract.sol";
import {BasicMintConfiguration} from "create/interfaces/v1/BasicMintConfiguration.sol";
import {Version} from "create/contracts/v1/Version.sol";

contract BasicMintModule is IBasicMintModule, Version {
    IMintPayout public immutable mintPayout;

    mapping(address => BasicMintConfiguration) private _configurations;
    mapping(address => mapping(address => uint256)) private _mintedByAddress;
    mapping(address => uint256) public mintedByContract;

    /// @notice Emitted when quantity is zero.
    error InvalidQuantity();
    /// @notice Emitted if the collector is minting too many tokens per transaction.
    error TooManyTokensPerTransaction();
    /// @notice Emitted if the collector is minting too many tokens per wallet.
    error TooManyTokensPerCollector();
    /// @notice Emitted if the collector is minting more tokens than this module is allowed to mint.
    error TooManyTokensForModule();
    /// @notice Emitted if the mint has not started yet.
    error MintNotStarted();
    /// @notice Emitted if the mint has ended.
    error MintEnded();
    /// @notice Emitted when the value sent is incorrect.
    error IncorrectPayment();
    /// @notice Emitted when the max supply is reached.
    error MaxSupplyReached();

    constructor(address _mintPayout) Version(1) {
        mintPayout = IMintPayout(_mintPayout);
    }

    /// @inheritdoc IConfigurable
    function updateConfiguration(bytes calldata args) external override {
        BasicMintConfiguration memory _config = abi.decode(args, (BasicMintConfiguration));

        _configurations[msg.sender] = _config;
        emit ConfigurationUpdated(msg.sender, _config);
    }

    /// @inheritdoc IBasicMintModule
    function configuration(address _contract) external view returns (BasicMintConfiguration memory) {
        return _configurations[_contract];
    }

    /// @inheritdoc IBasicMintModule
    function mint(address _contract, address _to, address _referrer, uint256 _quantity) external payable {
        _mint(_contract, _to, _referrer, _quantity);
    }

    /// @inheritdoc IBasicMintModule
    function mint_efficient_7e80c46e(address _contract, address _to, address _referrer, uint256 _quantity)
        external
        payable
    {
        _mint(_contract, _to, _referrer, _quantity);
    }

    /// @notice The implementation of the mint function.
    /// @dev This is implemented as an internal function to share the logic between the `mint` and `mint_efficient_7e80c46e` functions.
    /// See the documentation for those functions for information on the parameters.
    function _mint(address _contract, address _to, address _referrer, uint256 _quantity) internal {
        BasicMintConfiguration memory config = _configurations[_contract];

        if (_quantity == 0) revert InvalidQuantity();
        if (config.maxPerTransaction > 0 && _quantity > config.maxPerTransaction) revert TooManyTokensPerTransaction();

        if (config.maxPerWallet > 0) {
            if (_mintedByAddress[_contract][_to] + _quantity > config.maxPerWallet) {
                revert TooManyTokensPerCollector();
            }
        }

        if (config.maxForModule > 0 && mintedByContract[_contract] + _quantity > config.maxForModule) {
            revert TooManyTokensForModule();
        }

        if (block.timestamp < config.mintStart) revert MintNotStarted();
        if (config.mintEnd > 0 && block.timestamp > config.mintEnd) revert MintEnded();

        uint256 protocolFee = mintPayout.protocolFee();
        if (msg.value != (config.price + protocolFee) * _quantity) revert IncorrectPayment();

        if (config.maxSupply > 0 && IMintContract(_contract).totalMinted() + _quantity > config.maxSupply) {
            revert MaxSupplyReached();
        }

        _mintedByAddress[_contract][_to] += _quantity;
        mintedByContract[_contract] += _quantity;

        mintPayout.mintDeposit{value: msg.value}(_contract, msg.sender, _referrer, _quantity);
        IMintContract(_contract).mint(_to, _quantity);
    }
}

// File: src/Create/interfaces/v1/IBasicMintModule.sol



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

import {BasicMintConfiguration} from "create/interfaces/v1/BasicMintConfiguration.sol";
import {IConfigurable} from "create/interfaces/v1/IConfigurable.sol";

interface IBasicMintModuleEvents {
    /// @notice Emitted when a contract's basic mint configuration is updated.
    /// @param _contract The address of the contract being configured.
    /// @param _config The new configuration.
    event ConfigurationUpdated(address indexed _contract, BasicMintConfiguration _config);
}

interface IBasicMintModule is IConfigurable, IBasicMintModuleEvents {
    /// @notice Retrieves the basic minting configuration for a contract.
    /// @param _contract The address of the contract.
    /// @return The current minting configuration.
    function configuration(address _contract) external view returns (BasicMintConfiguration memory);

    /// @notice Mints tokens for a NFT contract to a recipient.
    /// @dev Reverts if the mint does not work in the current configuration.
    /// @param _contract The address of the contract to mint for.
    /// @param _to The recipient of the tokens.
    /// @param _referrer The referrer of this mint, or the zero address if none.
    /// @param _quantity The quantity of tokens to mint.
    function mint(address _contract, address _to, address _referrer, uint256 _quantity) external payable;

    /// @notice Mints tokens for a NFT contract to a recipient.
    /// @dev Reverts if the mint does not work in the current configuration.
    /// This function is preferred over `mint` because the four byte signature is 0x00000000 which is cheaper to call.
    /// The implementation is identical to `mint`.
    /// @param _contract The address of the contract to mint for.
    /// @param _to The recipient of the tokens.
    /// @param _referrer The referrer of this mint, or the zero address if none.
    /// @param _quantity The quantity of tokens to mint.
    function mint_efficient_7e80c46e(address _contract, address _to, address _referrer, uint256 _quantity)
        external
        payable;
}

// File: src/Create/interfaces/v1/IMintPayout.sol



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

interface IMintPayoutEvents {
    /// @notice Emitted when a deposit has been made.
    /// @param from The depositor's address.
    /// @param to The address receiving the deposit.
    /// @param reason The reason code for the deposit.
    /// @param amount The deposit amount.
    event Deposit(address from, address to, bytes4 reason, uint256 amount);

    /// @notice Emitted when a withdrawal has been made.
    /// @param from The address withdrawing.
    /// @param to The address receiving the withdrawn funds.
    /// @param amount The withdrawal amount.
    event Withdraw(address from, address to, uint256 amount);

    /// @notice Emitted during a mint deposit to provide additional context.
    /// @param depositedBy The address of the mint initiator.
    /// @param mintContract The mint contract address this mint deposit refers to.
    /// @param minter The address of the person minting.
    /// @param referrer The address of the referrer, or the zero address for no referrer.
    /// @param creator The address of the contract creator, or the protocol fee recipient if none.
    /// @param creatorPayout The amount being paid to the creator.
    /// @param referralPayout The amount being paid to the referrer.
    /// @param protocolPayout The amount being paid to the protocol.
    /// @param totalAmount The total deposit amount.
    /// @param quantity The number of tokens being minted.
    /// @param protocolFee The per-mint fee for the protocol.
    event MintDeposit(
        address depositedBy,
        address mintContract,
        address minter,
        address referrer,
        address creator,
        uint256 creatorPayout,
        uint256 referralPayout,
        uint256 protocolPayout,
        uint256 totalAmount,
        uint256 quantity,
        uint256 protocolFee
    );

    /// @notice Emitted when the protocol fee is updated.
    /// @param fee The new protocol fee.
    event ProtocolFeeUpdated(uint256 fee);
}

interface IMintPayout is IMintPayoutEvents {
    function balanceOf(address owner) external view returns (uint256);
    function totalSupply() external view returns (uint256);

    /// @notice The current protocol fee per-mint.
    function protocolFee() external view returns (uint256 fee);

    /// @notice Sets the protocol fee per-mint.
    /// @dev Only callable by the owner.
    /// @param fee The new protocol fee.
    function setProtocolFee(uint256 fee) external;

    /// @notice Magic value used to represent the fees belonging to the protocol.
    function protocolFeeRecipientAccount() external view returns (address);

    /// @notice Withdraws from the protocol fee balance.
    /// @dev Only callable by the owner.
    /// @param to The address receiving the withdrawn funds.
    /// @param amount The withdrawal amount.
    function withdrawProtocolFee(address to, uint256 amount) external;

    /// @notice Deposits ether for a mint.
    /// @dev Ensure that `quantity` is > 0. The `protocolFee` should be per-mint, not the total taken.
    /// Will trigger a `MintDeposit` event, followed by `Deposit` events for:
    /// creator payout, protocol payout, and referrer payout (if a referrer is specified).
    /// @param mintContract The mint contract address this mint deposit refers to.
    /// @param minter The address of the minter.
    /// @param referrer The address of the referrer, or the zero address for no referrer.
    /// @param quantity The amount being minted.
    function mintDeposit(address mintContract, address minter, address referrer, uint256 quantity) external payable;

    /// @notice Deposits ether to an address.
    /// @param to The address receiving the deposit.
    /// @param reason The reason code for the deposit.
    function deposit(address to, bytes4 reason) external payable;

    /// @notice Deposits ether to multiple addresses.
    /// @dev The length of `recipients`, `amounts`, and `reasons` must be the same.
    /// @param recipients List of addresses receiving the deposits.
    /// @param amounts List of deposit amounts.
    /// @param reasons List of reason codes for the deposits.
    function depositBatch(address[] calldata recipients, uint256[] calldata amounts, bytes4[] calldata reasons)
        external
        payable;

    /// @notice Withdraws ether from the `msg.sender`'s account to a specified address.
    /// @param to The address receiving the withdrawn funds.
    /// @param amount The withdrawal amount.
    function withdraw(address to, uint256 amount) external;

    /// @notice Withdraws all ether from the `msg.sender`'s account to a specified address.
    /// @param to The address receiving the withdrawn funds.
    function withdrawAll(address to) external;
}

// File: src/Create/interfaces/v1/IMintContract.sol



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

import {IMetadataRenderer} from "create/interfaces/v1/IMetadataRenderer.sol";

interface IMintContractEvents {
    /// @notice Emitted when the royalty is updated.
    event RoyaltyUpdated(uint256 bps);
    /// @notice Emitted when a new mint module is added.
    event ModuleAdded(address module);
    /// @notice Emitted when a mint module is removed.
    event ModuleRemoved(address module);
    /// @notice Emitted when the metadata renderer is updated.
    event MetadataRendererUpdated(address renderer);
}

interface IMintContract is IMintContractEvents {
    /// @notice Mints tokens using approved mint modules.
    /// @param to The address receiving the minted tokens.
    /// @param quantity The quantity of tokens to mint.
    function mint(address to, uint256 quantity) external;

    /// @notice Mints tokens, callable only by the contract owner.
    /// @param to The address receiving the minted tokens.
    /// @param quantity The quantity of tokens to mint.
    function adminMint(address to, uint256 quantity) external;

    /// @notice Retrieves the payout recipient address for this mint contract.
    /// @return recipient address of the payout recipient.
    function payoutRecipient() external view returns (address recipient);

    /// @notice Returns the total number of tokens minted.
    /// @return total number of tokens minted.
    function totalMinted() external view returns (uint256 total);

    /// @notice Adds a new mint module as an approved minter.
    /// @dev Can only be executed by the owner of the contract.
    /// Must be approved in the MintModuleRegistry.
    /// @param mintModule The contract address of the mint module.
    function addMintModule(address mintModule) external;

    /// @notice Removes a mint module as an approved minter.
    /// @dev Can only be executed by the owner of the contract.
    /// @param mintModule The contract address of the mint module.
    function removeMintModule(address mintModule) external;

    /// @notice Returns whether a mint module is approved.
    /// @param mintModule The contract address of the mint module.
    /// @return isApproved Whether the mint module is approved.
    function isMintModuleApproved(address mintModule) external view returns (bool isApproved);

    /// @notice Updates configuration located in an external contract.
    /// @dev Can only be executed by the owner of the contract.
    /// The cardinality of `configurables` and `configData` must be the same.
    /// @param configurables The contract addresses to configure.
    /// @param configData The configuration data for the contracts.
    function updateExternalConfiguration(address[] calldata configurables, bytes[] calldata configData) external;

    /// @notice Sets the metadata renderer.
    /// @dev This will not request a metadata refresh. If needed, call `refreshMetadata`.
    /// @param renderer The new metadata renderer.
    function setMetadataRenderer(IMetadataRenderer renderer) external;

    /// @notice Returns the metadata renderer for this contract.
    /// @return metadataRenderer The metadata renderer.
    function metadataRenderer() external returns (IMetadataRenderer metadataRenderer);

    /// @notice Triggers a batch metadata update.
    function refreshMetadata() external;

    /// @notice Updates the royalty for this contract.
    /// @dev Can only be called by the contract owner.
    /// Emits a `RoyaltyUpdated` event.
    /// @param bps The new royalty.
    function setRoyalty(uint256 bps) external;

    /// @notice Returns the royalty for this contract.
    /// @return bps The royalty.
    function royaltyBps() external returns (uint256 bps);
}

// File: src/Create/interfaces/v1/BasicMintConfiguration.sol



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

struct BasicMintConfiguration {
    /// @notice Purchase cost per token.
    uint256 price;
    /// @notice UNIX timestamp of mint start.
    uint64 mintStart;
    /// @notice UNIX timestamp of mint end, or zero if open-ended.
    uint64 mintEnd;
    /// @notice Maximum token purchase limit per wallet, or zero if no limit.
    uint32 maxPerWallet;
    /// @notice Maximum tokens mintable per transaction, or zero if no limit.
    uint32 maxPerTransaction;
    /// @notice Maximum tokens mintable by this module, or zero if no limit.
    uint32 maxForModule;
    /// @notice Maximum tokens that can be minted in total, or zero if no max.
    uint32 maxSupply;
}

// File: src/Create/contracts/v1/Version.sol



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

abstract contract Version {
    /// @notice The version of the contract.
    uint32 public immutable contractVersion;

    constructor(uint32 _contractVersion) {
        contractVersion = _contractVersion;
    }
}

// File: src/Create/interfaces/v1/IConfigurable.sol



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

interface IConfigurable {
    /// @notice Updates the configuration for the calling contract.
    /// @param data The configuration data.
    function updateConfiguration(bytes calldata data) external;
}

// File: src/Create/interfaces/v1/IMetadataRenderer.sol



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

interface IMetadataRenderer {
    /// @notice Retrieves the token URI for the specified token ID.
    /// @param tokenId The ID of the token.
    /// @return uri The URI of the token.
    function tokenURI(uint256 tokenId) external view returns (string memory uri);
}