import { StringCaser } from "@/components/WebExportReact/src/utils/helpers";
import { getType } from "../utils/helpers";

export const mapRequestModels = (configuration, model) => {
  if (model?.inputs?.length == 0) return;
  const fieldListStr = model?.inputs
    ?.filter((field) => field.type == "body")
    .map((singleField) => {
      return `val ${singleField.name}: ${getType(
        singleField.dataType ?? "String"
      )}`;
    })
    .join(",\n");

  if (fieldListStr.trim().length == "") return;

  const modelName = StringCaser(model.name, { casetype: "PascalCase" });

  return {
    fileName: `${modelName}Request.kt`,
    content: `
package ${configuration.packageName}.model.remote

data class ${modelName}Request(
    ${fieldListStr}
)
  `,
  };
};

export const mapResponseModels = (configuration, model) => {

  let fieldList = "";
  let excludedKeys = ["message", "data", "page", "total"];
  if (model.columns && typeof model.columns === "object") {
    fieldList = Object.keys(model.columns)
      .filter((field) => !excludedKeys.includes(field))
      .map(
        (key) =>
          `val ${key}: ${getType(model.columns[key]?.valueType ?? "String")}`
      )
      .join(",\n");
  }

  let fieldListStr = model.response
    .filter((field) => field.parent == "")
    .map((singleField, index, array) => {
      return `val ${singleField.key}: ${getType(
        singleField.valueType ?? "String"
      )}${index === array.length - 1 ? "," : ""}`;
    })
    .join(",\n");

    // console.log("fieldList,  fieldListStr: ", fieldList,  fieldListStr)

  if (fieldListStr.trim() == "") fieldListStr = `val error: Boolean`;

  const modelName = StringCaser(model.name, { casetype: "PascalCase" });

  return {
    fileName: `${modelName}Response.kt`,
    content: `
    package ${configuration.packageName}.model.remote
    import ${configuration.packageName}.data.DataModel
    data class ${modelName}Response(
        ${fieldListStr}
        ${fieldList}
    )
  `,
  };
};
