package incheon.uis.ums.shp;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * SHP DBF 컬럼명 10자 제한으로 인한 약자명 매핑 테이블
 *
 * - fullToShp(tableName, fullName) : 원본컬럼명 → SHP약자명 (SHP 생성 시)
 * - shpToFull(tableName, shpName)  : SHP약자명 → 원본컬럼명 (INSERT/UPDATE 시)
 */
public class ShpColumnMapping {

    // 테이블명 → (원본컬럼명 → SHP약자명)
    private static final Map<String, Map<String, String>> FULL_TO_SHP;

    // 테이블명 → (SHP약자명 → 원본컬럼명)
    private static final Map<String, Map<String, String>> SHP_TO_FULL;

    static {
        Map<String, Map<String, String>> fullToShp = new HashMap<>();
        Map<String, Map<String, String>> shpToFull = new HashMap<>();

        register(fullToShp, shpToFull, "drl_dpl_ls",
                entry("complt_length",      "complt_len"),
                entry("complt_width",       "complt_wid"),
                entry("construct_time",     "construc1"),
                entry("construct_corp",     "construc2"),
                entry("construct_tel",      "construc3"),
                entry("construct_manager",  "construc4"),
                entry("construct_name",     "construct_"),
                entry("occupy_place_area",  "occupy_pla"),
                entry("occupy_purpose",     "occupy_pur"),
                entry("rdig_status",        "rdig_statu"),
                entry("recov_method",       "recov_meth")
        );

        register(fullToShp, shpToFull, "gel_kisn_p",
                entry("decisiondate",  "decisionda"),
                entry("installdate",   "installdat"),
                entry("project_name",  "project_na")
        );

        register(fullToShp, shpToFull, "gel_sriver_a",
                entry("noni_lftbnk_cnt", "noni_lftbn"),
                entry("noni_rhtbnk_cnt", "noni_rhtbn")
        );

        register(fullToShp, shpToFull, "grl_bejn_p",
                entry("stlm_arr_mthd_cd", "stlm_arr_m")
        );

        register(fullToShp, shpToFull, "grl_ccut_l",
                entry("stlm_arr_mthd_cd", "stlm_arr_m")
        );

        register(fullToShp, shpToFull, "rdl_bejn_p",
                entry("stlm_arr_mthd_cd", "stlm_arr_m")
        );

        register(fullToShp, shpToFull, "rdl_ccut_l",
                entry("stlm_arr_mthd_cd", "stlm_arr_m")
        );

        register(fullToShp, shpToFull, "wtl_pipe_l",
                entry("pblsrv_indx", "pblsrv_ind")
        );

        FULL_TO_SHP = Collections.unmodifiableMap(fullToShp);
        SHP_TO_FULL = Collections.unmodifiableMap(shpToFull);
    }

    /**
     * 원본 컬럼명 → SHP 약자명
     * 매핑이 없으면 앞 10자 자동 truncate 반환
     */
    public static String fullToShp(String tableName, String fullName) {
        if (tableName == null || fullName == null) return truncate(fullName);
        Map<String, String> tableMap = FULL_TO_SHP.get(tableName);
        if (tableMap != null && tableMap.containsKey(fullName)) {
            return tableMap.get(fullName);
        }
        return truncate(fullName);
    }

    /**
     * SHP 약자명 → 원본 컬럼명
     * 매핑이 없으면 shpName 그대로 반환
     */
    public static String shpToFull(String tableName, String shpName) {
        if (tableName == null || shpName == null) return shpName;
        Map<String, String> tableMap = SHP_TO_FULL.get(tableName);
        if (tableMap != null && tableMap.containsKey(shpName)) {
            return tableMap.get(shpName);
        }
        return shpName;
    }

    /**
     * 해당 테이블의 전체 SHP→원본 매핑 반환 (없으면 빈 Map)
     */
    public static Map<String, String> getShpToFullMap(String tableName) {
        return SHP_TO_FULL.getOrDefault(tableName, Collections.emptyMap());
    }

    // ======== 내부 헬퍼 ========

    private static String truncate(String name) {
        if (name == null) return null;
        return name.length() > 10 ? name.substring(0, 10) : name;
    }

    @SafeVarargs
    private static void register(Map<String, Map<String, String>> fullToShp,
                                  Map<String, Map<String, String>> shpToFull,
                                  String tableName,
                                  String[]... entries) {
        Map<String, String> fts = new HashMap<>();
        Map<String, String> stf = new HashMap<>();
        for (String[] e : entries) {
            fts.put(e[0], e[1]);  // fullName → shpName
            stf.put(e[1], e[0]);  // shpName  → fullName
        }
        fullToShp.put(tableName, Collections.unmodifiableMap(fts));
        shpToFull.put(tableName, Collections.unmodifiableMap(stf));
    }

    private static String[] entry(String fullName, String shpName) {
        return new String[]{fullName, shpName};
    }
}