using Netina.Domain.Enums;

namespace Netina.Api.Controllers;

public class FileController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("File")
            .MapGroup("api/file");

        group.MapGet("", GetFilesAsync)
            .WithDisplayName("GetFilesAsync")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageFiles, ApplicationPermission.ViewFiles))
            .HasApiVersion(1.0);

        group.MapPost("", UploadFileAsync)
            .WithDisplayName("UploadFileAsync")
            .HasApiVersion(1.0);

    }

    public async Task<IResult> GetFilesAsync([FromQuery]StorageFileType? fileType,[FromServices] IStorageService storageService, CancellationToken cancellationToken)
        => TypedResults.Ok(await storageService.GetStorageFiles(fileType: fileType ?? StorageFileType.Image));

    public async Task<IResult> UploadFileAsync([FromBody] FileUploadRequest uploadRequest, [FromServices] IUploadFileService uploadFileService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await uploadFileService.UploadImageAsync(uploadRequest));
    }
}