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

import "./IPostagePriceModule.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

error RefundFailed(address to);
error Paused();
error InsufficentPostagePayment(uint256 postageSent, uint256 postageRequired);

contract EthereumPostalService is Ownable {
    event MailReceived(
        PostalAddress postalAddress,
        string msgHtml,
        address sender,
        bool addressEncrypted,
        bool msgEncrypted,
        bytes2 encryptionPubKey
    );

    struct PostalAddress {
        string addressLine1;
        string addressLine2;
        string city;
        string countryCode;
        string postalOrZip;
        string name;
    }

    modifier pausable() {
        if (paused) {
            revert Paused();
        }
        _;
    }

    IPostagePriceModule public postagePriceModule;
    bytes public encryptionPubKey;
    bool public paused;

    constructor(IPostagePriceModule _postagePriceModule, bytes memory _encryptionPubKey) {
        postagePriceModule = _postagePriceModule;
        encryptionPubKey = _encryptionPubKey;
        paused = false;
    }

    function sendMail(PostalAddress calldata postalAddress, string calldata msgHtml) external payable pausable {
        handlePayment();
        emit MailReceived(postalAddress, msgHtml, msg.sender, false, false, 0x0);
    }

    function sendEncryptedMail(
        PostalAddress calldata postalAddress,
        string calldata msgHtml,
        bool addressEncrypted,
        bool msgEncrypted
    ) external payable pausable {
        handlePayment();
        emit MailReceived(postalAddress, msgHtml, msg.sender, addressEncrypted, msgEncrypted, bytes2(encryptionPubKey));
    }

    function handlePayment() internal {
        uint256 weiRequired = postagePriceModule.getPostageWei();
        if (msg.value < weiRequired) {
            revert InsufficentPostagePayment(msg.value, weiRequired);
        }

        if (msg.value > weiRequired) {
            uint256 weiReturn = msg.value - weiRequired;
            bool refunded = payable(address(msg.sender)).send(weiReturn);
            if (!refunded) {
                revert RefundFailed(msg.sender);
            }
        }
    }

    function getPostageWei() public view returns (uint256) {
        return postagePriceModule.getPostageWei();
    }

    // Admin functionality
    function updatePostagePriceModule(IPostagePriceModule newPostagePriceModule) external onlyOwner {
        postagePriceModule = newPostagePriceModule;
    }

    function updateEncryptionPubKey(bytes memory newEncryptionPubKey) external onlyOwner {
        encryptionPubKey = newEncryptionPubKey;
    }

    function togglePause() external onlyOwner {
        paused = !paused;
    }
}
[{"inputs":[{"internalType":"contract IPostagePriceModule","name":"_postagePriceModule","type":"address"},{"internalType":"bytes","name":"_encryptionPubKey","type":"bytes"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"postageSent","type":"uint256"},{"internalType":"uint256","name":"postageRequired","type":"uint256"}],"name":"InsufficentPostagePayment","type":"error"},{"inputs":[],"name":"Paused","type":"error"},{"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"RefundFailed","type":"error"},{"anonymous":false,"inputs":[{"components":[{"internalType":"string","name":"addressLine1","type":"string"},{"internalType":"string","name":"addressLine2","type":"string"},{"internalType":"string","name":"city","type":"string"},{"internalType":"string","name":"countryCode","type":"string"},{"internalType":"string","name":"postalOrZip","type":"string"},{"internalType":"string","name":"name","type":"string"}],"indexed":false,"internalType":"struct EthereumPostalService.PostalAddress","name":"postalAddress","type":"tuple"},{"indexed":false,"internalType":"string","name":"msgHtml","type":"string"},{"indexed":false,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"bool","name":"addressEncrypted","type":"bool"},{"indexed":false,"internalType":"bool","name":"msgEncrypted","type":"bool"},{"indexed":false,"internalType":"bytes2","name":"encryptionPubKey","type":"bytes2"}],"name":"MailReceived","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"encryptionPubKey","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPostageWei","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"postagePriceModule","outputs":[{"internalType":"contract IPostagePriceModule","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"string","name":"addressLine1","type":"string"},{"internalType":"string","name":"addressLine2","type":"string"},{"internalType":"string","name":"city","type":"string"},{"internalType":"string","name":"countryCode","type":"string"},{"internalType":"string","name":"postalOrZip","type":"string"},{"internalType":"string","name":"name","type":"string"}],"internalType":"struct EthereumPostalService.PostalAddress","name":"postalAddress","type":"tuple"},{"internalType":"string","name":"msgHtml","type":"string"},{"internalType":"bool","name":"addressEncrypted","type":"bool"},{"internalType":"bool","name":"msgEncrypted","type":"bool"}],"name":"sendEncryptedMail","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"string","name":"addressLine1","type":"string"},{"internalType":"string","name":"addressLine2","type":"string"},{"internalType":"string","name":"city","type":"string"},{"internalType":"string","name":"countryCode","type":"string"},{"internalType":"string","name":"postalOrZip","type":"string"},{"internalType":"string","name":"name","type":"string"}],"internalType":"struct EthereumPostalService.PostalAddress","name":"postalAddress","type":"tuple"},{"internalType":"string","name":"msgHtml","type":"string"}],"name":"sendMail","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"togglePause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"newEncryptionPubKey","type":"bytes"}],"name":"updateEncryptionPubKey","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IPostagePriceModule","name":"newPostagePriceModule","type":"address"}],"name":"updatePostagePriceModule","outputs":[],"stateMutability":"nonpayable","type":"function"}]