
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Resource Allocation functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


/**
 * Open an Ai chat with the records dislayed in the Resource Allocation chart
 * @returns
 */
function ResourceAllocationOpenAiChat() {

    // Search only within the active content
    var ContentDiv = GetSelectedContentInformationDiv();
    if (!ContentDiv) {
        ToastError("Chat With AI", "This content has no data to share with AI");
        return;
    }

    // Find all the records to share with Ai chat
    var aRecordIds = [];
    $(ContentDiv).find(".HiddenRecordIds").each(function () {
        var sVal = $(this).val().trim();
        if (sVal.length)
            aRecordIds = aRecordIds.concat(sVal.split(",").map(function (s) { return s.trim(); }).filter(function (s) { return s.length; }));
    });

    // None?
    if (!aRecordIds.length) {
        ToastError("Chat With AI", "No records found to share with AI. Try filtering the page by future date ranges or project");
        return;
    }

    // Only proceed if the user answers Yes to the number of records to be sent to AI for analyis and updates
    // Sending a large number of records to ai could result in high token usage and costs
    // Warn the user first, then proceed if Yes
    // proceed() is a named function so it can be passed as a callback to CheckAskSendNumberOfRecordsToAi
    function proceed() {
        var RecordIds = aRecordIds.join(",");
        var PropertyType = "";
        var ContentType = GetSelectedContentType();

        var sAiChatSessionId = GetActiveAiChatSessionId();
        if (sAiChatSessionId) {
            var sAiModelSetId = AiChatWindows[sAiChatSessionId].modelId;
            AjaxAiSelectRecordIdsChange(sAiChatSessionId, PropertyType, ContentType, "", RecordIds, sAiModelSetId, true/*AskFirst*/);
        } else {
            OpenAiModelChatAndThen(function (sNewAiModelSetId, sNewAiChatSessionId) {
                AjaxAiSelectRecordIdsChange(sNewAiChatSessionId, PropertyType, ContentType, "", RecordIds, sNewAiModelSetId, true/*AskFirst*/);
            });
        }
    }

    CheckAskSendNumberOfRecordsToAi(aRecordIds.length, proceed);
}


