package p.studio.compiler.source.diagnostics;

import java.util.Locale;

public enum DiagnosticPhase {
    SYNTAX("syntax"),
    STATIC_SEMANTICS("static-semantics"),
    MANIFEST_IMPORT_RESOLUTION("manifest-import-resolution"),
    LINKING("linking"),
    HOST_ADMISSION("host-admission"),
    LOAD_FACING_REJECTION("load-facing-rejection"),
    UNKNOWN("unknown"),
    ;

    private final String id;

    DiagnosticPhase(final String id) {
        this.id = id;
    }

    public String id() {
        return id;
    }

    public static DiagnosticPhase fromCode(final String code) {
        if (code == null || code.isBlank()) {
            return UNKNOWN;
        }

        final var normalized = code.toUpperCase(Locale.ROOT);
        if (normalized.startsWith("E_PARSE_") || normalized.startsWith("E_LEX_")
                || normalized.startsWith("W_PARSE_") || normalized.startsWith("W_LEX_")) {
            return SYNTAX;
        }
        if (normalized.startsWith("E_SEM_") || normalized.startsWith("W_SEM_")) {
            return STATIC_SEMANTICS;
        }
        if (normalized.startsWith("E_LINK_") || normalized.startsWith("W_LINK_")) {
            return LINKING;
        }
        if (normalized.startsWith("E_MANIFEST_") || normalized.startsWith("W_MANIFEST_")
                || normalized.startsWith("E_IMPORT_") || normalized.startsWith("W_IMPORT_")) {
            return MANIFEST_IMPORT_RESOLUTION;
        }
        if (normalized.startsWith("E_HOST_") || normalized.startsWith("W_HOST_")) {
            return HOST_ADMISSION;
        }
        if (normalized.startsWith("E_LOAD_") || normalized.startsWith("W_LOAD_")) {
            return LOAD_FACING_REJECTION;
        }
        return UNKNOWN;
    }
}
