import { AlarmScopeModel, AlarmScopeStatus } from "@/contracts/client.schemas";
import { Steps } from "@/pages/AlarmDialog/model/Steps";

export function getLastAlarmScopeStatusChanging(alarmScope: AlarmScopeModel) {
	const sortedHistory = alarmScope.statusHistory.sort(
		(a, b) =>
			new Date(b.changedAt).getTime() - new Date(a.changedAt).getTime(),
	);
	return sortedHistory[0];
}

export function getAlarmScopeStatus(alarmScope: AlarmScopeModel) {
	const lastStatus = getLastAlarmScopeStatusChanging(alarmScope);
	return lastStatus.status;
}

export function getStepByStatus(status: AlarmScopeStatus): Steps {
	if (status === "New") {
		return "accept";
	}

	if (status === "Accepted") {
		return "camera";
	}

	if (status === "CamerasViewed") {
		return "responsibleCalls";
	}

	if (status === "CallsCompleted") {
		return "selectAlarmReality";
	}

	if (status === "CommentAndSend") {
		return "confirmation";
	}

	if (status === "Resolved") {
		return "completed";
	}

	throw new Error("Invalid status");
}
