import asyncio
import sys

from parser import get_from_header, parse
from perun_sync.client import PerunSyncClient

from config import AppConfig


async def main() -> None:
    config = AppConfig.get()
    message = parse(sys.stdin.read())
    from_ = get_from_header(message)
    perun_sync_client = PerunSyncClient(config.perun_sync)
    if (user := await perun_sync_client.get_user_by_email(from_)) is not None:
        message.replace_header("From", f"{user.display_name} <{user.login_mu}@mail.muni.cz>")
        message.add_header("X-RT-Original-Requestor", from_)
    sys.stdout.write(message.as_string())


if __name__ == "__main__":
    asyncio.run(main())
