/*-
 * ~~~~~~licensing~~~~~~
 * search-service
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.search.shell;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Service;

import ru.entaxy.platform.base.support.karaf.shell.RowExt;
import ru.entaxy.platform.base.support.karaf.shell.ShellTableExt;
import ru.entaxy.platform.search.shell.support.AbstractSearchCommand;
import ru.entaxy.platform.search.shell.support.ObjectTypeAwareCommand;

@Service
@Command(scope = AbstractSearchCommand.COMMAND_SCOPE, name = "search")
public class Search extends ObjectTypeAwareCommand {

    @Option(name = "-d", aliases = {"--display-attributes"})
    List<String> attributes = new ArrayList<>();

    @Option(name = "-n", aliases = {"--attribute-names-only"})
    boolean attributeNamesOnly = false;

    @Option(name = "-s", aliases = {"--no-split-lists"})
    boolean noSplitLists = false;

    @Argument(index = 1, required = true)
    String filter;

    @Override
    protected void doExecute() throws Exception {

        if (attributeNamesOnly) {
            listAttributeNames();
            return;
        }

        List<Map<String, Object>> result = searchService.search(type, filter);

        ShellTableExt table = new ShellTableExt();
        table.splitLists(!noSplitLists);

        if (!attributes.isEmpty()) {
            // fixed attributes

            List<String> attrs = attributes.stream().distinct().collect(Collectors.toList());

            for (String s : attrs)
                table.column(s);

            for (Map<String, Object> essence : result) {
                RowExt row = table.addRow();
                List<Object> data = new ArrayList<>();
                for (String s : attrs)
                    data.add(essence.getOrDefault(s, ""));
                row.addContent(data);
            }

        } else {

            List<String> attrs = result.stream().map(s -> s.keySet()).flatMap(ks -> ks.stream()).distinct()
                    .collect(Collectors.toList());

            for (String attr : attrs)
                table.column(attr);

            for (Map<String, Object> essence : result) {
                RowExt row = table.addRow();

                List<Object> data = new ArrayList<>();
                for (String s : attrs)
                    data.add(essence.getOrDefault(s, ""));
                row.addContent(data);
            }


        }

        // output
        table.print(System.out);

    }

    protected void listAttributeNames() throws Exception {
        List<String> list = searchService.getAttributes(type, filter);

        // output
        for (String s : list)
            System.out.println(s);

    }

}
