import argparse

from maps_adv.common.config_loader import ConfigLoader, Option, converters
from maps_adv.common.lasagna import setup_logging
from maps_adv.geosmb.doorman.server.lib import Application

arg_parser = argparse.ArgumentParser("Doorman")
arg_parser.add_argument("--host")
arg_parser.add_argument("--port", type=int)

config = ConfigLoader(
    Option("DATABASE_URL"),
    Option("TVM_DAEMON_URL", load_from="DEPLOY_TVM_TOOL_URL"),
    Option("TVM_TOKEN", load_from="TVMTOOL_LOCAL_AUTHTOKEN"),
    Option(
        "TVM_WHITELIST", default=(), converter=converters.Tuple(element_converter=int)
    ),
    Option(
        "TVM_GDPR_WHITELIST",
        default=(),
        converter=converters.Tuple(element_converter=int),
    ),
    Option("WARDEN_TASKS", default=(), converter=converters.Tuple()),
    Option("WARDEN_URL", default=None),
    Option("YQL_TOKEN", default=None),
    Option("YT_CLUSTER", default="hahn"),
    Option("CLIENT_YT_EXPORT_TABLE", default=None),
    Option("CLIENT_YT_EXPORT_TOKEN", default=None),
    Option("CALLS_STAT_YT_TABLE", default=None),
    Option("CALLS_TRACKING_YT_TABLE", default=None),
    Option("ADVERT_ORGS_YT_TABLE", default=None),
    Option("ADVERT_YT_TABLE", default=None),
    Option("CAMPAIGN_ORGANIZATION_YT_TABLE", default=None),
    Option("CAMPAIGN_YT_TABLE", default=None),
    Option("BVM_URL"),
)


def main():
    setup_logging()
    config.init()

    args = arg_parser.parse_args()

    app = Application(config)
    app.run(host=args.host, port=args.port)


if __name__ == "__main__":
    main()
