package gc.mda.kcg.collector.aircraft;

import gc.mda.kcg.domain.aircraft.AircraftCategory;

import java.util.Map;

/**
 * 항공기 카테고리 분류 유틸리티.
 * 프론트엔드 airplaneslive.ts / opensky.ts의 분류 로직을 이식.
 */
public final class AircraftClassifier {

    private AircraftClassifier() {}

    // Airplanes.live 타입코드 기반 분류
    private static final Map<String, AircraftCategory> TYPE_CATEGORY_MAP = Map.ofEntries(
            // Fighter
            Map.entry("F16", AircraftCategory.FIGHTER),
            Map.entry("F15", AircraftCategory.FIGHTER),
            Map.entry("F15E", AircraftCategory.FIGHTER),
            Map.entry("FA18", AircraftCategory.FIGHTER),
            Map.entry("F22", AircraftCategory.FIGHTER),
            Map.entry("F35", AircraftCategory.FIGHTER),
            Map.entry("F14", AircraftCategory.FIGHTER),
            Map.entry("EF2K", AircraftCategory.FIGHTER),
            Map.entry("RFAL", AircraftCategory.FIGHTER),
            Map.entry("SU27", AircraftCategory.FIGHTER),
            Map.entry("SU30", AircraftCategory.FIGHTER),
            Map.entry("SU35", AircraftCategory.FIGHTER),
            // Tanker
            Map.entry("KC10", AircraftCategory.TANKER),
            Map.entry("KC30", AircraftCategory.TANKER),
            Map.entry("KC46", AircraftCategory.TANKER),
            Map.entry("K35R", AircraftCategory.TANKER),
            Map.entry("KC35", AircraftCategory.TANKER),
            Map.entry("A332", AircraftCategory.TANKER),
            // Surveillance
            Map.entry("RC135", AircraftCategory.SURVEILLANCE),
            Map.entry("E3", AircraftCategory.SURVEILLANCE),
            Map.entry("E8", AircraftCategory.SURVEILLANCE),
            Map.entry("RQ4", AircraftCategory.SURVEILLANCE),
            Map.entry("MQ9", AircraftCategory.SURVEILLANCE),
            Map.entry("P8", AircraftCategory.SURVEILLANCE),
            Map.entry("EP3", AircraftCategory.SURVEILLANCE),
            Map.entry("E6", AircraftCategory.SURVEILLANCE),
            Map.entry("U2", AircraftCategory.SURVEILLANCE),
            // Cargo
            Map.entry("C17", AircraftCategory.CARGO),
            Map.entry("C5", AircraftCategory.CARGO),
            Map.entry("C130", AircraftCategory.CARGO),
            Map.entry("C2", AircraftCategory.CARGO)
    );

    // OpenSky 콜사인 prefix 기반 분류
    private static final Map<String, AircraftCategory> CALLSIGN_PREFIX_MAP = Map.ofEntries(
            Map.entry("RCH", AircraftCategory.CARGO),
            Map.entry("REACH", AircraftCategory.CARGO),
            Map.entry("KING", AircraftCategory.TANKER),
            Map.entry("ETHYL", AircraftCategory.TANKER),
            Map.entry("STEEL", AircraftCategory.TANKER),
            Map.entry("PACK", AircraftCategory.TANKER),
            Map.entry("NCHO", AircraftCategory.TANKER),
            Map.entry("JULEP", AircraftCategory.TANKER),
            Map.entry("IRON", AircraftCategory.FIGHTER),
            Map.entry("VIPER", AircraftCategory.FIGHTER),
            Map.entry("RAGE", AircraftCategory.FIGHTER),
            Map.entry("DEATH", AircraftCategory.FIGHTER),
            Map.entry("TOXIN", AircraftCategory.SURVEILLANCE),
            Map.entry("OLIVE", AircraftCategory.SURVEILLANCE),
            Map.entry("COBRA", AircraftCategory.SURVEILLANCE),
            Map.entry("FORTE", AircraftCategory.SURVEILLANCE),
            Map.entry("HAWK", AircraftCategory.SURVEILLANCE),
            Map.entry("GLOBAL", AircraftCategory.SURVEILLANCE),
            Map.entry("SNTRY", AircraftCategory.SURVEILLANCE),
            Map.entry("WIZARD", AircraftCategory.SURVEILLANCE),
            Map.entry("REAPER", AircraftCategory.SURVEILLANCE),
            Map.entry("DRAGON", AircraftCategory.SURVEILLANCE),
            Map.entry("DOOM", AircraftCategory.MILITARY),
            Map.entry("EVAC", AircraftCategory.MILITARY),
            Map.entry("SAM", AircraftCategory.MILITARY),
            Map.entry("EXEC", AircraftCategory.MILITARY),
            Map.entry("NAVY", AircraftCategory.MILITARY),
            Map.entry("TOPCT", AircraftCategory.MILITARY)
    );

    /**
     * 타입코드 기반 분류 (Airplanes.live).
     * 타입코드에 군용 코드가 포함되어 있으면 해당 카테고리 반환.
     */
    public static AircraftCategory classifyByTypecode(String typecode) {
        if (typecode == null || typecode.isBlank()) return AircraftCategory.CIVILIAN;
        String upper = typecode.toUpperCase();
        for (var entry : TYPE_CATEGORY_MAP.entrySet()) {
            if (upper.contains(entry.getKey())) return entry.getValue();
        }
        return AircraftCategory.CIVILIAN;
    }

    /**
     * dbFlags 기반 군용기 판별 (Airplanes.live).
     * dbFlags & 1 == 1이면 DB에서 군용기로 분류된 항공기.
     */
    public static boolean isMilitaryByDbFlags(int dbFlags) {
        return (dbFlags & 1) == 1;
    }

    /**
     * 콜사인 prefix 기반 분류 (OpenSky).
     */
    public static AircraftCategory classifyByCallsign(String callsign) {
        if (callsign == null || callsign.isBlank()) return AircraftCategory.CIVILIAN;
        String upper = callsign.toUpperCase().trim();
        for (var entry : CALLSIGN_PREFIX_MAP.entrySet()) {
            if (upper.startsWith(entry.getKey())) return entry.getValue();
        }
        return AircraftCategory.CIVILIAN;
    }
}
