package com.ai.trackwalker.capability.uav;

import java.util.Set;

public final class InspectionTaskWorkflow {
    private static final Set<String> PRE_FLIGHT_STATUSES = Set.of("created", "pending");

    private InspectionTaskWorkflow() {
    }

    public static boolean canDispatch(
            String taskStatus,
            String approvalStatus,
            boolean routeAssigned,
            boolean routePublished,
            boolean hasMission
    ) {
        return dispatchBlockReason(taskStatus, approvalStatus, routeAssigned, routePublished, hasMission) == null;
    }

    public static String dispatchBlockReason(
            String taskStatus,
            String approvalStatus,
            boolean routeAssigned,
            boolean routePublished,
            boolean hasMission
    ) {
        if (!PRE_FLIGHT_STATUSES.contains(taskStatus)) {
            return "当前任务状态不可下发";
        }
        if (!"APPROVED".equals(approvalStatus)) {
            return "任务尚未审批通过";
        }
        if (!routeAssigned) {
            return "任务尚未配置航线";
        }
        if (!routePublished) {
            return "任务航线尚未发布";
        }
        if (hasMission) {
            return "任务已存在飞行任务，不能重复下发";
        }
        return null;
    }

    public static boolean canCancel(String taskStatus, boolean hasMission) {
        return PRE_FLIGHT_STATUSES.contains(taskStatus) && !hasMission;
    }

    public static String taskStatusForMission(String missionStatus) {
        return switch (missionStatus) {
            case "FLYING", "PAUSED", "RETURNING" -> "flying";
            case "COMPLETED", "UPLOADING" -> "data_uploading";
            case "CANCELLED" -> "cancelled";
            case "ABORTED", "FAILED", "DISPATCH_FAILED" -> "failed";
            default -> "dispatched";
        };
    }
}
