/*
 * Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "pub_options.h"

#include <arpa/inet.h>
#include <charconv>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <stdexcept>
#include <string_view>
#include <unistd.h>

#include "base/url_parser.h"
#include "engine/topic_io/transport_library.h"
#include "command_options.h"

namespace ibmw::cli::topic {
namespace {
constexpr uint64_t MAX_PORT = 65535;
constexpr uint64_t MAX_NET_THREADS = 256;
constexpr uint64_t MAX_MQTT_QOS = 2;
constexpr double MIN_PUBLISH_RATE_HZ = 0.000001;
constexpr double MAX_PUBLISH_RATE_HZ = 1000000;
constexpr double MAX_DELAY_SECONDS = 86400;
constexpr const char* DRIVERS[] = {"dds", "ros2", "zenoh", "mqtt", "tcp", "udp", "http", "iceoryx", "shm", "local"};

uint64_t PositiveInteger(const Invocation& invocation, const std::string& name, const char* fallback)
{
    const auto value = OptionValue(invocation, name, fallback);
    uint64_t number = 0;
    const auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), number);
    if (error != std::errc{} || end != value.data() + value.size() || number == 0) {
        throw std::invalid_argument(name + " requires a positive integer.");
    }
    return number;
}

double Number(const Invocation& invocation, const std::string& name, const char* fallback, double minimum,
              double maximum)
{
    const auto value = OptionValue(invocation, name, fallback);
    double number = 0;
    const auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), number);
    if (error != std::errc{} || end != value.data() + value.size() || !std::isfinite(number) || number < minimum ||
        number > maximum) {
        throw std::invalid_argument("Invalid value for " + name + ".");
    }
    return number;
}

uint64_t Integer(std::string_view text, std::string_view option, uint64_t min, uint64_t max)
{
    uint64_t number = 0;
    const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), number);
    if (error != std::errc{} || end != text.data() + text.size() || number < min || number > max) {
        throw std::invalid_argument(std::string(option) + " requires a decimal integer in [" + std::to_string(min) +
                                    ", " + std::to_string(max) + "].");
    }
    return number;
}

std::string RequiredValue(const Invocation& invocation, const char* option, const std::string& fallback = {})
{
    auto value = OptionValue(invocation, option, fallback);
    if (value.empty()) {
        throw std::invalid_argument(std::string(option) + " requires a nonempty value.");
    }
    return value;
}

void ValidateTarget(const std::string& value, const std::string& driver)
{
    const auto url = ibmw::common::util::ParseUrl(value);
    if (!url || url->host.empty() || !url->query.empty() || !url->fragment.empty() ||
        (driver == "http" ? url->protocol != "http" || (!url->path.empty() && url->path != "/")
                          : !url->protocol.empty() || !url->path.empty())) {
        throw std::invalid_argument("--target requires " +
                                    std::string(driver == "http" ? "http://host:port" : "IPv4:port") + ".");
    }
    Integer(url->service, "--target port", 1, MAX_PORT);
    if (driver != "http") {
        in_addr address{};
        if (::inet_pton(AF_INET, std::string(url->host).c_str(), &address) != 1) {
            throw std::invalid_argument("--target requires a numeric IPv4 address for TCP/UDP.");
        }
    } else if (url->host.find_first_of("@:[] \t\r\n") != std::string_view::npos) {
        throw std::invalid_argument("--target requires an HTTP host without credentials or IPv6 brackets.");
    }
}
} // namespace

namespace {
constexpr uint64_t SHM_MAX_LOAN_SIZE = uint64_t{1} << 28;

struct PublisherNodes {
    YAML::Node& settings;
    YAML::Node& options;
    YAML::Node& publisher;
};

std::string SelectPublishDriver(const Invocation& invocation)
{
    for (const auto& [name, values] : invocation.options) {
        if (name != "--type-support" && name != "--target" && values.size() != 1) {
            throw std::invalid_argument(name + " must be specified exactly once.");
        }
    }
    std::string selectedDriver;
    unsigned selected = 0;
    for (const auto* driver : DRIVERS) {
        if (invocation.HasOption(std::string("--") + driver)) {
            selectedDriver = driver;
            ++selected;
        }
    }
    if (selected != 1) {
        throw std::invalid_argument(
            "pub requires exactly one publishing backend: --dds, --ros2, --zenoh, --mqtt, --tcp, --udp, --http, "
            "--iceoryx, --shm or --local.");
    }
    return selectedDriver;
}

void ValidateDriverOptions(const Invocation& invocation, const std::string& driver, bool ddsOrRos, bool network)
{
    const auto require = [&invocation, &driver](const char* option, bool supported) {
        if (invocation.HasOption(option) && !supported) {
            throw std::invalid_argument(std::string(option) + " does not apply to --" + driver + ".");
        }
    };
    require("--domain", ddsOrRos);
    require("--dds-domain", driver == "dds");
    require("--ros-domain", driver == "ros2");
    require("--ros2-compatible", driver == "dds");
    require("--zenoh-domain", driver == "zenoh");
    require("--zenoh-config", driver == "zenoh");
    require("--target", network);
    require("--net-threads", network);
    require("--transport-library", driver != "local");
    for (const auto* option : {"--broker", "--client-id", "--mqtt-qos", "--mqtt-truststore", "--mqtt-client-cert",
                               "--mqtt-client-key", "--mqtt-key-password"}) {
        require(option, driver == "mqtt");
    }
    require("--iceoryx-runtime-id", driver == "iceoryx");
    require("--shm-init-size", driver == "iceoryx" || driver == "shm");
    for (const auto* option : {"--shm-runtime-id", "--shm-domain", "--shm-root-dir", "--shm-group",
                               "--shm-max-bytes-per-publisher", "--shm-prefault", "--shm-require-read-only"}) {
        require(option, driver == "shm");
    }
    for (const auto* option : {"--qos-reliability", "--qos-durability", "--qos-history", "--qos-depth"}) {
        if (invocation.HasOption(option) && !ddsOrRos) {
            throw std::invalid_argument(std::string(option) + " only applies to DDS/ROS 2.");
        }
    }
}

void ConfigureDdsQos(const Invocation& invocation, YAML::Node& publisher)
{
    for (const auto& policy : {std::string("reliability"), std::string("durability"), std::string("history")}) {
        const auto key = "--qos-" + policy;
        const auto value = OptionValue(invocation, key,
                                       policy == "reliability"  ? "reliable"
                                       : policy == "durability" ? "volatile"
                                                                : "keep_last");
        if (!(policy == "reliability"  ? value == "reliable" || value == "best_effort"
              : policy == "durability" ? value == "volatile" || value == "transient_local"
                                       : value == "keep_last" || value == "keep_all")) {
            throw std::invalid_argument("Invalid value for " + key + ".");
        }
        publisher["qos"][policy] = value;
    }
    publisher["qos"]["depth"] =
        Integer(OptionValue(invocation, "--qos-depth", "10"), "--qos-depth", 1, std::numeric_limits<int32_t>::max());
}

void ConfigureDdsPublisher(const Invocation& invocation, const std::string& driver, const std::string& identity,
                           PublisherNodes& nodes)
{
    std::string domain = OptionValue(invocation, "--domain", "0");
    if (driver == "ros2" && !invocation.HasOption("--domain")) {
        if (const char* value = std::getenv("ROS_DOMAIN_ID"); value && *value) {
            domain = value;
        }
    }
    const char* option = driver == "dds" ? "--dds-domain" : "--ros-domain";
    domain = OptionValue(invocation, option, domain);
    nodes.settings["domain_id"] = Integer(domain, option, 0, std::numeric_limits<uint32_t>::max());
    nodes.settings[driver == "dds" ? "participant_name" : "node_name"] = identity;
    ConfigureDdsQos(invocation, nodes.publisher);
    if (invocation.HasOption("--ros2-compatible")) {
        nodes.options["ros2_compatible"] = true;
    }
}

void ConfigureZenohPublisher(const Invocation& invocation, PublisherNodes& nodes)
{
    nodes.settings["limit_domain"] = OptionValue(invocation, "--zenoh-domain");
    if (invocation.HasOption("--zenoh-config")) {
        nodes.settings["native_cfg_path"] =
            std::filesystem::absolute(RequiredValue(invocation, "--zenoh-config")).string();
    }
    nodes.publisher["shm_enabled"] = false;
}

void ConfigureNetworkPublisher(const Invocation& invocation, const std::string& driver, PublisherNodes& nodes)
{
    const auto targets = invocation.options.find("--target");
    if (targets == invocation.options.end() || targets->second.empty()) {
        throw std::invalid_argument("--" + driver + " requires --target.");
    }
    for (const auto& target : targets->second) {
        ValidateTarget(target, driver);
        nodes.publisher["server_url_list"].push_back(target);
    }
    nodes.settings["thread_num"] =
        Integer(OptionValue(invocation, "--net-threads", "2"), "--net-threads", 1, MAX_NET_THREADS);
}

void ConfigureMqttTls(const Invocation& invocation, bool tls, PublisherNodes& nodes)
{
    if (tls && !invocation.HasOption("--mqtt-truststore")) {
        throw std::invalid_argument("A TLS broker requires --mqtt-truststore.");
    }
    for (const auto& [option, key] : {std::pair{"--mqtt-truststore", "truststore"},
                                      {"--mqtt-client-cert", "client_cert"},
                                      {"--mqtt-client-key", "client_key"},
                                      {"--mqtt-key-password", "client_key_password"}}) {
        if (!invocation.HasOption(option)) {
            continue;
        }
        if (!tls) {
            throw std::invalid_argument(std::string(option) + " requires a TLS broker URL.");
        }
        nodes.settings[key] = RequiredValue(invocation, option);
    }
    if (invocation.HasOption("--mqtt-client-cert") != invocation.HasOption("--mqtt-client-key")) {
        throw std::invalid_argument("--mqtt-client-cert and --mqtt-client-key must be supplied together.");
    }
    if (invocation.HasOption("--mqtt-key-password") && !invocation.HasOption("--mqtt-client-key")) {
        throw std::invalid_argument("--mqtt-key-password requires --mqtt-client-key.");
    }
}

void ConfigureMqttPublisher(const Invocation& invocation, const std::string& identity, PublisherNodes& nodes)
{
    const auto broker = RequiredValue(invocation, "--broker");
    const auto url = ibmw::common::util::ParseUrl(broker);
    if (!url || url->host.empty() ||
        (url->protocol != "tcp" && url->protocol != "mqtt" && url->protocol != "ssl" && url->protocol != "mqtts") ||
        !url->path.empty() || !url->query.empty() || !url->fragment.empty() ||
        url->host.find_first_of("@:[] \t\r\n") != std::string_view::npos) {
        throw std::invalid_argument("--broker requires tcp://host:port or ssl://host:port.");
    }
    Integer(url->service, "--broker port", 1, MAX_PORT);
    nodes.settings["broker_addr"] = (url->protocol == "mqtt"    ? "tcp"
                                     : url->protocol == "mqtts" ? "ssl"
                                                                : std::string(url->protocol)) +
                                    "://" + std::string(url->host) + ":" + std::string(url->service);
    nodes.settings["client_id"] = RequiredValue(invocation, "--client-id", identity);
    nodes.publisher["qos"] = Integer(OptionValue(invocation, "--mqtt-qos", "2"), "--mqtt-qos", 0, MAX_MQTT_QOS);
    const bool tls = url->protocol == "ssl" || url->protocol == "mqtts";
    ConfigureMqttTls(invocation, tls, nodes);
}

void ConfigureShmPublisher(const Invocation& invocation, const std::string& identity, PublisherNodes& nodes)
{
    auto& settings = nodes.settings;
    // Keys of the shm_extension YAML node; omitted ones keep its defaults.
    settings["runtime_id"] = RequiredValue(invocation, "--shm-runtime-id", identity);
    // The shm driver loans at most 256 MiB (shm_ext kMaxLoanSize, the largest
    // selfdev payload class) and rejects a larger first loan at Initialize().
    settings["shm_init_size"] =
        Integer(OptionValue(invocation, "--shm-init-size", "1024"), "--shm-init-size", 1, SHM_MAX_LOAN_SIZE);
    if (invocation.HasOption("--shm-domain")) {
        settings["domain"] = RequiredValue(invocation, "--shm-domain");
    }
    if (invocation.HasOption("--shm-root-dir")) {
        settings["root_dir"] = RequiredValue(invocation, "--shm-root-dir");
    }
    if (invocation.HasOption("--shm-group")) {
        // 4294967295 is (gid_t)-1, "no change" for fchown, so it is not a group.
        settings["group"] = std::to_string(Integer(OptionValue(invocation, "--shm-group", ""), "--shm-group", 0,
                                                   std::numeric_limits<uint32_t>::max() - 1));
    }
    if (invocation.HasOption("--shm-max-bytes-per-publisher")) {
        settings["max_bytes_per_publisher"] = PositiveInteger(invocation, "--shm-max-bytes-per-publisher", "");
    }
    if (invocation.HasOption("--shm-prefault")) {
        settings["prefault"] = true;
    }
    if (invocation.HasOption("--shm-require-read-only")) {
        settings["require_read_only"] = true;
    }
}

void ConfigureDriver(const Invocation& invocation, const std::string& driver, bool network, const std::string& identity,
                     PublisherNodes& nodes)
{
    if (driver == "dds" || driver == "ros2") {
        return ConfigureDdsPublisher(invocation, driver, identity, nodes);
    }
    if (driver == "zenoh") {
        return ConfigureZenohPublisher(invocation, nodes);
    }
    if (network) {
        return ConfigureNetworkPublisher(invocation, driver, nodes);
    }
    if (driver == "mqtt") {
        return ConfigureMqttPublisher(invocation, identity, nodes);
    }
    if (driver == "iceoryx") {
        nodes.settings["runtime_id"] = RequiredValue(invocation, "--iceoryx-runtime-id", identity);
        nodes.settings["shm_init_size"] = Integer(OptionValue(invocation, "--shm-init-size", "1024"), "--shm-init-size",
                                                  1, std::numeric_limits<uint32_t>::max());
        return;
    }
    if (driver == "shm") {
        ConfigureShmPublisher(invocation, identity, nodes);
    }
}

void AddRuntimeSections(YAML::Node& runtime, const std::string& driver, YAML::Node& transport)
{
    runtime["messaging"]["drivers"].push_back(transport);
    YAML::Node route;
    route["topic"] = ".*";
    route["enable_drivers"].push_back(driver);
    runtime["messaging"]["publishers"].push_back(route);
    runtime["runtime_graph"]["enabled"] = true;
    runtime["logging"]["core_level"] = "WARN";
    YAML::Node console;
    console["type"] = "console";
    runtime["logging"]["backends"].push_back(console);
}

void ParsePublicationSchedule(const Invocation& invocation, PublicationConfig& result)
{
    result.topic = invocation.positionalArgs.at(0);
    result.typeName = invocation.positionalArgs.at(1);
    if (result.topic.empty() || result.typeName.empty()) {
        throw std::invalid_argument("Topic and type must not be empty.");
    }
    result.rate = Number(invocation, "--rate", "1", MIN_PUBLISH_RATE_HZ, MAX_PUBLISH_RATE_HZ);
    result.wait = Number(invocation, "--wait", "1", 0, MAX_DELAY_SECONDS);
    result.keepAlive = Number(invocation, "--keep-alive", "0.2", 0, MAX_DELAY_SECONDS);
    if (invocation.HasOption("--once") && invocation.HasOption("--times")) {
        throw std::invalid_argument("--once and --times are mutually exclusive.");
    }
    result.count = invocation.HasOption("--once")    ? 1
                   : invocation.HasOption("--times") ? PositiveInteger(invocation, "--times", "1")
                                                     : std::numeric_limits<uint64_t>::max();
}

struct OptionSpec {
    const char* longName;
    const char* shortName;
    const char* metavar;
    const char* description;
    OptionValueMode mode;
};

constexpr auto REQUIRED = OptionValueMode::REQUIRED;
constexpr auto FLAG = OptionValueMode::NONE;

// Help output keeps this order.
constexpr OptionSpec PUBLICATION_OPTIONS[] = {
    {"--ros2-compatible", "", "", "Use ROS 2 wire topic naming for ros2: messages over DDS", FLAG},
    {"--domain", "", "id", "DDS/ROS 2 domain", REQUIRED},
    {"--dds-domain", "", "id", "DDS domain (overrides --domain; default: 0)", REQUIRED},
    {"--ros-domain", "", "id", "ROS 2 domain (overrides --domain; otherwise ROS_DOMAIN_ID or 0)", REQUIRED},
    {"--zenoh-domain", "", "name", "Zenoh limit_domain", REQUIRED},
    {"--zenoh-config", "", "path", "Optional Zenoh native connection configuration", REQUIRED},
    {"--target", "", "address", "TCP/UDP IPv4:port or HTTP http://host:port; repeatable", REQUIRED},
    {"--net-threads", "", "count", "TCP/UDP/HTTP worker threads (default: 2)", REQUIRED},
    {"--broker", "", "url", "MQTT broker, e.g. tcp://127.0.0.1:1883", REQUIRED},
    {"--client-id", "", "name", "MQTT client ID (default: unique process ID)", REQUIRED},
    {"--mqtt-qos", "", "level", "MQTT QoS: 0, 1 or 2 (default: 2)", REQUIRED},
    {"--mqtt-truststore", "", "path", "MQTT TLS CA certificate", REQUIRED},
    {"--mqtt-client-cert", "", "path", "MQTT TLS client certificate", REQUIRED},
    {"--mqtt-client-key", "", "path", "MQTT TLS client private key", REQUIRED},
    {"--mqtt-key-password", "", "password", "MQTT TLS client key password", REQUIRED},
    {"--iceoryx-runtime-id", "", "name", "Iceoryx runtime ID (default: unique process ID)", REQUIRED},
    {"--shm-init-size", "", "bytes", "Iceoryx/shm initial shared-memory chunk size (default: 1024)", REQUIRED},
    {"--shm-runtime-id", "", "name", "shm runtime ID (default: unique process ID)", REQUIRED},
    {"--shm-domain", "", "name", "shm domain (default: IBMW_SHM_DOMAIN)", REQUIRED},
    {"--shm-root-dir", "", "path", "shm discovery root directory (default: IBMW_SHM_ROOT or the built-in root)",
     REQUIRED},
    {"--shm-group", "", "gid", "shm numeric group ID sharing the memory (default: IBMW_SHM_GROUP or user-private)",
     REQUIRED},
    {"--shm-max-bytes-per-publisher", "", "bytes", "shm shared-memory budget per publisher (default: 512 MiB)",
     REQUIRED},
    {"--shm-prefault", "", "", "shm: prefault new shared-memory segments", FLAG},
    {"--shm-require-read-only", "", "", "shm: fail instead of creating segments subscribers could map writable", FLAG},
    {"--transport-library", "", "path", "Override the selected transport extension library", REQUIRED},
    {"--qos-reliability", "", "policy", "DDS/ROS 2: reliable (default) or best_effort", REQUIRED},
    {"--qos-durability", "", "policy", "DDS/ROS 2: volatile (default) or transient_local", REQUIRED},
    {"--qos-history", "", "policy", "DDS/ROS 2: keep_last (default) or keep_all", REQUIRED},
    {"--qos-depth", "", "count", "DDS/ROS 2 history depth (default: 10)", REQUIRED},
    {"--type-support", "", "path", "Message TypeSupport library; repeatable", REQUIRED},
    {"--once", "", "", "Publish one message and exit", FLAG},
    {"--times", "-t", "count", "Publish this many messages and exit", REQUIRED},
    {"--rate", "", "hz", "Frequency (default: 1 Hz, until Ctrl+C)", REQUIRED},
    {"--wait", "", "seconds", "Connection/discovery delay before sending (default: 1); no matching guarantee",
     REQUIRED},
    {"--keep-alive", "", "seconds", "Remain alive after the last send (default: 0.2); not an acknowledgment", REQUIRED},
};
} // namespace

PublicationConfig ParsePublicationOptions(const Invocation& invocation)
{
    PublicationConfig result;
    result.driver = SelectPublishDriver(invocation);
    const auto& driver = result.driver;
    const bool ddsOrRos = driver == "dds" || driver == "ros2";
    const bool network = driver == "tcp" || driver == "udp" || driver == "http";
    ValidateDriverOptions(invocation, driver, ddsOrRos, network);

    auto runtime = result.root["ibmw"];
    YAML::Node extension;
    YAML::Node transport;
    YAML::Node publisher;
    extension["name"] = (network ? "net" : driver) + "_extension";
    auto settings = extension["options"];
    transport["type"] = driver;
    auto options = transport["options"];
    publisher["topic"] = ".*"; // This Engine owns one publisher.
    const auto identity = "ibmw_topic_pub_" + std::to_string(::getpid());
    PublisherNodes nodes{settings, options, publisher};
    ConfigureDriver(invocation, driver, network, identity, nodes);
    if (driver != "local") {
        extension["path"] = invocation.HasOption("--transport-library")
                                ? RequiredValue(invocation, "--transport-library")
                                : ibmw::runtime::core::topic_io::TransportLibrary(network ? "net" : driver).string();
        runtime["extensions"]["items"].push_back(extension);
    }
    // Local, Iceoryx and shm do not have per-topic driver publisher options.
    if (driver != "local" && driver != "iceoryx" && driver != "shm") {
        options["publishers"].push_back(publisher);
    }
    AddRuntimeSections(runtime, driver, transport);
    ParsePublicationSchedule(invocation, result);
    return result;
}

void AddPublicationOptions(Command& pub)
{
    pub.AddArgument("<topic>", "Topic to publish (may be new)");
    pub.AddArgument("<type>", "Canonical message type, including dds:, ros2: or json:");
    pub.AddArgument("<values>", "Quoted JSON/YAML message contents");
    for (const auto* driver : DRIVERS) {
        const std::string name(driver);
        pub.AddOption("--" + name,
                      name == "dds"     ? "-d"
                      : name == "ros2"  ? "-r"
                      : name == "zenoh" ? "-z"
                                        : "",
                      {}, name == "local" ? "Publish inside this CLI process only" : "Publish through " + name);
    }
    for (const auto& option : PUBLICATION_OPTIONS) {
        pub.AddOption(option.longName, option.shortName, option.metavar, option.description, option.mode);
    }
}
} // namespace ibmw::cli::topic