/*
 * 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 "list_command.h"

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include "discovery_options.h"
#include "discovery_query.h"

namespace ibmw::cli::topic {
namespace {

using ibmw::cli::Invocation;
using ibmw::discovery::DiscoveredEndpoint;
using ibmw::discovery::DiscoverySnapshot;
using ibmw::discovery::DiscoveryStatus;
using ibmw::discovery::EndpointRole;

void PrintTypes(const std::vector<DiscoveredEndpoint>& advertisements)
{
    std::cout << " [";
    bool first = true;
    for (const auto& type : TopicTypes(advertisements)) {
        if (!first) {
            std::cout << ", ";
        }
        first = false;
        std::cout << type;
    }
    std::cout << ']';
}

void PrintEndpointCounts(const std::vector<DiscoveredEndpoint>& advertisements)
{
    size_t publisherCount = 0;
    size_t subscriberCount = 0;
    for (const auto& advertisement : advertisements) {
        if (advertisement.role == EndpointRole::kPublisher) {
            ++publisherCount;
        } else {
            ++subscriberCount;
        }
    }
    std::cout << "  publishers: " << publisherCount << '\n';
    std::cout << "  subscribers: " << subscriberCount << '\n';
}

int RunTopicListCommandImpl(const Invocation& invocation)
{
    const bool includeHiddenTopics = invocation.HasOption("--include-hidden-topics");
    const auto graph = DiscoverTopics(invocation);
    const auto topicNames = VisibleTopicNames(graph, includeHiddenTopics);

    if (invocation.HasOption("--count-topics")) {
        std::cout << topicNames.size() << '\n';
        return graph.Succeeded() ? 0 : 1;
    }

    const bool showTypes = invocation.HasOption("--show-types");
    const bool verbose = invocation.HasOption("--verbose");
    for (const auto& topicName : topicNames) {
        const auto advertisements = VisibleEndpoints(graph, topicName, includeHiddenTopics);
        std::cout << topicName;
        if (showTypes || verbose) {
            PrintTypes(advertisements);
        }
        std::cout << '\n';

        if (verbose) {
            PrintEndpointCounts(advertisements);
        }
    }
    return graph.Succeeded() ? 0 : 1;
}

} // namespace

void AddListCommand(Command& topic)
{
    auto& list = topic.AddSubcommand("list", "List discovered topics");
    list.SetHandler(RunTopicListCommandImpl);
    list.AddOption("--show-types", "-t", {}, "Additionally show the topic type");
    list.AddOption("--count-topics", "-c", {}, "Only display the number of topics");
    list.AddOption("--verbose", "-v", {}, "Show publisher and subscriber details");
    list.AddOption("--include-hidden-topics", {}, {}, "Consider hidden topics as well");
    AddDiscoveryOptions(list);
}

} // namespace ibmw::cli::topic