<%*
const labels = ["Personal", "Alten", "Stellantis"];
const areas = ["personal", "alten", "stellantis"];
const area = await tp.system.suggester(labels, areas, false, "Meeting area");

if (!area) {
  await tp.file.delete();
  return;
}

const subjectInput = await tp.system.prompt("Meeting subject / object?");
if (!subjectInput?.trim()) {
  await tp.file.delete();
  return;
}

const notesInput = await tp.system.prompt("Meeting notes", "", false, true);
const tasksInput = await tp.system.prompt("Tasks — one task per line", "", false, true);

const areaLabel = labels[areas.indexOf(area)];
const destinationByArea = {
  personal: "06 Personal/Meetings",
  alten: "02 Work/Alten/Meetings",
  stellantis: "02 Work/Stellantis/Meetings",
};
const destinationFolder = destinationByArea[area];

if (!app.vault.getAbstractFileByPath(destinationFolder)) {
  await app.vault.createFolder(destinationFolder);
}

const subject = subjectInput.trim();
const safeSubject = subject
  .replace(/[\\/:*?"<>|#^[\]]/g, "-")
  .replace(/\s+/g, " ")
  .trim();
const date = tp.date.now("YYYY-MM-DD");
const time = tp.date.now("HH:mm");
const baseName = `${date} - ${safeSubject}`;
let fileName = baseName;
let counter = 2;
while (app.vault.getAbstractFileByPath(`${destinationFolder}/${fileName}.md`)) {
  fileName = `${baseName} ${counter}`;
  counter += 1;
}

await tp.file.move(`${destinationFolder}/${fileName}`);

const yamlSubject = subject.replace(/"/g, '\\"');
const notes = (notesInput ?? "")
  .split(/\r?\n/)
  .map((line) => line.trim())
  .filter(Boolean)
  .map((line) => /^[-*]\s/.test(line) ? line : `- ${line}`)
  .join("\n") || "-";
const tasks = (tasksInput ?? "")
  .split(/\r?\n/)
  .map((line) => line.trim())
  .filter(Boolean)
  .map((line) => line
    .replace(/^[-*]\s+\[[ xX]\]\s*/, "")
    .replace(/^[-*]\s+/, ""))
  .map((line) => `- [ ] ${line}`)
  .join("\n") || "- [ ]";

tR += `---
type: meeting
area: ${area}
subject: "${yamlSubject}"
date: ${date}
time: ${time}
created: ${date}
updated: ${date}
tags:
  - meeting
  - ${area}
---

# ${areaLabel} — ${subject}

## Object

${subject}

## Notes

${notes}

## Tasks

${tasks}
`;
-%>
