using System.Management.Automation;
namespace Noz.PowerShell.Cmdlets;
/// Create a new note on the server.
///
/// New-NozNote -Slug psychologie/neue-notiz -Title "Neue Notiz"
/// New-NozNote -Slug biosysteme/pilze -Title "Pilze" -Topic biosysteme -Status budding -Lang de
///
[Cmdlet(VerbsCommon.New, "NozNote", SupportsShouldProcess = true)]
[OutputType(typeof(NozNote))]
public sealed class NewNozNoteCmdlet : NozCmdletBase
{
[Parameter(Mandatory = true, Position = 0)]
public string Slug { get; set; } = "";
[Parameter(Mandatory = true, Position = 1)]
public string Title { get; set; } = "";
[Parameter]
public string? Topic { get; set; }
[Parameter]
[ValidateSet("seed", "budding", "evergreen")]
public string Status { get; set; } = "seed";
[Parameter]
[ValidateSet("de", "en")]
public string Lang { get; set; } = "de";
[Parameter]
public string Description { get; set; } = "";
protected override void ProcessRecord()
{
if (!ShouldProcess(Slug, "Create note")) return;
var topic = Topic ?? (Slug.Contains('/') ? Slug.Split('/')[0] : Slug);
var markdown = $"""
---
title: "{Title}"
description: "{Description}"
topic: {topic}
status: {Status}
lang: {Lang}
---
# {Title}
""";
Run(Api.PutNoteAsync(Slug, markdown));
var notes = Run(Api.GetNotesAsync());
var created = notes.FirstOrDefault(n => n.Slug == Slug);
if (created is not null)
WriteObject(NozNote.From(created));
}
}