package cosmosgen

import (
	"os"
	"path/filepath"
)

const routeNameTemplate = `<%
const { routeInfo, utils } = it;
const {
  operationId,
  method,
  route,
  moduleName,
  responsesTypes,
  description,
  tags,
  summary,
  pathArgs,
} = routeInfo;
const { _, fmtToJSDocLine, require } = utils;

const methodAliases = {
  get: (pathName, hasPathInserts) =>
    _.camelCase(` + "`" + `${pathName}_${hasPathInserts ? "detail" : "list"}` + "`" + `),
  post: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_create` + "`" + `),
  put: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_update` + "`" + `),
  patch: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_partial_update` + "`" + `),
  delete: (pathName, hasPathInserts) => _.camelCase(` + "`" + `${pathName}_delete` + "`" + `),
};

const createCustomOperationId = (method, route, moduleName) => {
  const hasPathInserts = /\{(\w){1,}\}/g.test(route);
  const splitedRouteBySlash = _.compact(_.replace(route, /\{(\w){1,}\}/g, "").split("/"));
  const routeParts = (splitedRouteBySlash.length > 1
    ? splitedRouteBySlash.splice(1)
    : splitedRouteBySlash
  ).join("_");
  return routeParts.length > 3 && methodAliases[method]
    ? methodAliases[method](routeParts, hasPathInserts)
    : _.camelCase(_.lowerCase(method) + "_" + [moduleName].join("_")) || "index";
};

if (operationId) {
	let routeName = operationId.replace('_','');	
  return routeName[0].toLowerCase() + routeName.slice(1);
}
if (route === "/")
  return _.camelCase(` + "`" + `${_.lowerCase(method)}Root` + "`" + `);

return createCustomOperationId(method, route, moduleName);
%>`

// generateRouteNameFile generates the `route-name.eta` file.
func generateRouteNameFile(outPath string) error {
	outTemplate := filepath.Join(outPath, "route-name.eta")
	return os.WriteFile(outTemplate, []byte(routeNameTemplate), 0o600)
}
