using System.Threading.Tasks;

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


namespace AIRuntime.Runtime.Filesystem
{
    /// <summary>
    /// Checks whether a file exists in the current workspace.
    /// </summary>
    public sealed class FileExistsCapability
        : CapabilityBase<FileSystemPathArguments>
    {
        public override string Name =>
            "file_exists";


        public override string Description =>
            "Returns true when a file exists at the supplied workspace path.";


        public override CapabilityPermission Permission =>
            CapabilityPermission.ReadOnly;


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


            bool exists =
                await context.FileSystem.FileExistsAsync(
                    args.Path);


            context.CancellationToken
                .ThrowIfCancellationRequested();


            return exists;
        }
    }
}
