package com.ai.trackwalker.uavaccess.event;

import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class VendorStatusNormalizer {
    public String normalize(Object rawStatus) {
        if (rawStatus == null) return "UNKNOWN";
        String status = String.valueOf(rawStatus).trim().toLowerCase(Locale.ROOT);
        if (status.isBlank()) return "UNKNOWN";
        if (contains(status, "complete", "finished", "success", "done")) return "COMPLETED";
        if (contains(status, "fail", "error")) return "FAILED";
        if (contains(status, "abort", "terminate")) return "ABORTED";
        if (contains(status, "cancel")) return "CANCELLED";
        if (contains(status, "return", "rth")) return "RETURNING";
        if (contains(status, "pause", "suspend")) return "PAUSED";
        if (contains(status, "flying", "running", "executing", "in_progress")) return "FLYING";
        if (contains(status, "prepare", "takeoff")) return "PREPARING";
        if (contains(status, "accepted", "pending", "queued", "ready", "scheduled")) return "DISPATCHED";
        if (contains(status, "upload")) return "UPLOADING";
        return "UNKNOWN";
    }

    private boolean contains(String status, String... candidates) {
        for (String candidate : candidates) {
            if (status.contains(candidate)) return true;
        }
        return false;
    }
}
