/*
 * ChatConstants.cpp
 *
 * Copyright (C) 2025 by Posit Software, PBC
 *
 * Unless you have received this program directly from Posit Software pursuant
 * to the terms of a commercial license agreement with Posit Software, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

#include "ChatConstants.hpp"

#include <boost/algorithm/string/predicate.hpp>

namespace rstudio {
namespace session {
namespace modules {
namespace chat {
namespace constants {

// Installation paths
const char* const kPositAiDirName = "pai/bin";
const char* const kPositAiBackupDirName = "ai.prev";
const char* const kClientDirPath = "dist/client";
const char* const kServerScriptPath = "dist/server/main.js";
const char* const kIndexFileName = "index.html";
const char* const kCspConfigPath = "dist/csp.json";
const char* const kProtocolVersionFileName = "protocol.json";

// Protocol Version (SUPPORTED_PROTOCOL_VERSION)
const char* const kProtocolVersion = "11.0";

// Capabilities: JSON-RPC methods that RStudio handles
const std::vector<std::string>& rstudioCapabilities()
{
   static const std::vector<std::string> s_capabilities = {
      "runtime/getActiveSession",
      "runtime/getDetailedContext",
      "runtime/executeCode",
      "runtime/executeCode/interleavingPrimitives",
      "runtime/getConsoleContent",
      "workspace/readFileContent",
      "workspace/writeFileContent",
      "workspace/editFileContent",
      "workspace/insertIntoNewFile",
      "workspace/insertAtCursor",
      "ui/openDocument",
      "ui/openDocument/line",
      "ui/revealInFilesPane",
      "ui/previewUrl",
   };
   return s_capabilities;
}

std::string assembleWebSocketPath(
   const std::string& rootPath,
   const std::string& sessionUrl,
   const std::string& portmappedPath)
{
   // Normalize root path: "/" (default) becomes empty
   std::string root = rootPath;
   if (root == "/")
      root.clear();
   if (!root.empty() && root.back() == '/')
      root.pop_back();

   // Normalize session URL: strip trailing slash
   std::string session = sessionUrl;
   if (!session.empty() && session.back() == '/')
      session.pop_back();

   // Normalize portmapped path: ensure leading slash, strip trailing slash
   std::string mapped = portmappedPath;
   if (!mapped.empty() && mapped[0] != '/')
      mapped = "/" + mapped;
   if (!mapped.empty() && mapped.back() == '/')
      mapped.pop_back();

   return root + session + mapped + "/ai-chat";
}

bool isValidPreviewUrlScheme(const std::string& url)
{
   // Case-sensitive on purpose -- see header comment.
   return boost::algorithm::starts_with(url, "http://") ||
          boost::algorithm::starts_with(url, "https://");
}

bool isValidPreviewUrlHeight(int height)
{
   return height >= -1;
}

} // namespace constants
} // namespace chat
} // namespace modules
} // namespace session
} // namespace rstudio
