package ru.nbch.indicsearchservice.model;

import jakarta.annotation.Nullable;
import java.sql.JDBCType;
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 static class FieldMetaData {
        private final String fieldName;
        @Nullable
        private final String fieldLocalization;
        @Nullable
        private JDBCType jdbcType;
        @Nullable
        private Integer columnSize;

        public FieldMetaData(String fieldName, @Nullable String fieldLocalization) {
            this.fieldName = fieldName;
            this.fieldLocalization = fieldLocalization;
            this.jdbcType = null;
            this.columnSize = null;
        }

        public FieldMetaData(String fieldName) {
            this.fieldName = fieldName;
            this.fieldLocalization = null;
            this.jdbcType = null;
            this.columnSize = null;
        }

        public String getFieldName() {
            return fieldName;
        }

        @Nullable
        public String getFieldLocalization() {
            return fieldLocalization;
        }

        @Nullable
        public JDBCType getJdbcType() {
            return jdbcType;
        }

        @Nullable
        public Integer getColumnSize() {
            return columnSize;
        }

        public void setJdbcType(@Nullable JDBCType jdbcType) {
            this.jdbcType = jdbcType;
        }

        public void setColumnSize(@Nullable Integer columnSize) {
            this.columnSize = columnSize;
        }
    }
}
