using System.Threading.Tasks;

using AIRuntime.Runtime.Capabilities;
using AIRuntime.Runtime.Capabilities.Permissions;
using AIRuntime.Runtime.Filesystem.Models;


namespace AIRuntime.Runtime.Filesystem
{
    /// <summary>
    /// Appends UTF-8 text to a file in the current workspace.
    /// </summary>
    public sealed class AppendFileCapability
        : CapabilityBase<AppendFileArguments>
    {
        public override string Name =>
            "append_file";


        public override string Description =>
            "Appends the supplied UTF-8 text to a workspace file without replacing existing contents. If the existing content does not end with a newline and the appended text does not start with one, exactly one newline is inserted automatically. You must call this tool when the user asks to append or add text. Never claim success unless this tool returns success.";


        public override CapabilityPermission Permission =>
            CapabilityPermission.Write;


        protected override async Task<object> ExecuteAsync(
            AppendFileArguments args,
            CapabilityContext context)
        {
            await context.FileSystem.AppendFileAsync(
                args.Path,
                args.Content);


            return "File appended successfully: " +
                   args.Path;
        }
    }
}
