# https://www.codefactor.io/repository/github/code-rgb/userge-x/source/alpha/userge/plugins/utils/header.py#L1

import requests

from userge import Message, userge


@userge.on_cmd(
    "head",
    about={
        "header": "View headers in URL",
        "flags": {
            "-r": "allow redirects",
            "-s": "allow streams",
            "-t": "request timeout",
        },
        "usage": "{tr}head [flags] [url]",
        "examples": "{tr}head -r -s -t5 https://www.google.com",
    },
)
async def req_head(message: Message):
    await message.edit("Processing ...")
    link = message.filtered_input_str
    flags = message.flags
    red = "-r" in flags
    stm = "-s" in flags
    tout = int(flags.get("-t", 3))
    if not link:
        await message.err(text="Please give me a link link!")
        return
    try:
        cd = requests.head(url=link, stream=stm, allow_redirects=red, timeout=tout)
    except Exception as i_e:
        await message.err(i_e)
        return
    output = f"**URL**: `{link}`\n\n**STATUS CODE**: __{cd.status_code}__\n\n**HEADERS**:\n\n"
    for k, v in cd.headers.items():
        output += f"   üè∑ __{k.lower()}__ : `{v}`\n\n"
    await message.edit_or_send_as_file(
        text=output, caption=link, disable_web_page_preview=True
    )
