package ru.nbch.indicsearchservice.util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class IndicSearchCommon {
    private static final Map<String, List<String>> CASTABLE_FIELDS = Map.ofEntries(
        Map.entry("RU_UID", List.of("ACCT_NUM")),
        Map.entry("RU_APPLICATION", List.of("OLD_APP_NUM")),
        Map.entry("ID", List.of("ID_NORM")),
        Map.entry("ACCOUNT", List.of("ACCT_NUM")),
        Map.entry("INFORMATION", List.of("APP_NUM"))
    );

    public static Map<String, List<String>> getCastableFields() {
        return CASTABLE_FIELDS;
    }

    public static boolean isCastableField(String tableName, String fieldName) {
        if (tableName == null || tableName.isEmpty() || fieldName == null || fieldName.isEmpty()) {
            return false;
        }

        if (CASTABLE_FIELDS.containsKey(tableName.toUpperCase())) {
            return CASTABLE_FIELDS.get(tableName).contains(fieldName.toUpperCase());
        }

        return false;
    }

    public static Optional<String> getRowValue(String string, String key) {
        return getRowValue(Arrays.asList(string.split("%%%", -1)), key);
    }

    public static Optional<String> getRowValue(List<String> stringValues, String key) {
        int keyIndex = stringValues.indexOf(key);
        if (keyIndex != -1) {
            return Optional.of(stringValues.get(keyIndex + 1));
        }
        return Optional.empty();
    }
}
