// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Libdnf 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#include "autoremove.hpp"

#include <dnf5/shared_options.hpp>
#include <libdnf5/rpm/package_query.hpp>

namespace dnf5 {

using namespace libdnf5::cli;

void AutoremoveCommand::set_parent_command() {
    auto * arg_parser_parent_cmd = get_session().get_argument_parser().get_root_command();
    auto * arg_parser_this_cmd = get_argument_parser_command();
    arg_parser_parent_cmd->register_command(arg_parser_this_cmd);
    arg_parser_parent_cmd->get_group("software_management_commands").register_argument(arg_parser_this_cmd);
}

void AutoremoveCommand::set_argument_parser() {
    get_argument_parser_command()->set_description(
        _("Remove all unneeded packages originally installed as dependencies."));
    create_offline_option(*this);
    create_store_option(*this);
}

void AutoremoveCommand::configure() {
    auto & context = get_context();
    context.set_load_system_repo(true);
    context.set_load_available_repos(Context::LoadAvailableRepos::NONE);
}

void AutoremoveCommand::run() {
    auto & ctx = get_context();
    libdnf5::rpm::PackageQuery unneeded(ctx.get_base());
    unneeded.filter_unneeded();

    auto & cfg_main = ctx.get_base().get_config();
    auto & protected_packages = cfg_main.get_protected_packages_option().get_value();
    libdnf5::rpm::PackageQuery protected_query(ctx.get_base(), libdnf5::sack::ExcludeFlags::IGNORE_EXCLUDES);
    protected_query.filter_installed();
    protected_query.filter_name(protected_packages);

    bool protected_found = false;
    for (const auto & protected_pkg : protected_query) {
        if (unneeded.contains(protected_pkg)) {
            ctx.print_info(libdnf5::utils::sformat(
                _("Unneeded protected package: {0} (and its dependencies) cannot be removed, either mark it as "
                  "user-installed or change protected_packages configuration option."),
                protected_pkg.get_full_nevra()));
            protected_found = true;
        }
    }

    if (protected_found) {
        unneeded = libdnf5::rpm::PackageQuery(ctx.get_base());
        unneeded.filter_unneeded_not_protected();
    }

    auto goal = get_context().get_goal();
    for (const auto & pkg : unneeded) {
        goal->add_rpm_remove(pkg);
    }
}

}  // namespace dnf5
