using System.Threading.Tasks;

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


namespace AIRuntime.Runtime.Filesystem
{
    /// <summary>
    /// Moves a file within the current workspace.
    /// </summary>
    public sealed class MoveFileCapability
        : CapabilityBase<FileTransferArguments>
    {
        public override string Name =>
            "move_file";


        public override string Description =>
            "Moves a file within the current workspace. " +
            "Missing destination directories are created and an existing destination file is replaced.";


        public override CapabilityPermission Permission =>
            CapabilityPermission.Destructive;


        protected override async Task<object> ExecuteAsync(
            FileTransferArguments args,
            CapabilityContext context)
        {
            context.CancellationToken
                .ThrowIfCancellationRequested();


            await context.FileSystem.MoveFileAsync(
                args.SourcePath,
                args.DestinationPath);


            context.CancellationToken
                .ThrowIfCancellationRequested();


            return "File moved successfully: " +
                   args.SourcePath +
                   " -> " +
                   args.DestinationPath;
        }
    }
}
