import argparse

from werk24.cli import utils
from werk24.exceptions import BadRequestException, UnauthorizedException
from werk24.models.helpdesk import W24HelpdeskTask


async def main(
    args: argparse.Namespace
) -> None:
    """
    Interact with the Help Desk client through the API.

    Args:
    ----
    args(argparse.Namespace): CLI args generated by
        argparse
    """
    if args.command == "create-helpdesk-task":
        await _handle_create_helpdesk_task(args)


async def _handle_create_helpdesk_task(args: argparse.Namespace) -> None:
    """
    Create a helpdesk task.

    Args:
    ----
    args (argparse.Namespace): Namespace of the args.
    """

    # Create the task
    task = W24HelpdeskTask(
        request_id=args.request_id,
        observed_outcome=args.observed_outcome,
        expected_outcome=args.expected_outcome,
        comment=args.comment,
        position=None,
        importance=args.importance
    )

    # Get the client instance and handle
    # potential errors
    client = utils.make_client()
    async with client as session:
        try:
            task = await session.create_helpdesk_task(task)
        except BadRequestException:
            print("BadRequest. Please check the content of your request.")

        except UnauthorizedException:
            print("Unauthorized. Please check your credentials and the request id")
            return

    # Give the user some feedback
    print("created task with id", task.task_id)
