#!/usr/bin/env python3
"""Administrative CLI for download link database."""
from __future__ import annotations

import argparse

from admin_links import cmd_add
from admin_links import cmd_ban
from admin_links import cmd_disable
from admin_links import cmd_enable
from admin_links import cmd_import
from admin_links import cmd_list
from admin_links import cmd_remove
from admin_links import cmd_rename
from admin_links import cmd_unban
from admin_links import cmd_validate_import
from admin_links import cmd_fix_revision
from admin_users import cmd_user_rename
from admin_users import cmd_users


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="download-admin")
    sub = parser.add_subparsers(dest="cmd", required=True)

    p_add = sub.add_parser("add")
    p_add.add_argument("user")
    p_add.add_argument("url")
    p_add.add_argument("--assume-yes", action="store_true")
    p_add.set_defaults(func=cmd_add)

    p_disable = sub.add_parser("disable")
    p_disable.add_argument("user")
    p_disable.add_argument("url", nargs="?")
    p_disable.set_defaults(func=cmd_disable)

    p_enable = sub.add_parser("enable")
    p_enable.add_argument("user")
    p_enable.add_argument("url", nargs="?")
    p_enable.set_defaults(func=cmd_enable)

    p_ban = sub.add_parser("ban")
    p_ban.add_argument("user")
    p_ban.add_argument("url", nargs="?")
    p_ban.add_argument("--reason")
    p_ban.set_defaults(func=cmd_ban)

    p_unban = sub.add_parser("unban")
    p_unban.add_argument("user")
    p_unban.add_argument("url", nargs="?")
    p_unban.set_defaults(func=cmd_unban)

    p_remove = sub.add_parser("remove")
    p_remove.add_argument("user")
    p_remove.add_argument("url", nargs="?")
    p_remove.set_defaults(func=cmd_remove)

    p_rename = sub.add_parser("rename")
    p_rename.add_argument("user")
    p_rename.add_argument("old_url", nargs="?")
    p_rename.add_argument("new_url", nargs="?")
    p_rename.set_defaults(func=cmd_rename)

    p_list = sub.add_parser("list")
    p_list.add_argument("--user", action="append")
    p_list.add_argument("--disabled", action="store_true")
    p_list.add_argument("--banned", action="store_true")
    p_list.add_argument("--requires-revision", action="store_true")
    p_list.set_defaults(func=cmd_list)

    p_users = sub.add_parser("users")
    p_users.set_defaults(func=cmd_users)

    p_import = sub.add_parser("import")
    p_import.set_defaults(func=cmd_import)

    p_validate = sub.add_parser("validate-import")
    p_validate.set_defaults(func=cmd_validate_import)

    p_fix_rev = sub.add_parser("fix-revision")
    p_fix_rev.set_defaults(func=cmd_fix_revision)

    p_user_rename = sub.add_parser("user-rename")
    p_user_rename.add_argument("user")
    p_user_rename.add_argument("site")
    p_user_rename.add_argument("old")
    p_user_rename.add_argument("new")
    p_user_rename.set_defaults(func=cmd_user_rename)

    return parser


def main() -> None:
    parser = build_parser()
    args = parser.parse_args()
    args.func(args)


if __name__ == "__main__":
    main()
