package gc.mda.signal_batch.global.util;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * MDA 선종 범례코드
 *
 * S&P Global AIS API의 vesselType + extraInfo를 기반으로
 * MDA 범례코드(signalKindCode)로 치환한다.
 *
 * ShipKindCodeConverter를 대체하며, SNP-Batch-1의 치환 로직을 이식.
 */
@Getter
@RequiredArgsConstructor
public enum SignalKindCode {

    FISHING("000020", "어선"),
    KCGV("000021", "함정"),
    FERRY("000022", "여객선"),
    CARGO("000023", "카고"),
    TANKER("000024", "탱커"),
    GOV("000025", "관공선"),
    DEFAULT("000027", "일반/기타선박"),
    BUOY("000028", "부이/항로표지");

    private final String code;
    private final String koreanName;

    /**
     * vesselType + extraInfo → MDA 범례코드 치환
     *
     * 치환 우선순위:
     * 1. vesselType 단독 매칭 (Cargo, Tanker, Passenger, AtoN 등)
     * 2. vesselType + extraInfo 조합 매칭 (Vessel + Fishing 등)
     * 3. fallback → DEFAULT (000027)
     */
    public static SignalKindCode resolve(String vesselType, String extraInfo) {
        String vt = normalizeOrEmpty(vesselType);
        String ei = normalizeOrEmpty(extraInfo);

        // 1. vesselType 단독 매칭
        switch (vt) {
            case "cargo":
                return CARGO;
            case "tanker":
                return TANKER;
            case "passenger":
                return FERRY;
            case "aton":
                return BUOY;
            case "law enforcement":
                return GOV;
            case "search and rescue":
                return KCGV;
            case "local vessel":
                return FISHING;
            default:
                break;
        }

        // vesselType 그룹 매칭
        if (matchesAny(vt, "tug", "pilot boat", "tender", "anti pollution", "medical transport")) {
            return GOV;
        }
        if (matchesAny(vt, "high speed craft", "wing in ground-effect")) {
            return FERRY;
        }

        // 2. "Vessel" + extraInfo 조합
        if ("vessel".equals(vt)) {
            return resolveVesselExtraInfo(ei);
        }

        // 3. "N/A" + extraInfo 조합
        if ("n/a".equals(vt)) {
            if (ei.startsWith("hazardous cat")) {
                return CARGO;
            }
            return DEFAULT;
        }

        // 4. fallback
        return DEFAULT;
    }

    private static SignalKindCode resolveVesselExtraInfo(String extraInfo) {
        if ("fishing".equals(extraInfo)) {
            return FISHING;
        }
        if ("military operations".equals(extraInfo)) {
            return GOV;
        }
        if (matchesAny(extraInfo, "towing", "towing (large)", "dredging/underwater ops", "diving operations")) {
            return GOV;
        }
        if (matchesAny(extraInfo, "pleasure craft", "sailing", "n/a")) {
            return FISHING;
        }
        if (extraInfo.startsWith("hazardous cat")) {
            return CARGO;
        }
        return DEFAULT;
    }

    private static boolean matchesAny(String value, String... candidates) {
        for (String candidate : candidates) {
            if (candidate.equals(value)) {
                return true;
            }
        }
        return false;
    }

    private static String normalizeOrEmpty(String value) {
        return (value == null || value.isBlank()) ? "" : value.strip().toLowerCase();
    }
}
