package ru.nbch.indicsearchservice.model;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class TableMetaData {
    private final String tableName;
    private String tableNameLocalization;
    private final Map<String, FieldMetaData> fieldMetaData = new LinkedHashMap<>();

    public TableMetaData(String tableName) {
        this.tableName = tableName;
    }

    public String getTableName() {
        return tableName;
    }

    public String getTableNameLocalization() {
        return tableNameLocalization;
    }

    public void setTableNameLocalization(String tableNameLocalization) {
        this.tableNameLocalization = tableNameLocalization;
    }

    public Map<String, FieldMetaData> getFieldMetaData() {
        return Collections.unmodifiableMap(fieldMetaData);
    }

    public boolean addField(String fieldName) {
        if (fieldMetaData.containsKey(fieldName)) {
            return false;
        }
        fieldMetaData.put(fieldName, new FieldMetaData(fieldName));
        return true;
    }

    public void addLocalization(String fieldName, String fieldLocalization) {
        fieldMetaData.compute(fieldName, (key, existingRecord) ->
            new FieldMetaData(fieldName, fieldLocalization)
        );
    }

    public record FieldMetaData(String fieldName, String fieldLocalization) {
        public FieldMetaData(String fieldName) {
            this(fieldName, null);
        }
    }
}
