#!/usr/bin/env python
# Copyright (C) 2023 OVH SAS
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import argparse

from urllib.parse import parse_qsl

from oio.cli.common.utils import KeyValueAction
from oio.common.amqp import (
    DEFAULT_ENDPOINT,
    DEFAULT_EXCHANGE,
    DEFAULT_QUEUE,
    DEFAULT_QUEUE_ARGS,
)
from oio.common.configuration import read_conf, load_namespace_conf
from oio.common.easy_value import int_value
from oio.common.logger import get_logger
from oio.event.amqp_agent import AmqpEventWorker
from oio.event.amqp_consumer import AmqpConsumerPool


def make_arg_parser():
    descr = """
    Read object storage events from RabbitMQ, execute actions.
    """
    parser = argparse.ArgumentParser(description=descr)

    parser.add_argument(
        "--verbose", "-v", action="store_true", help="More verbose output"
    )
    parser.add_argument("configuration", help="Path to the legacy configuration file")
    return parser


def main():
    args = make_arg_parser().parse_args()
    conf = read_conf(args.configuration, "event-agent")
    ns_conf = load_namespace_conf(conf["namespace"])

    # Configuration from dedicated file
    logger = get_logger(conf, verbose=args.verbose)
    workers = int_value(conf.get("workers"), 0)

    # Configuration either from dedicated file or central file (in that order)
    endpoint = conf.get("queue_url", ns_conf.get("event-agent", DEFAULT_ENDPOINT))
    input_queue = conf.get(
        "queue_name",
        conf.get("tube", ns_conf.get("events.amqp.queue_name", DEFAULT_QUEUE)),
    )
    routing_key = conf.get(
        "routing_key", ns_conf.get("events.amqp.bind_routing_key", "#")
    )
    exchange_name = conf.get(
        "exchange_name", ns_conf.get("events.amqp.exchange_name", DEFAULT_EXCHANGE)
    )
    if "bind_args" in conf:
        bind_conf = conf.get("bind_args")
    else:
        bind_conf = ns_conf.get("events.amqp.bind_args", "")
    bind_args = dict(parse_qsl(bind_conf, separator=","))

    if "queue_args" in conf:
        queue_conf = conf.get("queue_args", DEFAULT_QUEUE_ARGS)
    else:
        queue_conf = ns_conf.get("events.amqp.queue_args", DEFAULT_QUEUE_ARGS)
    queue_args = dict(parse_qsl(queue_conf, separator=","))

    pool = AmqpConsumerPool(
        endpoint,
        input_queue,
        AmqpEventWorker,
        queue_args=queue_args,
        exchange_name=exchange_name,
        routing_key=routing_key,
        bind_args=bind_args,
        logger=logger,
        processes=workers,
        app_conf=conf,
    )
    pool.run()


if __name__ == "__main__":
    main()
