package com.ai.trackwalker.capability.workorder;

import java.util.Map;
import java.util.Set;

public final class WorkOrderStateMachine {
    private static final Map<String, String> TARGETS = Map.ofEntries(
            Map.entry("ACCEPT", "accepted"),
            Map.entry("DEPART", "en_route"),
            Map.entry("ARRIVE", "on_site"),
            Map.entry("START_PROCESSING", "processing"),
            Map.entry("SUBMIT", "submitted"),
            Map.entry("START_REVIEW", "reviewing"),
            Map.entry("APPROVE", "closed"),
            Map.entry("RETURN", "returned"),
            Map.entry("REOPEN", "processing")
    );
    private static final Map<String, Set<String>> ALLOWED_FROM = Map.ofEntries(
            Map.entry("ACCEPT", Set.of("created", "dispatched")),
            Map.entry("DEPART", Set.of("accepted")),
            Map.entry("ARRIVE", Set.of("en_route")),
            Map.entry("START_PROCESSING", Set.of("on_site")),
            Map.entry("SUBMIT", Set.of("processing", "returned")),
            Map.entry("START_REVIEW", Set.of("submitted")),
            Map.entry("APPROVE", Set.of("submitted", "reviewing")),
            Map.entry("RETURN", Set.of("submitted", "reviewing")),
            Map.entry("REOPEN", Set.of("returned"))
    );

    private WorkOrderStateMachine() {
    }

    public static boolean supports(String action) {
        return TARGETS.containsKey(action);
    }

    public static boolean canApply(String status, String action) {
        return ALLOWED_FROM.getOrDefault(action, Set.of()).contains(status);
    }

    public static String target(String action) {
        return TARGETS.get(action);
    }
}
