# : out que
# ruff: noqa: PERF203
"""simple client for que.run."""

import argparse
import configparser
import functools
import http.client
import logging
import pathlib
import subprocess
import sys
import textwrap
import time
import typing
import urllib.parse
from urllib import request

MAX_TIMEOUT = 9999999
RETRIES = 10
DELAY = 3
BACKOFF = 1


def auth(args: argparse.Namespace) -> str | None:
    """Return the auth key for the given ns from ~/.config/que.conf."""
    logging.debug("auth")
    namespace = args.target.split("/")[0]
    if namespace == "pub":
        return None
    conf_file = pathlib.Path("~/.config/que.conf").expanduser()
    if not conf_file.exists():
        sys.exit("you need a ~/.config/que.conf")
    cfg = configparser.ConfigParser()
    cfg.read(conf_file)
    return cfg[namespace]["key"]


def autodecode(bytestring: bytes) -> typing.Any:
    """
    Automatically decode bytes into common codecs.

    Or at least make an attempt. Output is preferably utf-8. If no decoding is
    available, just return the raw bytes.

    For all available codecs, see:
    <https://docs.python.org/3/library/codecs.html#standard-encodings>

    """
    logging.debug("autodecode")
    codecs = ["utf-8", "ascii"]
    for codec in codecs:
        try:
            return bytestring.decode(codec)
        except UnicodeDecodeError:
            pass
    return bytestring


@typing.no_type_check
def retry(
    exception: str,
    tries: typing.Any = RETRIES,
    delay: typing.Any = DELAY,
    backoff: typing.Any = BACKOFF,
) -> typing.Any:
    """Retry an action."""

    def decorator(func: typing.Any) -> typing.Any:
        @functools.wraps(func)
        def func_retry(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
            mtries, mdelay = tries, delay
            while mtries > 1:
                try:
                    return func(*args, **kwargs)
                except exception as ex:
                    logging.debug(ex)
                    logging.debug("retrying...")
                    time.sleep(mdelay)
                    mtries -= 1
                    mdelay *= backoff
            return func(*args, **kwargs)

        return func_retry

    return decorator


@typing.no_type_check
@retry(urllib.error.URLError)
@retry(http.client.IncompleteRead)
@retry(http.client.RemoteDisconnected)
def send(args: argparse.Namespace) -> None:
    """Send a message to the que."""
    logging.debug("send")
    key = auth(args)
    data = args.infile
    req = request.Request(f"{args.host}/{args.target}")
    req.add_header("User-Agent", "Que/Client")
    req.add_header("Content-Type", "text/plain;charset=utf-8")
    if key:
        req.add_header("Authorization", key)
    if args.serve:
        logging.debug("serve")
        while not time.sleep(1):
            request.urlopen(req, data=data, timeout=MAX_TIMEOUT)
    else:
        request.urlopen(req, data=data, timeout=MAX_TIMEOUT)


def then(args: argparse.Namespace, msg: str) -> None:
    """Perform an action when passed `--then`."""
    if args.then:
        logging.debug("then")
        subprocess.run(
            args.then.format(msg=msg, que=args.target),
            check=False,
            shell=True,  # noqa: S602
        )


@typing.no_type_check
@retry(urllib.error.URLError)
@retry(http.client.IncompleteRead)
@retry(http.client.RemoteDisconnected)
def recv(args: argparse.Namespace) -> None:
    """Receive a message from the que."""
    logging.debug("recv on: %s", args.target)
    if args.poll:
        req = request.Request(f"{args.host}/{args.target}/stream")
    else:
        req = request.Request(f"{args.host}/{args.target}")
    req.add_header("User-Agent", "Que/Client")
    key = auth(args)
    if key:
        req.add_header("Authorization", key)
    if not req.startswith(("http:", "https:")):
        msg = "URL must start with 'http:' or 'https:'"
        raise ValueError(msg)
    with request.urlopen(req) as _req:
        if args.poll:
            logging.debug("polling")
            while not time.sleep(1):
                reply = _req.readline()
                if reply:
                    msg = autodecode(reply)
                    logging.debug("read")
                    sys.stdout.write(msg)
                    then(args, msg)
                else:
                    continue
        else:
            msg = autodecode(_req.readline())
            sys.stdout.write(msg)
            then(args, msg)


def get_args() -> argparse.Namespace:
    """Command line parser."""
    cli = argparse.ArgumentParser(
        description=__doc__,
        epilog=textwrap.dedent(
            f"""Requests will retry up to {RETRIES} times, with {DELAY} seconds
        between attempts.""",
        ),
    )
    cli.add_argument("test", action="store_true", help="run tests")
    cli.add_argument("--debug", action="store_true", help="log to stderr")
    cli.add_argument(
        "--host",
        default="http://que.run",
        help="where que-server is running",
    )
    cli.add_argument(
        "--poll",
        default=False,
        action="store_true",
        help=textwrap.dedent(
            """keep the connection open to stream data from the que. without
            this flag, the program will exit after receiving a message""",
        ),
    )
    cli.add_argument(
        "--then",
        help=textwrap.dedent(
            """when polling, run this shell command after each response,
            presumably for side effects, replacing '{que}' with the target and
            '{msg}' with the body of the response""",
        ),
    )
    cli.add_argument(
        "--serve",
        default=False,
        action="store_true",
        help=textwrap.dedent(
            """when posting to the que, do so continuously in a loop. this can
            be used for serving a webpage or other file continuously""",
        ),
    )
    cli.add_argument(
        "target",
        help="namespace and path of the que, like 'ns/path'",
    )
    cli.add_argument(
        "infile",
        nargs="?",
        type=argparse.FileType("rb"),
        help="file of data to put on the que. use '-' for stdin",
    )
    return cli.parse_args()


if __name__ == "__main__":
    ARGV = get_args()
    if ARGV.test:
        sys.stdout.write("ok\n")
        sys.exit()
    if ARGV.debug:
        logging.basicConfig(
            format="%(asctime)s:  %(levelname)s:  %(message)s",
            level=logging.DEBUG,
            datefmt="%Y.%m.%d..%H.%M.%S",
        )
    try:
        if ARGV.infile:
            send(ARGV)
        else:
            recv(ARGV)
    except KeyboardInterrupt:
        sys.exit(0)